【Follow me第二季第2期】Arduino 基本任务 和 基于ArduinoHA SHT40数据上传HA
<article data-content="[{"type":"block","id":"3060-1621846615933","name":"heading","data":{"level":"h1","style":{}},"nodes":[{"type":"text","id":"p5PQ-1621846617594","leaves":[{"text":"1.Arduino UNO R4 WiFi 分析与介绍","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"Erkg-1728191988502","name":"paragraph","data":{},"nodes":[{"type":"text","id":"eSUJ-1728191988500","leaves":[{"text":"UNO R4 WiFi采用了强大且非常稳健的瑞萨微控制器,该控制器也用于UNO R4 Minima上。瑞萨的微控制器以其高性能和稳健性而闻名,包括其内置的外设。这些外设包括模数转换器、定时器、脉冲宽度调制(PWM)单元、通信接口(例如UART、SPI和I2C)等。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"qOz3-1727358043170","name":"image","data":{"version":1,"url":"https://note.youdao.com/yws/res/9822/WEBRESOURCE9b95444b4852f3c4416e0e45e5aed80e","width":1850,"height":999,"fromIdentity":""},"nodes":[],"state":{}},{"type":"block","id":"1MOz-1724829107779","name":"heading","data":{"version":1,"level":"h3","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"W5Qi-1724829107777","leaves":[{"text":"一、核心规格","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"cCf9-1724829109395","name":"list-item","data":{"version":1,"listId":"TAj9-1724829109412","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"CAyS-1724829109394","leaves":[{"text":"处理器:采用Renesas RA4M1(Arm Cortex®-M4)微控制器,运行速度为48MHz,相比UNO R3快3倍。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":1}},{"type":"block","id":"OBU1-1724829109397","name":"list-item","data":{"version":1,"listId":"TAj9-1724829109412","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"lcXk-1724829109396","leaves":[{"text":"存储:SRAM从R3的2kB增加到32kB,闪存从32kB增加到256kB,以适应更复杂的项目需求。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":2}},{"type":"block","id":"pnzf-1724829109399","name":"list-item","data":{"version":1,"listId":"TAj9-1724829109412","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"Ps9V-1724829109398","leaves":[{"text":"工作电压:标准5V,但输入电压支持6V至24V,提供了更大的灵活性。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":3}},{"type":"block","id":"auiI-1724829109401","name":"list-item","data":{"version":1,"listId":"TAj9-1724829109412","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"Ix8D-1724829109400","leaves":[{"text":"编程端口:USB-C接口,方便现代设备的连接。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":4}},{"type":"block","id":"GQ7P-1724829109415","name":"heading","data":{"version":1,"level":"h3","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"qUHE-1724829109402","leaves":[{"text":"二、特色功能","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"fRQZ-1724829109405","name":"list-item","data":{"version":1,"listId":"Rts1-1724829109413","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"WUpW-1724829109404","leaves":[{"text":"WiFi与蓝牙:内置Espressif S3 WiFi模块,支持WiFi和蓝牙连接,便于开发者创建物联网项目。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":1}},{"type":"block","id":"Qw0E-1724829109407","name":"list-item","data":{"version":1,"listId":"Rts1-1724829109413","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"TpQZ-1724829109406","leaves":[{"text":"LED矩阵:板载一个12x8的红色LED矩阵(总共96个点),非常适合用于创意项目中的动画或数据可视化。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":2}},{"type":"block","id":"Iv2v-1724829109409","name":"list-item","data":{"version":1,"listId":"Rts1-1724829109413","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"Wrvy-1724829109408","leaves":[{"text":"Qwiic连接器:提供一个行业标准的Qwiic I2C连接器,便于快速连接各种兼容模块,扩展开发板的功能。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":3}},{"type":"block","id":"G5xJ-1724829109411","name":"list-item","data":{"version":1,"listId":"Rts1-1724829109413","listLevel":1,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"P2ij-1724829109410","leaves":[{"text":"新外设:包括12位模拟DAC、CAN总线、运算放大器和SWD端口等,增强了开发板的功能和灵活性。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":4}},{"type":"block","id":"8HEA-1724823370609","name":"paragraph","data":{},"nodes":[{"type":"text","id":"WtlA-1724823370606","leaves":[{"text":"硬件探索","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"R52w-1724823378515","name":"paragraph","data":{},"nodes":[{"type":"text","id":"DGam-1724823378514","leaves":[{"text":" 供电电路","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"HPax-1724824522349","name":"paragraph","data":{},"nodes":[{"type":"text","id":"a2Ji-1724824522347","leaves":[{"text":"主要有3种供电方式","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"0JDS-1724824455232","name":"list-item","data":{"listId":"gGqv-1724824600376","listType":"unordered","listLevel":1},"nodes":[{"type":"text","id":"CsM9-1724824455230","leaves":[{"text":" ","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]},{"text":"开发板可以通过VIN引脚供电,支持6-24V的范围。","marks":[{"type":"color","value":"#37373a"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":1}},{"type":"block","id":"gVGl-1724824549328","name":"list-item","data":{"listId":"gGqv-1724824600376","listType":"unordered","listLevel":1},"nodes":[{"type":"text","id":"rOfy-1724824549326","leaves":[{"text":" VIN引脚也连接到直流插孔(圆柱插头连接器)。","marks":[{"type":"color","value":"#37373a"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]},{"text":" ","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{"index":2}},{"type":"block","id":"nMvy-1724824553379","name":"list-item","data":{"listId":"gGqv-1724824600376","listType":"unordered","listLevel":1},"nodes":[{"type":"text","id":"hitd-1724824553377","leaves":[{"text":" USB C口供电。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}}]"><p><span style="font-size:24px;"><strong>项目演示视频</strong></span></p>
<p><a href="https://training.eeworld.com.cn/video/41252" target="_blank">【Follow me第二季第2期】项目演示视频</a></p>
<p> </p>
<p><span style="font-size:24px;">物料展示</span></p>
<p> </p>
<p> </p>
<p>清单</p>
<p>Arduino UNO R4 WiFi</p>
<p>Qwiic缆线-50mm</p>
<p>SHT40温湿度传感器扩展板</p>
<p>10K电阻 * 2</p>
<p>6PIN 排针</p>
<p> </p>
<p> </p>
<p> </p>
<p><strong><span style="font-size:22px;">1.Arduino UNO R4 WiFi 分析与介绍</span></strong></p>
<p><span style="font-size:22px;"> UNO R4 WiFi采用了强大且非常稳健的瑞萨微控制器,该控制器也用于UNO R4 Minima上。瑞萨的微控制器以其高性能和稳健性而闻名,包括其内置的外设。这些外设包括模数转换器、定时器、脉冲宽度调制(PWM)单元、通信接口(例如UART、SPI和I2C)等。</span></p>
<p><span style="font-size:22px;"> </span></p>
<p><strong><span style="font-size:22px;">1.1、核心规格</span></strong></p>
<ul yne-block-type="list">
<li><span style="font-size:22px;">处理器:采用Renesas RA4M1(Arm Cortex®-M4)微控制器,运行速度为48MHz,相比UNO R3快3倍。</span></li>
<li><span style="font-size:22px;">存储:SRAM从R3的2kB增加到32kB,闪存从32kB增加到256kB,以适应更复杂的项目需求。</span></li>
<li><span style="font-size:22px;">工作电压:标准5V,但输入电压支持6V至24V,提供了更大的灵活性。</span></li>
<li><span style="font-size:22px;">编程端口:USB-C接口,方便现代设备的连接。</span></li>
</ul>
<p><strong><span style="font-size:22px;">1.2、特色功能</span></strong></p>
<ul yne-block-type="list">
<li><span style="font-size:22px;">WiFi与蓝牙:内置Espressif S3 WiFi模块,支持WiFi和蓝牙连接,便于开发者创建物联网项目。</span></li>
<li><span style="font-size:22px;">LED矩阵:板载一个12x8的红色LED矩阵(总共96个点),非常适合用于创意项目中的动画或数据可视化。</span></li>
<li><span style="font-size:22px;">Qwiic连接器:提供一个行业标准的Qwiic I2C连接器,便于快速连接各种兼容模块,扩展开发板的功能。</span></li>
<li><span style="font-size:22px;">新外设:包括12位模拟DAC、CAN总线、运算放大器和SWD端口等,增强了开发板的功能和灵活性。</span></li>
</ul>
<p><strong><span style="font-size:22px;">1.3硬件探索</span></strong></p>
<p><span style="font-size:22px;">供电电路</span></p>
<p><span style="font-size:22px;">主要有3种供电方式</span></p>
<ul yne-block-type="list">
<li><span style="font-size:22px;">开发板可以通过VIN引脚供电,支持6-24V的范围。</span></li>
<li><span style="font-size:22px;">VIN引脚也连接到直流插孔(圆柱插头连接器)。</span></li>
<li><span style="font-size:22px;">USB C口供电。</span></li>
</ul>
<p> </p>
<article data-content="[{"type":"block","id":"dSYN-1724824459428","name":"paragraph","data":{},"nodes":[{"type":"text","id":"6aez-1724824459426","leaves":[{"text":"使用ISL8541102 降压型DC-DC芯片 将VIN的电压转换成5V电源,ISL8541102是瑞萨的一个宽输入,能提供1.2A电流能力的同步整流的降压芯片,封装很小,使用圣邦微SGM2205(线性稳压器)提供3.3V电源","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}}]">
<p>使用ISL8541102 降压型DC-DC芯片 将VIN的电压转换成5V电源,ISL8541102是瑞萨的一个宽输入,能提供1.2A电流能力的同步整流的降压芯片,封装很小,使用圣邦微SGM2205(线性稳压器)提供3.3V电源</p>
<p> </p>
<article data-content="[{"type":"block","id":"Sakj-1724824699390","name":"paragraph","data":{},"nodes":[{"type":"text","id":"0Pue-1724824699389","leaves":[{"text":"USB CONNECTOR 电路","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}}]">
<p> USB CONNECTOR 电路</p>
<p> </p>
</article>
</article>
<p> </p>
<article data-content="[{"type":"block","id":"TYbZ-1724824700823","name":"paragraph","data":{},"nodes":[{"type":"text","id":"zrej-1724824700822","leaves":[{"text":"NLASB3157 是一个模拟开关用于切换USB C 是跟ESP32S3连接,还是R7FA4M1AB3CFM。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"7hCR-1724831879532","name":"paragraph","data":{},"nodes":[{"type":"text","id":"Pk36-1724831879531","leaves":[{"text":" 综合来看这个板子,应该是用ESP32给瑞萨MCU下载的。","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}}]">
<p><span style="font-size:20px;">NLASB3157 是一个模拟开关用于切换USB C 是跟ESP32S3连接,还是R7FA4M1AB3CFM。</span></p>
<p><span style="font-size:20px;">综合来看这个板子,应该是用ESP32给瑞萨MCU下载的。</span></p>
</article>
<p> </p>
<article data-content="[{"type":"block","id":"uGoU-1724831958961","name":"heading","data":{"level":"h1"},"nodes":[{"type":"text","id":"8s2x-1724831958960","leaves":[{"text":"2.基本任务","marks":[{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"2CUp-1728192908675","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"NsXP-1728192908674","leaves":[{"text":"2.1","marks":[{"type":"fontFamily","value":"SimSun"}]},{"text":"入门任务(必做):","marks":[{"type":"bold"},{"type":"color","value":"#424242"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"}]},{"text":"搭建环境并开启第一步Blink / 串口打印Hello EEWorld!","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"}]}]}],"state":{}},{"type":"block","id":"C5z0-1728193550200","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"rRqo-1728193550201","leaves":[{"text":"2.1.1 ","marks":[{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"Blink ","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"LED闪灯","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><span style="font-size:20px;"><strong>2.基本任务</strong></span></p>
<p><span style="font-size:20px;"><strong>2.1入门任务(必做):搭建环境并开启第一步Blink / 串口打印Hello EEWorld!</strong></span></p>
<p><span style="font-size:20px;"><strong>2.1.1 Blink LED闪灯</strong></span></p>
</article>
<p><span style="font-size:20px;">使用物料及外设:GPIO,一个板载LED</span></p>
<p> </p>
<p> </p>
<p> </p>
<p> LED 使用的是Arduino 13号引脚。</p>
<p> </p>
<pre>
<code>
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);// turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}</code></pre>
<p> </p>
<p> </p>
<article data-content="[{"type":"block","id":"HXQH-1724985535673","name":"heading","data":{"version":1,"level":"h3"},"nodes":[{"type":"text","id":"XuDG-1724985535674","leaves":[{"text":"2.1.2 ","marks":[{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"串口打印Hello EEWorld!","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><strong>2.1.2 串口打印Hello EEWorld!</strong></p>
<p>使用物料及外设:GPIO,一个板载LED</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code>void setup()
{
Serial.begin(9600);//设置波特率
Serial.println("Hello EEWorld!\r\n");//提示字符
}
void loop()
{
delay(5000);
Serial.println("Hello EEWorld!\r\n");
}</code></pre>
<p> </p>
</article>
<p> </p>
<article data-content="[{"type":"block","id":"ICNT-1728193122709","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"YP2O-1728193122708","leaves":[{"text":"2.2 ","marks":[]},{"text":"基础任务(必做):","marks":[{"type":"bold"},{"type":"color","value":"#424242"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"}]},{"text":"驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据曲线","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"}]}]}],"state":{}},{"type":"block","id":"dxcM-1728194195227","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"Zifw-1728194195228","leaves":[{"text":"2.2.1","marks":[{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"驱动12x8点阵LED","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><span style="font-size:20px;"><strong>2.2 基础任务(必做):驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据曲线</strong></span></p>
<p><span style="font-size:20px;"><strong>2.2.1驱动12x8点阵LED</strong></span></p>
</article>
<p> </p>
<p><span style="font-size:20px;">使用物料及外设:使用板载LED矩阵</span></p>
<p> </p>
<p> </p>
<p> </p>
<article data-content="[{"type":"block","id":"q4AC-1728194203086","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"PhUB-1728194203084","leaves":[{"text":"UNO R4 WiFi 板载了一个12*8的红色LED矩阵,","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]},{"text":"可以通过编程来控制这个矩阵以显示不同的图案、文字或动画。","marks":[{"type":"color","value":"#05073b"},{"type":"backgroundColor","value":"rgb(253, 253, 254)"},{"type":"fontFamily","value":"Arial"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"Vseb-1724831531494","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"jSEb-1724831531492","leaves":[{"text":" 该矩阵使用Charlieplexing将 96 个 LED 连接到仅 11 个 GPIO 端口(Arduino 表示法中的 D28 到 D38)。这种方式使用了很少的gpio端口就点亮了很多的LED灯, 因为像素由两个反并联连接的 LED 组成,并且像素共享端口。然而,由于人眼速度较慢,快速时间复用可以欺骗大脑看到完整的图像。","marks":[{"type":"fontSize","value":20},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"T3Ru-1725006635562","name":"paragraph","data":{"version":1,"style":{"textAlign":"left"}},"nodes":[{"type":"text","id":"grn1-1725006635561","leaves":[{"text":"Arduino官方开发了一种工具,可以在浏览器中的 LED 矩阵上渲染的帧和动画。制作完动画之后,可以从工具中导出LED点阵数据。","marks":[{"type":"fontSize","value":18},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"7rmE-1725008082629","name":"paragraph","data":{"version":1,"style":{"textAlign":"left"}},"nodes":[{"type":"inline","id":"QLoF-1725008082621","name":"link","data":{"href":"https://ledmatrix-editor.arduino.cc/"},"nodes":[{"type":"text","id":"d6WB-1725008082627","leaves":[{"text":"单击此处","marks":[{"type":"fontSize","value":18},{"type":"fontFamily","value":"SimSun"}]}]}]},{"type":"text","id":"j1Dy-1725008082628","leaves":[{"text":"转到 LED 矩阵工具。","marks":[{"type":"fontSize","value":18},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"EpTi-1725008082631","name":"paragraph","data":{"version":1,"style":{"textAlign":"left"}},"nodes":[{"type":"text","id":"nPWL-1725008082630","leaves":[{"text":"制作完动画之后,你可以按照前面讨论过的格式,从工具中导出它们。","marks":[{"type":"fontSize","value":18},{"type":"fontFamily","value":"SimSun"}]}]}],"state":{}},{"type":"block","id":"B9rJ-1724831531590","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"cwNu-1724831531588","leaves":[{"text":"这里可以导出.h 文件","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":18}]}]}],"state":{}},{"type":"block","id":"dQFe-1728194501070","name":"image","data":{"version":1,"fromIdentity":"","url":"https://note.youdao.com/yws/res/9910/WEBRESOURCE0e69e338d94c6f5fd44ee1dddd91b1f0","width":1422,"height":636},"nodes":[],"state":{"renderSource":"https://note.youdao.com/yws/res/9910/WEBRESOURCE0e69e338d94c6f5fd44ee1dddd91b1f0","initialSize":{"width":1422,"height":636},"loading":false}},{"type":"block","id":"nvcq-1724831531787","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"Ah2S-1724831531785","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}},{"type":"block","id":"6xGx-1724831531884","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"qOjt-1724831531883","leaves":[{"text":"","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"fontSize","value":20}]}]}],"state":{}}]">
<p><span style="font-size:20px;"> UNO R4 WiFi 板载了一个12*8的红色LED矩阵,可以通过编程来控制这个矩阵以显示不同的图案、文字或动画。</span></p>
<p><span style="font-size:20px;"> 该矩阵使用Charlieplexing将 96 个 LED 连接到仅 11 个 GPIO 端口(Arduino 表示法中的 D28 到 D38)。这种方式使用了很少的gpio端口就点亮了很多的LED灯, 因为像素由两个反并联连接的 LED 组成,并且像素共享端口。然而,由于人眼速度较慢,快速时间复用可以欺骗大脑看到完整的图像。</span></p>
<p><span style="font-size:20px;">Arduino官方开发了一种工具,可以在浏览器中的 LED 矩阵上渲染的帧和动画。制作完动画之后,可以从工具中导出LED点阵数据。</span></p>
<p><span style="font-size:20px;"><a href="https://ledmatrix-editor.arduino.cc/">单击此处</a>转到 LED 矩阵工具。</span></p>
<p><span style="font-size:20px;">制作完动画之后,你可以按照前面讨论过的格式,从工具中导出它们。</span></p>
<p><span style="font-size:20px;">这里可以导出.h 文件</span></p>
<p style="text-align: center;"> </p>
</article>
<p style="text-align: center;"> </p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code>#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
byte frame = {
{ 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
uint32_t animation[] = {
0x400e015,
0x400400,
0x40040000
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
matrix.begin();
matrix.renderBitmap(frame, 8, 12);
delay(1000);
delay(1000);
frame = 1;
matrix.renderBitmap(frame, 8, 12);
delay(1000);
delay(1000);
matrix.loadFrame(animation);
}
void loop() {
// put your main code here, to run repeatedly:
}</code></pre>
<p> </p>
<article data-content="[{"type":"block","id":"PGEn-1728194203933","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"4N6q-1728194203932","leaves":[{"text":"2.2.2","marks":[{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"用DAC生成正弦波","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><span style="font-size:20px;"><strong>2.2.2用DAC生成正弦波</strong></span></p>
<p> </p>
<p><span style="font-size:20px;">使用物料及外设:使用板载ADC DAC</span></p>
<p> </p>
</article>
<p> </p>
<p> </p>
<p> </p>
<p><span style="font-size:20px;">Arduino R4可以直接使用wave生成正弦波</span></p>
<p> </p>
<pre>
<code>void setup() {
Serial.begin(500000);//设置波特率
Serial.println("Hello EEWorld!\r\n");//提示字符
wave.sine(freq);
wave.amplitude(0.5);
}
void loop() {
sensorValue = analogRead(A4);
Serial.println(sensorValue);
}</code></pre>
<p> </p>
<p> </p>
<article data-content="[{"type":"block","id":"r1gk-1728194210313","name":"heading","data":{"level":"h3"},"nodes":[{"type":"text","id":"mIm4-1728194210312","leaves":[{"text":"2.2.3","marks":[{"type":"bold"},{"type":"fontSize","value":20}]},{"text":"用OPAMP放大DAC信号","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":20}]}]}]}]">
<p><span style="font-size:20px;"><strong>2.2.3用OPAMP放大DAC信号</strong></span></p>
</article>
<p> </p>
<p><span style="font-size:20px;">使用物料及外设:2个10K电阻,板载放大器,ADC</span></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code>#include "analogWave.h"
#include <OPAMP.h>
analogWave wave(DAC); // 使用DAC引脚实例化模拟曲线对象wave
float freq = 1;// 设置曲线初始频率
void setup() {
Serial.begin(2000000);// 串口波特率
OPAMP.begin(OPAMP_SPEED_HIGHSPEED); //设置OPAMP
wave.sine(freq); // 使用模拟曲线对象wave按照初始频率生成正弦波
wave.amplitude(0.5);//设置正弦曲线幅值为0.4
}
void loop() {
printf("%d\n",analogRead(A4)); // 读取DAC输出正弦值
Serial.print(" ");
printf("%d\n",analogRead(A5)); // 读取OPAMP输出正弦值
delay(100);
}</code></pre>
<p> </p>
<article data-content="[{"type":"block","id":"xfjJ-1728193219652","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"Sg2C-1728193219651","leaves":[{"text":"2.3 ","marks":[{"type":"bold"},{"type":"fontSize","value":22}]},{"text":"进阶任务(必做):","marks":[{"type":"bold"},{"type":"color","value":"#424242"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":22}]},{"text":"通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)","marks":[{"type":"color","value":"#333333"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"bold"},{"type":"fontSize","value":22}]}]}]}]">
<p><span style="font-size:20px;"><strong>2.3 进阶任务(必做):通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant)</strong></span></p>
</article>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<pre>
<code>#include <ArduinoHA.h>
#include <Wire.h>
#include "WiFiS3.h"
#include "arduino_secrets.h"
int status= WL_IDLE_STATUS;
unsigned long lastUpdateAt = 0;
int freg=1;//max200 Hz imit by serial print
WiFiClient client;
HADevice device(MQTT_CLIENT_ID);//HADevice device (mac, sizeof (mac));
HAMqtt mqtt(client,device);
void setup() {
Serial.begin(9600);
wifi_and_mqtt_init();
}
void loop() {
mqtt.loop();
if ((millis() - lastUpdateAt) > 2000) { // update in 2s interval
lastUpdateAt = millis();
mqtt.publish(TOPIC_SUBSCRIBE,"Hi HA I'm Arduino UNO R4 WII");
}
}
void wifi_and_mqtt_init()
{
//check for the WiFi module:
////检查WiFi模块:
if(WiFi.status() == WL_NO_MODULE){
//WiFi模块通信失败
Serial.println("Communication with WiFi module failed!");
while(true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION){
Serial.println("Please upqrade the firmware");
}
//attempt to connect to WiFi network:
while(status!=WL_CONNECTED){
Serial.print("Attempting to connect to sID:");
Serial.println(SECRET_SSID);
//Connect to WPA/WPA2 network, change this line if using open or WEp network:
status =WiFi.begin(SECRET_SSID,SECRET_PASS);
delay(10000);
}
// mqtt.setDataPrefix("UNO");
if(!mqtt.begin(MQTT_SERVER,MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD)){
Serial.print("Failed,rc=");
Serial.print(mqtt.getState());
Serial.println("try again in f 5 seconds");
delay(5000);
}
printWifistatus();
}
void printWifistatus()
{
Serial.print("SSID:");
Serial.println(WiFi.SSID());
IPAddress ip= WiFi.localIP();
Serial.print("IP Address:");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength(RssI):");
Serial.print(rssi);
Serial.println("dBm");
}</code></pre>
<p> </p>
<article data-content="[]"> </article>
<article data-content="[{"type":"block","id":"yaki-1728193297871","name":"heading","data":{"level":"h1"},"nodes":[{"type":"text","id":"FIb7-1728193297870","leaves":[{"text":"3.扩展任务","marks":[{"type":"bold"},{"type":"fontSize","value":26}]}]}]}]">
<article data-content="[{"type":"block","id":"yaki-1728193297871","name":"heading","data":{"level":"h1"},"nodes":[{"type":"text","id":"FIb7-1728193297870","leaves":[{"text":"3.扩展任务","marks":[{"type":"bold"},{"type":"fontSize","value":26}]}]}]}]">
<p><span style="font-size:24px;"><strong>3.扩展任务</strong></span></p>
</article>
<p> </p>
<article data-content="[{"type":"block","id":"WOMm-1724985540903","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"X7aG-1724985540901","leaves":[{"text":"3.1 SHT40传感器","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"bold"},{"type":"fontSize","value":22}]}]}]}]">
<p><span style="font-size:20px;"><strong>3.1 SHT40传感器</strong></span></p>
</article>
</article>
<article data-content="[{"type":"block","id":"WOMm-1724985540903","name":"heading","data":{"level":"h2"},"nodes":[{"type":"text","id":"X7aG-1724985540901","leaves":[{"text":"3.1 SHT40传感器","marks":[{"type":"fontFamily","value":"SimSun"},{"type":"bold"},{"type":"fontSize","value":22}]}]}]}]">
<p style="text-align: center;"> </p>
</article>
<article data-content="[{"type":"block","id":"Fe5h-1727945607085","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"dG3F-1727945607084","leaves":[{"text":"SHT40传感器可以连接到","marks":[{"type":"color","value":"#05073b"},{"type":"backgroundColor","value":"rgb(253, 253, 254)"},{"type":"fontSize","value":15},{"type":"fontFamily","value":"Arial"}]},{"text":"Arduino UNO R4 WiFi ","marks":[{"type":"fontSize","value":15}]},{"text":"Qwiic接口上,但需要注意几个关键点,以确保硬件的安全和正确的通信。","marks":[{"type":"color","value":"#05073b"},{"type":"backgroundColor","value":"rgb(253, 253, 254)"},{"type":"fontSize","value":15},{"type":"fontFamily","value":"Arial"}]}]}],"state":{}},{"type":"block","id":"ZFyY-1727946239768","name":"list-item","data":{"version":1,"listId":"qoz8-1728208019070","listLevel":1,"listType":"ordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"BEvN-1727946239766","leaves":[{"text":"电压兼容性:","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"QkEq-1727946240764","name":"list-item","data":{"version":1,"listId":"yVse-1728208019071","listLevel":2,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"df1D-1727946240763","leaves":[{"text":"Qwiic接口仅支持3.3V供电。这意味着SHT40传感器和其他通过Qwiic连接器连接的设备都应设计为在3.3V下工作。","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"UqgY-1727946240766","name":"list-item","data":{"version":1,"listId":"yVse-1728208019071","listLevel":2,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"hxRq-1727946240765","leaves":[{"text":"切勿尝试将5V或其他高于3.3V的电压连接到Qwiic接口,因为这可能会损坏您的Arduino R4板或其他连接的组件。","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"VRWr-1727946240768","name":"list-item","data":{"version":1,"listId":"qoz8-1728208019070","listLevel":1,"listType":"ordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"PzEY-1727946240767","leaves":[{"text":"I2C总线选择:","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"Fre6-1727946240770","name":"list-item","data":{"version":1,"listId":"D3Lu-1728208019072","listLevel":2,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"e6uX-1727946240769","leaves":[{"text":"Arduino R4可能具有多个I2C总线。由于Qwiic连接器连接到次级I2C总线(通常标记为IIC0或Wire1),您需要在代码中指定使用","marks":[]},{"text":"Wire1","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"库来初始化这个特定的I2C总线。","marks":[]}]}],"state":{"index":1}},{"type":"block","id":"UK95-1727946240772","name":"list-item","data":{"version":1,"listId":"D3Lu-1728208019072","listLevel":2,"listType":"unordered","style":{"lineHeight":"1.7"}},"nodes":[{"type":"text","id":"fFL2-1727946240771","leaves":[{"text":"使用","marks":[]},{"text":"Wire1.begin()","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"而不是默认的","marks":[]},{"text":"Wire.begin()","marks":[{"type":"backgroundColor","value":"rgba(27, 31, 35, 0.05)"},{"type":"fontSize","value":12},{"type":"fontFamily","value":"Courier New"}]},{"text":"来初始化I2C通信。","marks":[]}]}],"state":{}}]">
<p><span style="font-size:20px;">SHT40传感器可以连接到Arduino UNO R4 WiFi Qwiic接口上,但需要注意几个关键点,以确保硬件的安全和正确的通信。</span></p>
<ol yne-block-type="list">
<li><span style="font-size:20px;">电压兼容性:</span></li>
</ol>
<ul yne-block-type="list">
<li>
<ul yne-block-type="list">
<li><span style="font-size:20px;">Qwiic接口仅支持3.3V供电。这意味着SHT40传感器和其他通过Qwiic连接器连接的设备都应设计为在3.3V下工作。</span></li>
<li><span style="font-size:20px;">切勿尝试将5V或其他高于3.3V的电压连接到Qwiic接口,因为这可能会损坏您的Arduino R4板或其他连接的组件。</span></li>
</ul>
</li>
</ul>
<ol start="2" yne-block-type="list">
<li><span style="font-size:20px;">I2C总线选择:</span></li>
</ol>
<ul yne-block-type="list">
<li>
<ul yne-block-type="list">
<li><span style="font-size:20px;">Arduino R4可能具有多个I2C总线。由于Qwiic连接器连接到次级I2C总线(通常标记为IIC0或Wire1),您需要在代码中指定使用Wire1库来初始化这个特定的I2C总线。</span></li>
<li><span style="font-size:20px;">使用Wire1.begin()而不是默认的Wire.begin()来初始化I2C通信。</span></li>
</ul>
</li>
</ul>
</article>
<article data-content="[{"type":"block","id":"Oz3b-1727945608589","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"bQsd-1727945608587","leaves":[{"text":"Wier1在Wier.h 的声明","marks":[]}]}],"state":{}}]">
<p><span style="font-size:20px;">Wier1在Wier.h 的声明</span></p>
</article>
<p> </p>
<pre>
<code>#if WIRE_HOWMANY > 0
extern TwoWire Wire;
#endif
#if WIRE_HOWMANY > 1
extern TwoWire Wire1;
#endif
#if WIRE_HOWMANY > 2
extern TwoWire Wire2;
#endif
#if WIRE_HOWMANY > 3
extern TwoWire Wire3;
#endif</code></pre>
<article data-content="[{"type":"block","id":"qPFs-1727945742785","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"v9DD-1727945742783","leaves":[{"text":"在pins_arduino.h 的引脚定义","marks":[]}]}],"state":{}}]">
<p>在pins_arduino.h 的引脚定义</p>
</article>
<pre>
<code>#define WIRE_HOWMANY 2
#define WIRE_SDA_PIN 18 /* A4 */
#define WIRE_SCL_PIN 19 /* A5 */
#define WIRE1_SDA_PIN 27
#define WIRE1_SCL_PIN 26
static const uint8_t SDA = WIRE_SDA_PIN;
static const uint8_t SCL = WIRE_SCL_PIN;</code></pre>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<article data-content="[{"type":"block","id":"JyrU-1727954091871","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"YBbf-1727954091869","leaves":[{"text":"示例代码","marks":[{"type":"color","value":"#374146"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16}]}]}],"state":{}}]">
<p>示例代码</p>
</article>
<p> </p>
<p> </p>
<p> </p>
<article data-content="[{"type":"block","id":"bAiv-1727954757856","name":"paragraph","data":{"version":1},"nodes":[{"type":"text","id":"xgaN-1727954757854","leaves":[{"text":"22行,要改为","marks":[{"type":"color","value":"#374146"},{"type":"backgroundColor","value":"rgb(255, 255, 255)"},{"type":"fontSize","value":16}]},{"text":" if (! sht4.begin(&Wire1))","marks":[{"type":"fontSize","value":16}]}]}],"state":{}}]">
<p>22行,要改为 if (! sht4.begin(&Wire1))</p>
<p> </p>
<p> </p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"> </p>
<pre>
<code>/***************************************************
This is an example for the SHT4x Humidity & Temp Sensor
Designed specifically to work with the SHT4x sensor from Adafruit
----> https://www.adafruit.com/products/4885
These sensors use I2C to communicate, 2 pins are required to
interface
****************************************************/
#include "Adafruit_SHT4x.h"
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
void setup() {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit SHT4x test");
if (! sht4.begin(&Wire1)) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
Serial.println("Found SHT4x sensor");
Serial.print("Serial number 0x");
Serial.println(sht4.readSerial(), HEX);
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
// You can have 6 different heater settings
// higher heat and longer times uses more power
// and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
}
void loop() {
sensors_event_t humidity, temp;
uint32_t timestamp = millis();
sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
timestamp = millis() - timestamp;
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
Serial.print("Read duration (ms): ");
Serial.println(timestamp);
delay(1000);
}</code></pre>
<article data-content="[{"type":"block","id":"78ia-1728208650964","name":"paragraph","data":{},"nodes":[{"type":"text","id":"mQPI-1728208650963","leaves":[{"text":"3.2 STH40温湿度数据上传HA","marks":[]}]}],"state":{}}]">
<p>3.2 STH40温湿度数据上传HA</p>
<p style="text-align: center;"> </p>
<p style="text-align: center;"> </p>
</article>
<pre>
<code>#include <ArduinoHA.h>
#include <OPAMP.h>
#include <Wire.h>
#include "WiFiS3.h"
#include "arduino_secrets.h"
#include "Adafruit_SHT4x.h"
sensors_event_t humidity, temp;
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
int status= WL_IDLE_STATUS;
unsigned long lastUpdateAt = 0;
int freg=1;//max200 Hz imit by serial print
WiFiClient client;
HADevice device(MQTT_CLIENT_ID);//HADevice device (mac, sizeof (mac));
HAMqtt mqtt(client,device);
HASensorNumber upSHT40_C_Sensor("SHT40_C");
HASensorNumber upSHT40_RH_Sensor("SHT40_RH");
void setup() {
Serial.begin(9600);
if (! sht4.begin(&Wire1)) {
Serial.println("Couldn't find SHT4x");
while (1) delay(1);
}
// You can have 3 different precisions, higher precision takes longer
sht4.setPrecision(SHT4X_HIGH_PRECISION);
switch (sht4.getPrecision()) {
case SHT4X_HIGH_PRECISION:
Serial.println("High precision");
break;
case SHT4X_MED_PRECISION:
Serial.println("Med precision");
break;
case SHT4X_LOW_PRECISION:
Serial.println("Low precision");
break;
}
// You can have 6 different heater settings
// higher heat and longer times uses more power
// and reads will take longer too!
sht4.setHeater(SHT4X_NO_HEATER);
switch (sht4.getHeater()) {
case SHT4X_NO_HEATER:
Serial.println("No heater");
break;
case SHT4X_HIGH_HEATER_1S:
Serial.println("High heat for 1 second");
break;
case SHT4X_HIGH_HEATER_100MS:
Serial.println("High heat for 0.1 second");
break;
case SHT4X_MED_HEATER_1S:
Serial.println("Medium heat for 1 second");
break;
case SHT4X_MED_HEATER_100MS:
Serial.println("Medium heat for 0.1 second");
break;
case SHT4X_LOW_HEATER_1S:
Serial.println("Low heat for 1 second");
break;
case SHT4X_LOW_HEATER_100MS:
Serial.println("Low heat for 0.1 second");
break;
}
wifi_and_mqtt_init();
device.setName("Arduino_SHT40_MY");
device.setSoftwareVersion("1.0.1");
upSHT40_C_Sensor.setIcon("mdi:home");
upSHT40_C_Sensor.setName("SHT40_C");
//upSHT40_C_Sensor.setUnitOfMeasurement("C");
upSHT40_RH_Sensor.setIcon("mdi:home");
upSHT40_RH_Sensor.setName("SHT40_RH");
// upSHT40_RH_Sensor.setUnitOfMeasurement("RH");
}
void loop() {
mqtt.loop();
if ((millis() - lastUpdateAt) > 2000) { // update in 2s interval
sht4.getEvent(&humidity, &temp);
upSHT40_RH_Sensor.setValue(humidity.relative_humidity);
upSHT40_C_Sensor.setValue(temp.temperature);
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
lastUpdateAt = millis();
mqtt.publish(TOPIC_SUBSCRIBE,"Hi EMQX I'm Arduino UNO R4 WII");
// you can reset the sensor as follows:
// analogSensor.setValue(nullptr);
}
}
void wifi_and_mqtt_init()
{
//check for the WiFi module:
////检查WiFi模块:
if(WiFi.status() == WL_NO_MODULE){
//WiFi模块通信失败
Serial.println("Communication with WiFi module failed!");
while(true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION){
Serial.println("Please upqrade the firmware");
}
//attempt to connect to WiFi network:
while(status!=WL_CONNECTED){
Serial.print("Attempting to connect to sID:");
Serial.println(SECRET_SSID);
//Connect to WPA/WPA2 network, change this line if using open or WEp network:
status =WiFi.begin(SECRET_SSID,SECRET_PASS);
delay(10000);
}
// mqtt.setDataPrefix("UNO");
if(!mqtt.begin(MQTT_SERVER,MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD)){
Serial.print("Failed,rc=");
Serial.print(mqtt.getState());
Serial.println("try again in f 5 seconds");
delay(5000);
}
printWifistatus();
}
void printWifistatus()
{
Serial.print("SSID:");
Serial.println(WiFi.SSID());
IPAddress ip= WiFi.localIP();
Serial.print("IP Address:");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength(RssI):");
Serial.print(rssi);
Serial.println("dBm");
}
</code></pre>
<p> </p>
<p><span style="font-size:24px;"><strong>4.ArduinoHA讲解补充</strong></span></p>
<p> <span style="font-size:20px;"> ArduinoHA是一个专为Arduino和ESP系列微控制器设计的开源库,其主要功能是实现这些设备与Home Assistant智能家居平台之间的无缝通信。使用MQTT(消息队列遥测传输)协议来完成通讯,MQTT是一种轻量级的、基于发布/订阅模式的消息传输协议,非常适合用于物联网(IoT)场景。</span></p>
<p><span style="font-size:20px;"> 通过ArduinoHA库,开发者可以轻松地将Arduino或ESP设备集成到Home Assistant生态系统中,实现智能家居设备的智能化控制。这包括但不限于灯光控制、温湿度监测、门窗传感器、安防系统等。</span></p>
<p><span style="font-size:20px;"> ArduinoHA库提供了一系列API函数,用于设备的初始化、MQTT连接管理、设备状态更新以及处理来自Home Assistant的命令等。这些API函数使得开发者能够以简单、直观的方式编写代码,从而实现与Home Assistant的通信。</span></p>
<p><span style="font-size:20px;"> 此外,ArduinoHA还支持MQTT自动发现功能,这意味着当设备首次连接到MQTT服务器时,它会自动向Home Assistant发送设备信息,从而避免了手动在Home Assistant中添加设备的繁琐过程。</span></p>
<p><span style="font-size:20px;"><strong>使用方法与配置步骤:</strong></span></p>
<p> </p>
<p><span style="font-size:20px;"><strong>1.连接到Home Assistant</strong>:</span></p>
<ul>
<li><span style="font-size:20px;">连接到WiFi网络。</span></li>
<li><span style="font-size:20px;">初始化MQTT客户端并连接到MQTT服务器。 </span></li>
<li><span style="font-size:20px;">配置并添加设备到MQTT客户端。</span></li>
<li><span style="font-size:20px;">在loop函数中处理MQTT消息并更新设备状态。</span></li>
</ul>
<p><span style="font-size:20px;"><strong>关键点</strong>:</span></p>
<article data-content="[{"type":"block","id":"jnWr-1728208651785","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"7M7r-1728208651784","leaves":[{"text":"全局初始化","marks":[]}]}],"state":{}},{"type":"block","id":"RqVB-1728282669802","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"r25e-1728282669801","leaves":[{"text":":HADevice 和 HAMqtt 实例需要全局初始化,或者作为另一个全局对象的一部分进行初始化。","marks":[]}]}],"state":{"index":2}},{"type":"block","id":"bGxK-1728282669804","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"AxY7-1728282669803","leaves":[{"text":"MQTT连接设置","marks":[]}]}],"state":{"index":3}},{"type":"block","id":"1z5i-1728282669806","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"E9r5-1728282669805","leaves":[{"text":":在Arduino的setup()逻辑结束时,需要调用HAMqtt::begin()方法。这个方法允许您提供MQTT代理(Broker)的IP地址和认证信息(如用户名和密码,如果有的话)。","marks":[]}]}],"state":{"index":4}},{"type":"block","id":"hv45-1728282669808","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"mL8s-1728282669807","leaves":[{"text":"MQTT循环处理","marks":[]}]}],"state":{"index":5}},{"type":"block","id":"mN5f-1728282669810","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"eFTj-1728282669809","leaves":[{"text":":HAMqtt::loop()方法需要被周期性地调用(不必在每个循环迭代中都调用)。这个方法负责处理MQTT通信中的接收和发送任务。","marks":[]}]}],"state":{"index":6}},{"type":"block","id":"RiT4-1728282669812","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"3l5M-1728282669811","leaves":[{"text":"设备类型初始化","marks":[]}]}],"state":{"index":7}},{"type":"block","id":"bHqc-1728282669814","name":"paragraph","data":{"style":{"indent":0}},"nodes":[{"type":"text","id":"1AOZ-1728282669813","leaves":[{"text":":设备类型(如传感器、开关等)需要在HAMqtt类之后进行初始化。关于如何初始化不同类型的设备,将在后面的文档中详细描述。","marks":[]}]}],"state":{}}]">
<p><span style="font-size:20px;"> 全局初始化:</span></p>
<p><span style="font-size:20px;"> HADevice 和 HAMqtt 实例需要全局初始化,或者作为另一个全局对象的一部分进行初始化。</span></p>
<p><span style="font-size:20px;"> MQTT连接设置:</span></p>
<p><span style="font-size:20px;"> 在Arduino的setup()逻辑结束时,需要调用HAMqtt::begin()方法。这个方法允许您提供MQTT代理(Broker)的IP地址和认证信息(如用户名和密码,如果有的话)。</span></p>
<p><span style="font-size:20px;"> MQTT循环处理:</span></p>
<p><span style="font-size:20px;"> HAMqtt::loop()方法需要被周期性地调用(不必在每个循环迭代中都调用)。这个方法负责处理MQTT通信中的接收和发送任务。</span></p>
<p><span style="font-size:20px;"> 设备类型初始化:</span></p>
<p><span style="font-size:20px;"> 设备类型(如传感器、开关等)需要在HAMqtt类之后进行初始化。关于如何初始化不同类型的设备,将在后面的文档中详细描述。</span></p>
</article>
<p><span style="font-size:20px;"><strong>2.配置Home Assistant</strong>:</span></p>
<p><span style="font-size:20px;"> 在Home Assistant中,无需手动添加设备,因为ArduinoHA支持MQTT自动发现。设备将自动出现在Home Assistant的设备列表中。</span></p>
<p><span style="font-size:20px;">使用HADevice 配置设备信息</span></p>
<p><span style="font-size:20px;"><span style="background-color: rgb(250, 250, 250); color: rgb(56, 58, 66); font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif;"> HADevice</span> 类是一个用于表示将要注册到 Home Assistant 设备注册表中的设备的类。在智能家居自动化系统中,每个设备都会有一个唯一的标识符(ID),以便系统能够区分和控制它们。<span style="background-color: rgb(250, 250, 250); color: rgb(56, 58, 66); font-family: "Source Code Pro", "DejaVu Sans Mono", "Ubuntu Mono", "Anonymous Pro", "Droid Sans Mono", Menlo, Monaco, Consolas, Inconsolata, Courier, monospace, "PingFang SC", "Microsoft YaHei", sans-serif;">HADevice</span> 类提供了构造函数来初始化设备对象,并允许设置设备的各种属性,如名称、制造商、型号等。</span></p>
<p><span style="font-size:20px;">使用Device types API来设置实体信息</span></p>
<p> </p>
<p> </p>
<p><span style="font-size:24px;"><strong>代码:</strong></span>>> <span style="font-size:20px;"><a href="https://download.eeworld.com.cn/detail/%E5%B0%B9%E5%B0%8F%E8%88%9F/634553" target="_blank">点击下载代码</a></span></p>
<p> </p>
<p><span style="font-size:24px;"><strong>项目总结</strong></span></p>
<p> <span style="font-size:20px;">通过这次Follow me第二季第2期的活动,我有幸接触并深入学习了Arduino uNo R4 WIFI板卡的基本功能,以及HA物联网系统的搭建和ArduinoHA的相关知识。这次活动为我提供了一个宝贵的学习和实践机会,让我能够更深入地理解物联网技术及其在实际应用中的潜力。通过实际操作,成功搭建了一个简单的物联网系统,实现了对智能家居设备的远程控制和监控。学习了ArduinoHA的基本概念和工作原理,了解了其作为Arduino与HA系统之间的桥梁所发挥的重要作用。本次Follow me第二季第2期的活动让我收获颇丰。我不仅学到了许多新知识,还通过实际操作加深了对物联网技术的理解。未来,我将继续深入学习物联网领域的相关知识,不断提高自己的专业技能和实践能力,为未来的项目开发和研究打下坚实的基础。同时,我也期待能够有更多的机会参与类似的活动,与更多的同行交流和学习。</span></p>
</article>
</article>
<p>哈哈,一个评论也没有</p>
页:
[1]