LTLyaoni 发表于 2024-10-23 00:01

【Follow me第二季第2期】+ 作品提交整合贴

本帖最后由 LTLyaoni 于 2024-11-4 00:14 编辑

<p>大家好,我是LTLyaoni非常荣幸能参与到电子工程世界和得捷电子联合举办的&ldquo;Follow me&rdquo;第二期活动中来。今天,我将和大家分享我的项目进展和心得体会。</p>

<h3>物料展示</h3>

<p>这次我拿到的开发板是Arduino Uno R4 WiFi,还有LTR-329光照传感器、SHT40温湿度传感器。</p>

<p> &nbsp;</p>

<h3><strong>视频如下</strong>:</h3>

<p><iframe allowfullscreen="true" frameborder="0" height="450" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&amp;lessonid=41508" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
&nbsp;</p>

<h3>设计思路</h3>

<h4>入门任务(任务1)</h4>

<ul>
        <li><strong>目标</strong>:在原理图中确认IO端口,使用GPIO控制LED闪烁。</li>
</ul>

<h4>基础任务(任务2)</h4>

<ul>
        <li><strong>目标</strong>:读懂Arduino官方文档,掌握LED矩阵、ADC和运算放大器的使用。</li>
</ul>

<h4>进阶任务(任务3)</h4>

<ul>
        <li><strong>目标</strong>:掌握ESP32S3 WiFi库和MQTT库的使用,配置HA实体。</li>
</ul>

<h4>&nbsp;</h4>

<h3>任务实现详情</h3>

<h4>入门任务一:搭建环境并开启第一步Blink / 串口打印Hello EEWorld!</h4>

<ul>
        <li><strong>帖子地址</strong>:<a href="https://bbs.eeworld.com.cn/thread-1294066-1-1.html" target="_blank">【Follow me第二季第2期】+ 入门任务【搭建环境,Blink / 串口日志打印】</a></li>
</ul>

<p><span style="font-size:16px;"><strong>物料:</strong></span></p>

<p>Arduino Uno R4 WiFi。后面的所有的任务都用到Arduino Uno R4 WiFi,实物上图有</p>

<p><strong>流程图</strong></p>

<p> &nbsp;</p>

<p> &nbsp;</p>

<p>代码如下</p>

<pre>
<code class="language-cpp">// 定义LED连接的引脚
int ledPin = 13;
void setup() {
Serial.begin(9600);          // 初始化串口通信
pinMode(ledPin, OUTPUT);
}

void loop() {
Serial.println("LED状态: ON");
digitalWrite(ledPin, HIGH);
Serial.println("Hello World!");
Serial.println("Hello DigiKey and EEWorld!");
delay(500);
Serial.println("LED状态: OFF");
digitalWrite(ledPin, LOW);
Serial.println("Hello World!");
Serial.println("Hello DigiKey and EEWorld!");
delay(500);
}
</code></pre>

<p><strong>串口打印HelloWorld</strong></p>

<p>上述代码同时做了串口打印功能,以便我们可以看到LED灯的状态和<strong>Hello world</strong>的输出:</p>

<p>代码编译烧录后,打开IDE自带的串口监视器,设置和代码相同的波特率,即可看到相应的串口输出</p>

<p><img _height="775" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202409/17/155335pz995u9mvnqxqzgv.png.thumb.jpg" width="800" /></p>

<h4><span style="font-size:16px;"><strong>基础任务:</strong></span></h4>

<h4>驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线</h4>

<p><strong>帖子地址:</strong><a href="https://bbs.eeworld.com.cn/thread-1295331-1-1.html" target="_blank">【Follow me第二季第2期】+ 基础任务【点亮LED矩阵, 使用运算放大器放大DAC信号】</a></p>

<p>流程图:</p>

<p> &nbsp;</p>

<p><strong>Task 1 : 点亮板载Led matrix</strong></p>

<p>&nbsp; &nbsp; Step 1 : &nbsp;打开Arduino 的 demo, 选择LED matrix.</p>

<p><img _height="775" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/011927aszyzn1n1n33zl1l.png.thumb.jpg" width="800" /></p>

<p>第一个代码是使用LED的frame的方式进行点亮的, 即在数组中定义每一个LED灯的状态, 通过加载帧的方式实现的. 相关的数组在<strong>frames.h&nbsp;</strong>里</p>

<pre>
<code class="language-cpp">const uint32_t chip[] = {
        0x1503f811,
        0x3181103,
        0xf8150000
};

const uint32_t danger[] = {
        0x400a015,
        0x1502082,
        0x484047fc
};

const uint32_t happy[] = {
        0x19819,
        0x80000001,
        0x81f8000
};

const uint32_t heart[] = {
        0x3184a444,
        0x44042081,
        0x100a0040
};
</code></pre>

<p><strong>Task 2: 使用 DAC 输出正弦波</strong></p>

<p>&nbsp; &nbsp; Step 1 : 在Arduino IDE中并没有发现,关于DAC输出的Demo程序, 因此我们需要查询一下官方文档看下如何使用Arduino 的DAC功能, 根据官方文档提供的代码,我们可以很快的实现DAC输出的功能</p>

<p>流程图:</p>

<p> &nbsp;</p>

<pre>
<code class="language-cpp">/*
SineWave

Generates a pre-generated sawtooth-waveform.

See the full documentation here:
https://docs.arduino.cc/tutorials/uno-r4-wifi/dac
*/

#include "analogWave.h" // Include the library for analog waveform generation

analogWave wave(DAC);   // Create an instance of the analogWave class, using the DAC pin

int freq = 10;// in hertz, change accordingly

void setup() {
Serial.begin(115200);// Initialize serial communication at a baud rate of 115200
wave.sine(freq);       // Generate a sine wave with the initial frequency
}

void loop() {
// Read an analog value from pin A5 and map it to a frequency range
freq = map(analogRead(A5), 0, 1024, 0, 10000);

// Print the updated frequency to the serial monitor
Serial.println("Frequency is now " + String(freq) + " hz");

wave.freq(freq);// Set the frequency of the waveform generator to the updated value
delay(1000);      // Delay for one second before repeating
}

</code></pre>

<p>由于DAC的输出是A0 端口, 我们可以手动调整A5的ADC电压来动态调整A0的DAC输出频率. 如果使用示波器连接A0的话, 那么A0的输出将会是如下图所示</p>

<p><img _height="456" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/013959um7gt7ql3kogkhb1.png.thumb.jpg" width="800" /></p>

<p>Task 3 : 对DAC信号使用运算放大器进行放大, 然后使用ADC进行采集并且展示.</p>

<p>&nbsp; &nbsp; Arduino uno 4的官方文档写的比较详细, 如果想使用运算放大器对信号进行放大的话只需要构建好以下的电路.</p>

<p><img _height="497" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202409/07/195056a78da2ajaatacddj.png" width="800" /></p>

<p>我们在官方的代码上稍微做一点修改(仅仅是在代码的第八行开启了运算放大器)</p>

<pre>
<code class="language-cpp">#include "analogWave.h" // Include the library for analog waveform generation
#include &lt;OPAMP.h&gt;
analogWave wave(DAC);   // Create an instance of the analogWave class, using the DAC pin

int freq = 10;// in hertz, change accordingly

void setup() {
OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
Serial.begin(115200);// Initialize serial communication at a baud rate of 115200
wave.sine(freq);       // Generate a sine wave with the initial frequency
}

void loop() {
// Read an analog value from pin A5 and map it to a frequency range
freq = map(analogRead(A5), 0, 1024, 0, 10000);

// Print the updated frequency to the serial monitor
Serial.println("Frequency is now " + String(freq) + " hz");

wave.freq(freq);// Set the frequency of the waveform generator to the updated value
delay(50);      // Delay for one second before repeating
}

</code></pre>

<p>之后我们讲代码烧录到系统中之后再使用示波器进行电压的采集的时候发现现在的平均电压已经被运算放大器放大到了之前的两倍左右</p>

<p><img _height="1080" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/014350cswaljomazmjjqs7.png.thumb.jpg" width="1920" /></p>

<p>Task 3 : 使用ADC 采集DAC输出,并且显示.</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; 我们可以在上述代码上做一点简单的修改使其DAC的输出作为ADC的输入(电路), 这样的话,当我们打印出数据的时候, 串口绘图功能则会自动绘制ADC的采集曲线,代码如下所示</p>

<pre>
<code class="language-cpp">#include "analogWave.h" // Include the library for analog waveform generation
#include &lt;OPAMP.h&gt;
analogWave wave(DAC);   // Create an instance of the analogWave class, using the DAC pin

int freq = 10;// in hertz, change accordingly
int reading = 0;
void setup() {
OPAMP.begin(OPAMP_SPEED_HIGHSPEED);
Serial.begin(115200);// Initialize serial communication at a baud rate of 115200
analogWriteResolution(14);
wave.sine(freq);       // Generate a sine wave with the initial frequency
}

void loop() {
// Read an analog value from pin A5 and map it to a frequency range
freq = map(analogRead(A5), 0, 1024, 0, 10000);

// Print the updated frequency to the serial monitor
Serial.println("Frequency is now " + String(freq) + " hz");
reading = analogRead(A4);
Serial.print(reading);
wave.freq(freq);// Set the frequency of the waveform generator to the updated value
delay(50);      // Delay for one second before repeating
}

</code></pre>

<p><strong>进阶任务:</strong>通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)</p>

<p><strong>帖子地址:</strong><a href="https://bbs.eeworld.com.cn/thread-1295332-1-1.html" target="_blank">【Follow me第二季第2期】+ 进阶任务 :使用Arduino uno r4 接入Home assistant</a></p>

<p><strong>Task 1 : 使用Uno r4 连接Wifi</strong></p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; Arduino IDE 提供了Arduino uno r4 的wifi连接的example, 我们可以根据下面的截图进行查找</p>

<p><img _height="507" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/015137qzdc4kvgjg1j8bj1.png.thumb.jpg" width="800" /></p>

<p>在上述的提供的代码中我们只需要修改wifi的&nbsp;ssid 和 pass 即可连接到WIFI上, 我这里连接的是2.4G的wifi</p>

<p><img _height="453" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/015253ctdqbvuuqb3155t5.png.thumb.jpg" width="800" /></p>

<p>当我们把代码烧录到开发版中之后,我们会发现串口控制台已经可以正确的获取到IP地址.</p>

<p><img _height="160" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/015532mm8u985z8u777lu5.png.thumb.jpg" width="800" /></p>

<p>当你控制台出现IP地址的时候即为连接WIFI成功.</p>

<p><strong>Task 2 : 使用MQTT连接到HA</strong></p>

<p><strong>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</strong>由于MQTT的便携性和容易集成等优点,因为我们可以在HA中集成MQTT服务,使其监视某一个主题从而实现消息的上报和下发. 因为我们本章节的任务非常简单, 即使用UNO r4 连接到MQTT的某个Topic 然后在HA中监视这个主题就算成功.</p>

<p><img _height="130" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/020309stlhlckzdrn3c1pd.png.thumb.jpg" width="800" /></p>

<p>我们只需要在配置集成中, 监视这个test的主题,然后使用Uno r4 向这个主题发送消息即可.</p>

<p><strong>Task 3&nbsp;: 使用UNO R4 ,连接MQTT发送消息.</strong>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp; &nbsp; 但是很不巧Arduino uno r4 并没有提供MQTT的Demo, 所以我们需要看看官方有没有什么推荐的. 好巧不巧, 我在<a href="https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device/" target="_blank">https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device/</a>上找到了如何使用MQTT的方式, 即使用Mqtt Client. &nbsp;</p>

<p><img _height="371" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/020949zcz4a82um82nmmhm.png.thumb.jpg" width="800" /></p>

<p>虽然它代码写的比较长,但是其核心的逻辑还是使用ArduinoMqttClientt对象使用WIFI client 对象来连接MQTT. 那么我们便可以在上述的代码中做出以下的修改</p>

<pre>
<code class="language-cpp">#include &lt;ArduinoMqttClient.h&gt;
#include &lt;WiFiS3.h&gt;
#include &lt;WiFiClient.h&gt;

char ssid[] = "你的Wi-Fi名称";      
char pass[] = "你的wifi密码";
int status = WL_IDLE_STATUS;   

const char broker[] = "你的MQTT地址";
int      port   = 端口;
const char topic[]= "主题";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

void setup() {
Serial.begin(115200);
while (!Serial) {

}

if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
}

String fv = WiFi.firmwareVersion();
if (fv &lt; WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
}

Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
    while (1);
}
Serial.println("You are connected to MQTT");

}


void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid;
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}

void printMacAddress(byte mac[]) {
for (int i = 0; i &lt; 6; i++) {
    if (i &gt; 0) {
      Serial.print(":");
    }
    if (mac &lt; 16) {
      Serial.print("0");
    }
    Serial.print(mac, HEX);
}
Serial.println();
}

void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");

Serial.println(ip);
// print your MAC address:
byte mac;
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}



void loop() {
    mqttClient.beginMessage(topic);
    mqttClient.print("Message from UNO R4");
    mqttClient.endMessage();
    delay(500);
}



</code></pre>

<p>注意上述的代码是在匿名的情况下连接到MQTT服务器的,如果你的MQTT服务器不支持匿名连接的话你需要自己调用mqttclinen的设置用户名和密码的function来认证你的连接. 此时我们打开HA的话我们开启监听即可收到来自Uno r4的消息 (注意HA中MQTT监视的topic是test主题,但是上述代码中我并没有写, 方便大家复用我的代码)</p>

<p><img _height="371" border="0" src="https://bbs.eeworld.com.cn/data/attachment/forum/202410/03/021802riebt45zxpx4pfxx.png.thumb.jpg" width="800" /></p>

<p>我们有一个智能灯,我们可以通过发送命令来控制它。如果我们想让灯关掉,我们就发送一个&ldquo;OFF&rdquo;的指令;如果我们想让灯打开,我们就发送一个&ldquo;ON&rdquo;的指令。</p>

<p>现在,虽然我们可以通过这些指令来控制灯的开关,但是家里的智能家居系统(我们称之为HA)并不知道灯的状态已经改变了。为了让HA知道灯的状态,我们还需要发送一个额外的指令,告诉它灯现在是关着的(&ldquo;OFF&rdquo;)。</p>

<p>为了解决这个问题,我们可以稍微调整一下代码,让它每隔一秒钟就自动切换灯的状态。这样,灯就会自动地一会儿开,一会儿关,同时HA也会实时更新灯的状态,知道灯现在是开着还是关着。这样,我们就可以确保灯的状态和HA中的显示是一致的。</p>

<pre>
<code class="language-cpp">#include &lt;ArduinoMqttClient.h&gt;
#include &lt;WiFiS3.h&gt;
#include &lt;WiFiClient.h&gt;

char ssid[] = "ssid";      
char pass[] = "pass";
int status = WL_IDLE_STATUS;   

const char broker[] = "broker";
int      port   = 1883;
const char command_topic[]= "home/bedroom/switch1/set";
const char state_topic[]= "home/bedroom/switch1";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

bool index = true;

void setup() {
Serial.begin(9600);
while (!Serial) {

}

if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
}

String fv = WiFi.firmwareVersion();
if (fv &lt; WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
}

Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
mqttClient.setUsernamePassword("","");
if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
    while (1);
}
Serial.println("You are connected to MQTT");

}


void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid;
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}

void printMacAddress(byte mac[]) {
for (int i = 0; i &lt; 6; i++) {
    if (i &gt; 0) {
      Serial.print(":");
    }
    if (mac &lt; 16) {
      Serial.print("0");
    }
    Serial.print(mac, HEX);
}
Serial.println();
}

void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");

Serial.println(ip);
// print your MAC address:
byte mac;
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}



void loop() {
if(index)
{
    mqttClient.beginMessage(command_topic);
    mqttClient.print("ON");
    mqttClient.endMessage();
    mqttClient.beginMessage(state_topic);
    mqttClient.print("ON");
    mqttClient.endMessage();
    Serial.print("ON\n");
}else {
    mqttClient.beginMessage(command_topic);
    mqttClient.print("OFF");
    mqttClient.endMessage();
    mqttClient.beginMessage(state_topic);
    mqttClient.print("OFF");
    mqttClient.endMessage();
    Serial.print("OFF\n");
}
index = !index;
delay(1000);

}




</code></pre>

<p><strong>拓展任务一:扩展任务一 &nbsp;使用LTR-329 环境光传感器,上传到HA并显示</strong></p>

<p> &nbsp;</p>

<p><strong>流程图如下:</strong></p>

<p> &nbsp;</p>

<p>这个拓展任务听起来挺有意思的,我们可以一步步来搞定。这个任务主要就是三件事:</p>

<ol>
        <li>
        <p><strong>读取LTR-329传感器数据</strong>:首先,我们要用我们的Arduino R4来读取LTR-329光照传感器的数据。这个传感器能帮我们测量环境光和红外光,我们需要用到专门的库来和它通信,获取数据。</p>
        </li>
        <li>
        <p><strong>配置HA实体</strong>:然后,我们要在Home Assistant(HA)里设置实体。实体就是HA用来识别和控制设备的一种方式。我们需要告诉HA,我们有一个光照传感器,并且告诉它这个传感器的数据要怎么显示。</p>
        </li>
        <li>
        <p><strong>上传数据到HA</strong>:最后,我们要做的是把从LTR-329传感器读取到的数据上传到HA。这样,我们就可以在HA的界面上实时看到光照数据,还可以用这些数据来控制家里的其他智能设备。</p>
        </li>
</ol>

<p>既然我们已经在进阶任务里搞定了MQTT和WIFI,这次我们可以直接用那些代码,不用再从头开始。这样我们就可以更高效地完成任务了。</p>

<p>&nbsp;</p>

<p><strong>读取LTR-329传感器数据和上传数据到HA的代码如下</strong></p>

<pre>
<code class="language-cpp">#include "Adafruit_LTR329_LTR303.h"
#include &lt;ArduinoMqttClient.h&gt;
#include &lt;WiFiS3.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;Arduino_JSON.h&gt;
Adafruit_LTR329 ltr = Adafruit_LTR329();

char ssid[] = "ssid";      
char pass[] = "pass";
int status = WL_IDLE_STATUS;   

const char broker[] = "broker";
int      port   = 1883;
const char command_topic[]= "LTR303";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

JSONVar dataObj;

void setup() {
Serial.begin(115200);
Serial.println("Adafruit LTR-329 advanced test");


String fv = WiFi.firmwareVersion();
if (fv &lt; WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
}

Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
mqttClient.setUsernamePassword("","");
if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
    while (1);
}
Serial.println("You are connected to MQTT");
if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
}

if ( ! ltr.begin(&amp;Wire1) ) {
    Serial.println("Couldn't find LTR sensor!");
    while (1) delay(10);
}
Serial.println("Found LTR sensor!");

ltr.setGain(LTR3XX_GAIN_2);
Serial.print("Gain : ");
switch (ltr.getGain()) {
    case LTR3XX_GAIN_1: Serial.println(1); break;
    case LTR3XX_GAIN_2: Serial.println(2); break;
    case LTR3XX_GAIN_4: Serial.println(4); break;
    case LTR3XX_GAIN_8: Serial.println(8); break;
    case LTR3XX_GAIN_48: Serial.println(48); break;
    case LTR3XX_GAIN_96: Serial.println(96); break;
}

ltr.setIntegrationTime(LTR3XX_INTEGTIME_100);
Serial.print("Integration Time (ms): ");
switch (ltr.getIntegrationTime()) {
    case LTR3XX_INTEGTIME_50: Serial.println(50); break;
    case LTR3XX_INTEGTIME_100: Serial.println(100); break;
    case LTR3XX_INTEGTIME_150: Serial.println(150); break;
    case LTR3XX_INTEGTIME_200: Serial.println(200); break;
    case LTR3XX_INTEGTIME_250: Serial.println(250); break;
    case LTR3XX_INTEGTIME_300: Serial.println(300); break;
    case LTR3XX_INTEGTIME_350: Serial.println(350); break;
    case LTR3XX_INTEGTIME_400: Serial.println(400); break;
}

ltr.setMeasurementRate(LTR3XX_MEASRATE_200);
Serial.print("Measurement Rate (ms): ");
switch (ltr.getMeasurementRate()) {
    case LTR3XX_MEASRATE_50: Serial.println(50); break;
    case LTR3XX_MEASRATE_100: Serial.println(100); break;
    case LTR3XX_MEASRATE_200: Serial.println(200); break;
    case LTR3XX_MEASRATE_500: Serial.println(500); break;
    case LTR3XX_MEASRATE_1000: Serial.println(1000); break;
    case LTR3XX_MEASRATE_2000: Serial.println(2000); break;
}
}


void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid;
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}

void printMacAddress(byte mac[]) {
for (int i = 0; i &lt; 6; i++) {
    if (i &gt; 0) {
      Serial.print(":");
    }
    if (mac &lt; 16) {
      Serial.print("0");
    }
    Serial.print(mac, HEX);
}
Serial.println();
}

void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");

Serial.println(ip);
// print your MAC address:
byte mac;
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}


void loop() {
bool valid;
uint16_t visible_plus_ir, infrared;
if (ltr.newDataAvailable()) {
    valid = ltr.readBothChannels(visible_plus_ir, infrared);
    if (valid) {
      dataObj["brightness"] = visible_plus_ir;
      dataObj["ir_data"] = infrared;
      Serial.print("brightness: ");
      Serial.print(infrared);
      Serial.print("\n");
      Serial.print("ir_data: ");
      Serial.print(visible_plus_ir);
      Serial.print("\n");


      String jsonString = JSON.stringify(dataObj);
      mqttClient.beginMessage(command_topic);
      mqttClient.print(jsonString);
      mqttClient.endMessage();
    }
}

delay(500);
}
</code></pre>

<p>而配置HA实体的,有些版本不一样,我是如下们可以作为参考</p>

<pre>
<code>sensor:
- platform: mqtt
    name: "光传感器IR"
    value_template: "{{ value_json.ir_data }}"
    state_topic: "LTR303"
    unit_of_measurement: "LTR303"
    unique_id: "LTR303_IR"
- platform: mqtt
    name: "光传感器brightness"
    value_template: "{{ value_json.brightness }}"
    state_topic: "LTR303"
    unit_of_measurement: "LTR303"
    unique_id: "LTR303_brightness"</code></pre>

<p>效果</p>

<p> &nbsp;</p>

<p><strong>拓展任务一:扩展任务二&nbsp; 通过外部SHT40温湿度传感器,上传温湿度到HA</strong></p>

<p> &nbsp;</p>

<p><strong>流程图如下:</strong></p>

<p> &nbsp;</p>

<p>这个流程就和弄光传感器的一样,只是换成了温湿度传感器和HA实体配置中MQTT主题</p>

<p>HA实体</p>

<pre>
<code>sensor:
- platform: mqtt
    name: "光传感器IR"
    value_template: "{{ value_json.ir_data }}"
    state_topic: "LTR303"
    unit_of_measurement: "LTR303"
    unique_id: "LTR303_IR"
- platform: mqtt
    name: "光传感器brightness"
    value_template: "{{ value_json.brightness }}"
    state_topic: "LTR303"
    unit_of_measurement: "LTR303"
    unique_id: "LTR303_brightness"
- platform: mqtt
    name: "温湿度传感器-温度"
    value_template: "{{ value_json.temperature }}"
    state_topic: "SHT4"
    unit_of_measurement: "°C"
    unique_id: "SHT4_temperature"
- platform: mqtt
    name: "温湿度传感器-湿度"
    value_template: "{{ value_json.humidity }}"
    state_topic: "SHT4"
    unit_of_measurement: "%"
    unique_id: "SHT4_humidity" </code></pre>

<p>代码如下</p>

<pre>
<code class="language-cpp">#include "Adafruit_SHT4x.h"
#include &lt;ArduinoMqttClient.h&gt;
#include &lt;WiFiS3.h&gt;
#include &lt;WiFiClient.h&gt;
#include &lt;Arduino_JSON.h&gt;

Adafruit_SHT4x sht4;

char ssid[] = "ssid";      
char pass[] = "pass";
int status = WL_IDLE_STATUS;   

const char broker[] = "ip";
int      port   = 1883;
const char command_topic[]= "SHT4";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

JSONVar dataObj;

void setup() {
Serial.begin(115200);
    sht4.setPrecision(SHT4X_HIGH_PRECISION);


switch (sht4.getPrecision()) {
   case SHT4X_HIGH_PRECISION:
       Serial.println(F("SHT40 set to High precision"));
       break;
   case SHT4X_MED_PRECISION:
       Serial.println(F("SHT40 set to Medium precision"));
       break;
   case SHT4X_LOW_PRECISION:
       Serial.println(F("SHT40 set to Low precision"));
       break;
}   
//*********************************************************************
//*************ADVANCED SETUP - SAFE TO IGNORE!************************      

// The SHT40 has a built-in heater, which can be used for self-decontamination.
// The heater can be used for periodic creep compensation in prolongued high humidity exposure.
// For normal operation, leave the heater turned off.

sht4.setHeater(SHT4X_NO_HEATER);


switch (sht4.getHeater()) {
   case SHT4X_NO_HEATER:
       Serial.println(F("SHT40 Heater turned OFF"));
       break;
   case SHT4X_HIGH_HEATER_1S:
       Serial.println(F("SHT40 Heater: High heat for 1 second"));
       break;
   case SHT4X_HIGH_HEATER_100MS:
       Serial.println(F("SHT40 Heater: High heat for 0.1 second"));
       break;
   case SHT4X_MED_HEATER_1S:
       Serial.println(F("SHT40 Heater: Medium heat for 1 second"));
       break;
   case SHT4X_MED_HEATER_100MS:
       Serial.println(F("SHT40 Heater: Medium heat for 0.1 second"));
       break;
   case SHT4X_LOW_HEATER_1S:
       Serial.println(F("SHT40 Heater: Low heat for 1 second"));
       break;
   case SHT4X_LOW_HEATER_100MS:
       Serial.println(F("SHT40 Heater: Low heat for 0.1 second"));
       break;
}


//*********************************************************************
//*************ADVANCED SETUP IS OVER - LET'S CHECK THE CHIP ID!*******


if (! sht4.begin(&amp;Wire1)) {
    Serial.println(F("SHT40 sensor not found!"));
    while (1) ;
}
   else
{
    Serial.print(F("SHT40 detected!\t"));
    Serial.print(F("Serial number:\t"));
    Serial.println(sht4.readSerial(), HEX);   
}
Serial.println(F("----------------------------------"));


String fv = WiFi.firmwareVersion();
if (fv &lt; WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
}

Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
mqttClient.setUsernamePassword("","");
if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
    while (1);
}
Serial.println("You are connected to MQTT");
if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
}



}


void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid;
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}

void printMacAddress(byte mac[]) {
for (int i = 0; i &lt; 6; i++) {
    if (i &gt; 0) {
      Serial.print(":");
    }
    if (mac &lt; 16) {
      Serial.print("0");
    }
    Serial.print(mac, HEX);
}
Serial.println();
}

void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");

Serial.println(ip);
// print your MAC address:
byte mac;
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}



void loop() {

sensors_event_t humidity, temp;
sht4.getEvent(&amp;humidity, &amp;temp);// populate temp and humidity objects with fresh data

float t = temp.temperature;
Serial.println("Temp *C = " + String(t));
float h = humidity.relative_humidity;
Serial.println("Hum. % = " + String(h));

dataObj["temperature"] = t;
dataObj["humidity"] = h;
String jsonString = JSON.stringify(dataObj);
mqttClient.beginMessage(command_topic);
mqttClient.print(jsonString);
mqttClient.endMessage();
delay(500);
}
</code></pre>

<p>任务效果:</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>代码下载:<a href="https://download.eeworld.com.cn/detail/LTLyaoni/634603" target="_blank">https://</a><a href="https://download.eeworld.com.cn/detail/LTLyaoni/634885">download.eeworld.com.cn/detail/LTLyaoni/634885</a></p>

<p>&nbsp;</p>

<p><span style="font-size:18px;"><strong>心得体会:</strong></span></p>

<p>这次活动我主要是抱着学习的心态去的。不得不说,Arduino的那些固件真的超级丰富,特别适合用来验证各种功能。我得说,论坛上那些大佬们和网友们的分享真的是太精彩了,给了我好多灵感和帮助。真的很感谢他们!</p>

<p>希望能有更多不同使用R4板子项目的帖子,可以加深学习Arduino</p>

chejm 发表于 2024-10-23 23:20

<p>祝贺楼主获得活动机会,希望楼主能分享活动心得及技术体验</p>

秦天qintian0303 发表于 2024-10-25 13:30

<p>LTR-329光照传感器、SHT40温湿度传感器,直接来两个传感器很明智</p>
页: [1]
查看完整版本: 【Follow me第二季第2期】+ 作品提交整合贴