eew_HqZm7h

  • 2024-10-09
  • 加入了学习《【Follow me第二季第2期】演示视频》,观看 2.放大正弦波信号

  • 加入了学习《【Follow me第二季第2期】演示视频》,观看 1.演示矩阵LED,滚动显示hello world

  • 2024-10-08
  • 发表了主题帖: 【Follow me第二季第2期】任务汇总

    本帖最后由 eew_HqZm7h 于 2024-11-17 15:59 编辑 写在前面: 首先,感谢得捷官方举办的这次活动,很荣幸参与到这次Arduino开发板任务当中,这是我第一次接触到嵌入式开发的任务,之前都是写软件的代码,看了些入门的课程开始做任务。很开心能学习到之前没有接触过的知识,了解了数字信号和模拟信号的转换,模拟信号的放大功能,学习到矩阵LED灯的使用,如何实现家庭设备智能化的操作。这次使用开发板的过程并不是一帆风顺,而是遇到了很多的问题,在连接HomeAssistant的过程中不断的失败,最后多亏大佬们的帖子才完成了任务。通过这次活动,不但收获了知识,还扩展了眼界,了解了很多新奇的事物。最后,衷心祝愿每一位同学变得更强,得捷官方的活动越办越红火!   代码地址:https://download.eeworld.com.cn/detail/eew_HqZm7h/634555 视频地址:【Follow me第二季第2期】任务视频 任务地址:【Follow me第二季第2期】搭建环境并开启第一步Blink / 串口打印Hello EEWorld! 【Follow me第二季第2期】驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号 【Follow me第二期】进阶任务 通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA 【Follow me第二季第2期】通过外部SHT40温湿度传感器,上传温湿度到HA,通过HA面板显示数据   所用硬件:    任务一:搭建环境并开启第一步Blink / 串口打印Hello EEWorld! 任务步骤: 1.下载Arduino,搭建环境 2.串口打印Hello EEWorld!   流程图:   1.下载Arduino,搭建环境 在官网Arduino 下载   选择对应的操作系统,填写好邮箱后就可以下载了   2.串口打印Hello EEWorld!   和一般的开发语言一样,直接使用打印函数即可,但是得首先设置串口通信的波特率,输出窗口的波特率需要和程序中的一致 void setup() { // put your setup code here, to run once: Serial.begin(115200); Serial.print("hello world"); } void loop() { // put your main code here, to run repeatedly: }  实验结果:     任务二:驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号;用ADC采集并且打印数据到串口等其他接口可上传到上位机显示曲线 任务步骤: 1.通过样例使用LED矩阵 2.参考样例用DAC生成正弦波 3.使用运算放大器放大正弦波信号   1.通过样例使用LED矩阵 流程图:   参考官网的 led-matrix示例代码启动矩阵,并滚动显示hello world #include "Arduino_LED_Matrix.h" //引用矩阵库   ArduinoLEDMatrix matrix; //创建矩阵对象   //在void setup()中启动矩阵 matrix.begin();` #include "Arduino_LED_Matrix.h" ArduinoLEDMatrix matrix; void setup() { Serial.begin(115200); matrix.begin(); }   // To use ArduinoGraphics APIs, please include BEFORE Arduino_LED_Matrix #include "ArduinoGraphics.h" #include "Arduino_LED_Matrix.h" ArduinoLEDMatrix matrix; void setup() { Serial.begin(115200); matrix.begin(); //启动矩阵(初始化矩阵) matrix.beginDraw(); //矩阵开启绘图(之后的绘图操作会显示在屏幕上) matrix.stroke(0xFFFFFFFF); //设置图形颜色。FFFFFFFF为红色 // add some static text // will only show "UNO" (not enough space on the display) const char text[] = "UNO r4"; // 文本显示内容 matrix.textFont(Font_4x6);//设置用于文本的字体。目前的库内置了Font_4x6和Font_5x7。 matrix.beginText(0, 1, 0xFFFFFF);//设置文本起始位置和文本颜色(第一列第二行开始设置文字) matrix.println(text);//开始显示文本内容 matrix.endText();//结束显示文本内容//没有参数则为静态文本 matrix.endDraw();//结束绘图 delay(2000); } void loop() { // Make it scroll! matrix.beginDraw();//开始绘图 matrix.stroke(0xFFFFFFFF);//设置绘图颜色 matrix.textScrollSpeed(50);//控制每个像素间的延迟,以毫秒为单位(滚动文本时需要设置) // add the text const char text[] = " Hello World! "; matrix.textFont(Font_5x7); matrix.beginText(0, 1, 0xFFFFFF); matrix.println(text); matrix.endText(SCROLL_LEFT); //scrollDirection:(可选)滚动方向,如果未提供,则默认为NO_SCROLL。有效选项为NO_SCROLL、SCROLL_LEFT、SCROLL_RIGHT、SCROLL_UP、SCROLL_DOWN matrix.endDraw(); } 矩阵滚动显示hello world视频      2.参考样例用DAC生成正弦波 流程图:   参考官网的 DAC 样例,实现生成正弦波 锯齿波用wave.saw() 方形波用wave.square() //生成正弦波 #include "analogWave.h" // Include the library for analog waveform generation analogWave wave(DAC); //define DAC A0 int freq = 100;//正弦波频率 void setup () { Serial.begin(115200); analogReadResolution(14); //模拟信号读取分辨率 wave.sine(freq); //生成正弦波 wave.amplitude(0.5); //改变信号幅值 } void loop() { Serial.println(analogRead(A1)); // DAC output }     3.使用运算放大器放大正弦波信号 流程图:   参考官网 OPAMP 的电路图以及课堂中老师讲解的电路图。         A0接收信号传递给A1,A1为放大前信号,A3为放大后信号。 实际连接电路图:       #include <OPAMP.h> #include "analogWave.h" // Include the library for analog waveform generation analogWave wave(DAC); //define DAC A0 int freq = 100;//正弦波频率 void setup () { Serial.begin(250000); analogReadResolution(14); //模拟信号读取分辨率 wave.sine(freq); //形成正弦波 wave.amplitude(0.5); //改变信号幅值 OPAMP.begin(OPAMP_SPEED_HIGHSPEED); //以高速率开启运算放大器 } void loop() { Serial.print(analogRead(A1)); // DAC output Serial.print(" "); Serial.println(analogRead(A3)); // OPAMP output } 实验结果: 放大正弦波信号视频   任务三:通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA(HomeAssistant) Home Assistant简介 Home Assistant 是一款免费的开源家庭自动化软件,将可联网的设备接入到Home Assistant中,可以实现对它们远程控制,在手机上可以远程控制灯光、电气设备,监控室内温度并自动执行开空调等操作。也可以关联不同的设备,使它们流程化操作。   任务步骤: 1.单片机连接WIFI 2.部署Home Assistant 3.部署MQTT服务器 4.Home Assistant与MQTT服务器连接 5.单片机通过MQTT服务连接到Home Assistant中   流程图:   1.单片机连接WIFI 使用arduino自带的样例连接WIFI   注意点:1.自己手动创建arduino_secrets.h头文件,设置SECRET_SSID(WIFI名称)和SECRET_PASS(WIFI密码)。或者在代码中直接对ssid和pass赋值。 2.单片机只能接受到2.4GHZ信号的WIFI,可以先用ScanNetworksAdvanced样例,扫描是否能接收到WIFI的信号。 /* This example connects to an unencrypted WiFi network. Then it prints the MAC address of the WiFi module, the IP address obtained, and other network details. created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe Find the full UNO R4 WiFi Network documentation here: https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#connect-with-wpa */ #include <WiFiS3.h> #include "arduino_secrets.h" ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; // the WiFi radio's status void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } String fv = WiFi.firmwareVersion(); if (fv < 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); } // you're connected now, so print out the data: Serial.print("You're connected to the network"); printCurrentNet(); printWifiData(); } void loop() { // check the network connection once every 10 seconds: delay(10000); printCurrentNet(); } 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[6]; WiFi.macAddress(mac); Serial.print("MAC address: "); printMacAddress(mac); } 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[6]; 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 < 6; i++) { if (i > 0) { Serial.print(":"); } if (mac[i] < 16) { Serial.print("0"); } Serial.print(mac[i], HEX); } Serial.println(); }   连接成功后会打印单片机的IP地址以及Mac地址   2.部署Home Assistant(Docker部署) 刚开始,我是直接下载Home Assistant镜像直接部署的,等了很久下载文件,但是最后还是失败了。换成容器部署   使用Docker部署Home Assistant 前提提要:将docker的拉取镜像的源换成国内源(如阿里云)   (1)拉取镜像(连接WIFI下载好多次都失败,换成流量下载试了两三次成功了) docker pull homeassistant/home-assistant:latest //获取最新版的Home Assistant 也可以获取稳定版 docker pull homeassistant/home-assistant:stable 等待所有相关程序Pull complete就安装成功了。   (2)创建并运行容器 docker run -d --name="hass" -v /home/hass/config:/config -p 8123:8123 homeassistant/home-assistant:latest (3)查看容器 docker ps -a   (4)使用自己的虚拟机IP+端口号8123在主机上访问Home Assistant   (5)创建账号并成功登录   3.部署MQTT服务器 (1)拉取EMQX镜像 docker pull emqx/emqx:5.8.0 (2)创建并启动EMQX容器 docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.8.0 (3)通过虚拟机IP+端口号18083访问EMQX(默认用户名:admin,密码:public)    4.Home Assistant与MQTT服务器通信 (1)创建内置数据库(数据库用来记录连接的信息)(全部选默认的即可)   (2)创建用户(Home Assistant通过创建的用户账号和密码连接到EMQX)   (3)Home Assistant连接EMQX 在Home Assistant主页面点击设置--设备与服务--添加集成(右下角)--搜索MQTT--点击MQTT 注意点(这里的代理是EMQX节点的地址,不是EMQX服务器的地址)   连接成功后连接数变为1(注意图中的EMQX节点地址)   5.单片机通过MQTT服务连接到Home Assistant中 (1)下载ArduinoMqttClient库,使用WiFiSimpleSender样例(此样例会连接WIFI并连接MQTT服务器)   需要修改的地方:(注:MQTT服务器的账号和密码为注释项,如果自己的服务器设置账号和密码,需要自己取消注释并修改。)         连接成功后一直发送消息:   两个错误点: (1) MQTT connection failed! Error code = -2   这个问题困扰了我很长时间。最后解决的方法:将虚拟机的网络和单片机的网络接入到同一个网段。 将虚拟机网络连接模式改为桥接模式,并在“编辑”中选择“虚拟网络编辑器”,指定桥接的网卡为无线网卡。并将单片机连接的热点和主机连接的热点一致。 之前 一直连接失败,是将虚拟机的连接模式设置为NAT模式,而且将单片机连接到了电脑的热点上。   (2)MQTT connection failed! Error code = 5 这是连接MQTT时认证失败,应该检查账号和密码,或者关闭MQTT服务器的内置数据库(起认证作用)。   扩展任务:通过外部SHT40温湿度传感器,上传温湿度到HA,通过HA面板显示数据 任务步骤: 1.单片机接收传感器数据并打印 2.将数据通过MQTT协议传输到Home Assistant上   流程图:   1.单片机接收传感器数据并打印 (1)下载“SHT4x”库,使用SHT4test样例测试传感器传输数据到单片机   /*************************************************** 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()) { 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); }   测试结果:   2.将数据通过MQTT协议传输到Home Assistant上 (1)在Home Assistant上创建传感器主题(各个参数的信息可以在MQTT Sensor查看) 点击“配置”,发送数据包,创建传感器   (2)发送数据包创建传感器数据(通过监听测试发送是否成功)     homeassistant/sensor/SHT40_HUMI/config //创建湿度数据 { "device_class":"humidity", "name":"Humidity", "state_topic":"homeassistant/SHT40/state", "unit_of_measurement":"rH%", "value_template":"{{ value_json.humidity}}", "unique_id":"hum_HA101", "device":{ "identifiers":[ "SHT40" ], "name":"SHT40-HUMI_HUMI" } }   homeassistant/sensor/SHT40_TEMP/config //创建温度数据 { "device_class":"temperature", "name":"Temperature", "state_topic":"homeassistant/SHT40/state", "unit_of_measurement":"°C", "value_template":"{{ value_json.temperature}}", "unique_id":"temp_HA101", "device":{ "identifiers":[ "SHT40" ], "name":"SHT40-TEMP_TEMP" } } 测试结果:得到接收湿度数据和温度数据的主体   (3)单片机将传感器的数据传输到HomeAssistant中 /* This example connects to a MQTT broker and publishes a message to a topic once a second. The circuit: - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board This example code is in the public domain. */ #include <ArduinoMqttClient.h> #include <WiFiS3.h> #include "Adafruit_SHT4x.h" Adafruit_SHT4x sht4 = Adafruit_SHT4x(); ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = "一觉睡到日三竿"; // your network SSID (name) char pass[] = "chen2456594826"; // your network password (use for WPA, or use as key for WEP) // To connect with SSL/TLS: // 1) Change WiFiClient to WiFiSSLClient. // 2) Change port value from 1883 to 8883. // 3) Change broker value to a server with a known SSL/TLS root certificate // flashed in the WiFi module. WiFiClient wifiClient; MqttClient mqttClient(wifiClient); const char broker[] = "192.168.187.230"; int port = 1883; const char topic[] = "homeassistant/SHT40/state"; const long interval = 1000; unsigned long previousMillis = 0; int count = 0; void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // attempt to connect to WiFi network: Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); while (WiFi.begin(ssid, pass) != WL_CONNECTED) { // failed, retry Serial.print("."); delay(5000); } Serial.println("You're connected to the network"); Serial.println(ssid); printWifiStatus(); Serial.println(); // You can provide a unique client ID, if not set the library uses Arduino-millis() // Each client must have a unique client ID mqttClient.setId("clientIdmurong"); // You can provide a username and password for authentication // mqttClient.setUsernamePassword("arduinoUnoBoard", "arduinoUnoBoard"); mqttClient.setUsernamePassword("admin", "20030121chen"); Serial.print("Attempting to connect to the MQTT broker: "); Serial.println(broker); #if 0 if (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError()); while (1); } #endif while (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError()); delay(100); } Serial.println("You're connected to the MQTT broker!"); Serial.println(); //Following are SHT4x Test Serial.println("Adafruit SHT4x Init"); if (! sht4.begin()) { 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; sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data // call poll() regularly to allow the library to send MQTT keep alives which // avoids being disconnected by the broker mqttClient.poll(); // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time a message was sent previousMillis = currentMillis; Serial.print("Sending message to topic: "); Serial.println(topic); Serial.println(count); // send message, the Print interface can be used to set the message contents mqttClient.beginMessage(topic); mqttClient.print("{\"humidity\": "); mqttClient.print(humidity.relative_humidity); mqttClient.print(", \"temperature\": "); mqttClient.print(temp.temperature); mqttClient.print("}"); mqttClient.endMessage(); Serial.println(); count++; } } /* -------------------------------------------------------------------------- */ void printWifiStatus() { /* -------------------------------------------------------------------------- */ // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); } 测试结果: 上传温度湿度到Home Assistant视频 获得温度和湿度  

  • 加入了学习《【Follow me第二季第2期】任务视频》,观看 1.搭建环境并开启第一步Blink / 串口打印Hello EEWorld!

  • 2024-10-07
  • 上传了资料: 【Follow me 第二季第2期】代码

  • 发表了主题帖: 【Follow me第二季第2期】 扩展任务二:通过外部SHT40温湿度传感器,上传温湿度到H...

    本帖最后由 eew_HqZm7h 于 2024-10-8 13:41 编辑 任务步骤: 1.单片机接收传感器数据并打印 2.将数据通过MQTT协议传输到Home Assistant上   1.单片机接收传感器数据并打印 (1)下载“SHT4x”库,使用SHT4test样例测试传感器传输数据到单片机   /*************************************************** 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()) { 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); } 测试结果:   2.将数据通过MQTT协议传输到Home Assistant上 (1)在Home Assistant上创建传感器主题(各个参数的信息可以在MQTT Sensor查看) 点击“配置”,发送数据包,创建传感器 (2)发送数据包创建传感器数据(通过监听测试发送是否成功)   homeassistant/sensor/SHT40_HUMI/config //创建湿度数据 { "device_class":"humidity", "name":"Humidity", "state_topic":"homeassistant/SHT40/state", "unit_of_measurement":"rH%", "value_template":"{{ value_json.humidity}}", "unique_id":"hum_HA101", "device":{ "identifiers":[ "SHT40" ], "name":"SHT40-HUMI_HUMI" } }   homeassistant/sensor/SHT40_TEMP/config //创建温度数据 { "device_class":"temperature", "name":"Temperature", "state_topic":"homeassistant/SHT40/state", "unit_of_measurement":"°C", "value_template":"{{ value_json.temperature}}", "unique_id":"temp_HA101", "device":{ "identifiers":[ "SHT40" ], "name":"SHT40-TEMP_TEMP" } } 测试结果:得到接收湿度数据和温度数据的主体    (3)单片机将传感器的数据传输到HomeAssistant中 /* This example connects to a MQTT broker and publishes a message to a topic once a second. The circuit: - Arduino MKR 1000, MKR 1010 or Uno WiFi Rev2 board This example code is in the public domain. */ #include <ArduinoMqttClient.h> #include <WiFiS3.h> #include "Adafruit_SHT4x.h" Adafruit_SHT4x sht4 = Adafruit_SHT4x(); ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = "一觉睡到日三竿"; // your network SSID (name) char pass[] = "chen2456594826"; // your network password (use for WPA, or use as key for WEP) // To connect with SSL/TLS: // 1) Change WiFiClient to WiFiSSLClient. // 2) Change port value from 1883 to 8883. // 3) Change broker value to a server with a known SSL/TLS root certificate // flashed in the WiFi module. WiFiClient wifiClient; MqttClient mqttClient(wifiClient); const char broker[] = "192.168.187.230"; int port = 1883; const char topic[] = "homeassistant/SHT40/state"; const long interval = 1000; unsigned long previousMillis = 0; int count = 0; void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // attempt to connect to WiFi network: Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); while (WiFi.begin(ssid, pass) != WL_CONNECTED) { // failed, retry Serial.print("."); delay(5000); } Serial.println("You're connected to the network"); Serial.println(ssid); printWifiStatus(); Serial.println(); // You can provide a unique client ID, if not set the library uses Arduino-millis() // Each client must have a unique client ID mqttClient.setId("clientIdmurong"); // You can provide a username and password for authentication // mqttClient.setUsernamePassword("arduinoUnoBoard", "arduinoUnoBoard"); mqttClient.setUsernamePassword("admin", "20030121chen"); Serial.print("Attempting to connect to the MQTT broker: "); Serial.println(broker); #if 0 if (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError()); while (1); } #endif while (!mqttClient.connect(broker, port)) { Serial.print("MQTT connection failed! Error code = "); Serial.println(mqttClient.connectError()); delay(100); } Serial.println("You're connected to the MQTT broker!"); Serial.println(); //Following are SHT4x Test Serial.println("Adafruit SHT4x Init"); if (! sht4.begin()) { 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; sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data // call poll() regularly to allow the library to send MQTT keep alives which // avoids being disconnected by the broker mqttClient.poll(); // to avoid having delays in loop, we'll use the strategy from BlinkWithoutDelay // see: File -> Examples -> 02.Digital -> BlinkWithoutDelay for more info unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time a message was sent previousMillis = currentMillis; Serial.print("Sending message to topic: "); Serial.println(topic); Serial.println(count); // send message, the Print interface can be used to set the message contents mqttClient.beginMessage(topic); mqttClient.print("{\"humidity\": "); mqttClient.print(humidity.relative_humidity); mqttClient.print(", \"temperature\": "); mqttClient.print(temp.temperature); mqttClient.print("}"); mqttClient.endMessage(); Serial.println(); count++; } } /* -------------------------------------------------------------------------- */ void printWifiStatus() { /* -------------------------------------------------------------------------- */ // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); }    

  • 发表了主题帖: 【Follow me第2期】进阶任务 通过Wi-Fi,利用MQTT协议接入到开源的智能家居平台HA...

    本帖最后由 eew_HqZm7h 于 2024-10-9 12:59 编辑 Home Assistant简介 Home Assistant 是一款免费的开源家庭自动化软件,将可联网的设备接入到Home Assistant中,可以实现对它们远程控制,在手机上可以远程控制灯光、电气设备,监控室内温度并自动执行开空调等操作。也可以关联不同的设备,使它们流程化操作。   任务步骤: 1.单片机连接WIFI 2.部署Home Assistant 3.部署MQTT服务器 4.Home Assistant与MQTT服务器连接 5.单片机通过MQTT服务连接到Home Assistant中   1.单片机连接WIFI 使用arduino自带的样例连接WIFI   注意点:1.自己手动创建arduino_secrets.h头文件,设置SECRET_SSID(WIFI名称)和SECRET_PASS(WIFI密码)。或者在代码中直接对ssid和pass赋值。 2.单片机只能接受到2.4GHZ信号的WIFI,可以先用ScanNetworksAdvanced样例,扫描是否能接收到WIFI的信号。 /* This example connects to an unencrypted WiFi network. Then it prints the MAC address of the WiFi module, the IP address obtained, and other network details. created 13 July 2010 by dlf (Metodo2 srl) modified 31 May 2012 by Tom Igoe Find the full UNO R4 WiFi Network documentation here: https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples#connect-with-wpa */ #include <WiFiS3.h> #include "arduino_secrets.h" ///////please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) int status = WL_IDLE_STATUS; // the WiFi radio's status void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } String fv = WiFi.firmwareVersion(); if (fv < 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); } // you're connected now, so print out the data: Serial.print("You're connected to the network"); printCurrentNet(); printWifiData(); } void loop() { // check the network connection once every 10 seconds: delay(10000); printCurrentNet(); } 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[6]; WiFi.macAddress(mac); Serial.print("MAC address: "); printMacAddress(mac); } 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[6]; 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 < 6; i++) { if (i > 0) { Serial.print(":"); } if (mac[i] < 16) { Serial.print("0"); } Serial.print(mac[i], HEX); } Serial.println(); } 连接成功后会打印单片机的IP地址以及Mac地址     2.部署Home Assistant(Docker部署) 刚开始,我是直接下载Home Assistant镜像直接部署的,等了很久下载文件,但是最后还是失败了。   使用Docker部署Home Assistant 前提提要:将docker的拉取镜像的源换成国内源(如阿里云)   (1)拉取镜像(连接WIFI下载好多次都失败,换成流量下载试了两三次成功了) docker pull homeassistant/home-assistant:latest //获取最新版的Home Assistant 也可以获取稳定版 docker pull homeassistant/home-assistant:stable 等待所有相关程序Pull complete就安装成功了。   (2)创建并运行容器 docker run -d --name="hass" -v /home/hass/config:/config -p 8123:8123 homeassistant/home-assistant:latest  (3)查看容器 docker ps -a (4)使用自己的虚拟机IP+端口号8123在主机上访问Home Assistant   (5)创建账号并成功登录   3.部署MQTT服务器 (1)拉取EMQX镜像 docker pull emqx/emqx:5.8.0 (2)创建并启动EMQX容器 docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx:5.8.0 (3)通过虚拟机IP+端口号18083访问EMQX(默认用户名:admin,密码:public)  4.Home Assistant与MQTT服务器通信 (1)创建内置数据库(数据库用来记录连接的信息)(全部选默认的即可)   (2)创建用户(Home Assistant通过创建的用户账号和密码连接到EMQX)   (3)Home Assistant连接EMQX 在Home Assistant主页面点击设置--设备与服务--添加集成(右下角)--搜索MQTT--点击MQTT 注意点(这里的代理是EMQX节点的地址,不是EMQX服务器的地址)   连接成功后连接数变为1(注意图中的EMQX节点地址)   5.单片机通过MQTT服务连接到Home Assistant中 (1)下载ArduinoMqttClient库,使用WiFiSimpleSender样例(此样例会连接WIFI并连接MQTT服务器)   需要修改的地方:(注:MQTT服务器的账号和密码为注释项,如果自己的服务器设置账号和密码,需要自己取消注释并修改。)       连接成功后一直发送消息:   两个错误点: (1) MQTT connection failed! Error code = -2   这个问题困扰了我很长时间。最后解决的方法:将虚拟机的网络和单片机的网络接入到同一个网段。 将虚拟机网络连接模式改为桥接模式,并在“编辑”中选择“虚拟网络编辑器”,指定桥接的网卡为无线网卡。并将单片机连接的热点和主机连接的热点一致。 之前 一直连接失败,是将虚拟机的连接模式设置为NAT模式,而且将单片机连接到了电脑的热点上。    (2)MQTT connection failed! Error code = 5 这是连接MQTT时认证失败,应该检查账号和密码,或者关闭MQTT服务器的内置数据库(起认证作用)。  

  • 发表了主题帖: 【Follow me第二季第2期】驱动12x8点阵LED;用DAC生成正弦波;用OPAMP放大DAC信号...

    本帖最后由 eew_HqZm7h 于 2024-10-9 12:57 编辑 任务步骤: 1.通过样例使用LED矩阵 2.参考样例用DAC生成正弦波 3.使用运算放大器放大正弦波信号   1.通过样例使用LED矩阵 参考官网的 led-matrix示例代码启动矩阵,并滚动显示hello world #include "Arduino_LED_Matrix.h" //引用矩阵库 ArduinoLEDMatrix matrix; //创建矩阵对象 //在void setup()中启动矩阵 matrix.begin();` #include "Arduino_LED_Matrix.h" ArduinoLEDMatrix matrix; void setup() { Serial.begin(115200); matrix.begin(); }   // To use ArduinoGraphics APIs, please include BEFORE Arduino_LED_Matrix #include "ArduinoGraphics.h" #include "Arduino_LED_Matrix.h" ArduinoLEDMatrix matrix; void setup() { Serial.begin(115200); matrix.begin(); //启动矩阵(初始化矩阵) matrix.beginDraw(); //矩阵开启绘图(之后的绘图操作会显示在屏幕上) matrix.stroke(0xFFFFFFFF); //设置图形颜色。FFFFFFFF为红色 // add some static text // will only show "UNO" (not enough space on the display) const char text[] = "UNO r4"; // 文本显示内容 matrix.textFont(Font_4x6);//设置用于文本的字体。目前的库内置了Font_4x6和Font_5x7。 matrix.beginText(0, 1, 0xFFFFFF);//设置文本起始位置和文本颜色(第一列第二行开始设置文字) matrix.println(text);//开始显示文本内容 matrix.endText();//结束显示文本内容//没有参数则为静态文本 matrix.endDraw();//结束绘图 delay(2000); } void loop() { // Make it scroll! matrix.beginDraw();//开始绘图 matrix.stroke(0xFFFFFFFF);//设置绘图颜色 matrix.textScrollSpeed(50);//控制每个像素间的延迟,以毫秒为单位(滚动文本时需要设置) // add the text const char text[] = " Hello World! "; matrix.textFont(Font_5x7); matrix.beginText(0, 1, 0xFFFFFF); matrix.println(text); matrix.endText(SCROLL_LEFT); //scrollDirection:(可选)滚动方向,如果未提供,则默认为NO_SCROLL。有效选项为NO_SCROLL、SCROLL_LEFT、SCROLL_RIGHT、SCROLL_UP、SCROLL_DOWN matrix.endDraw(); } 滚动显示hello world 视频 2.参考样例用DAC生成正弦波 参考官网的 DAC 样例,实现生成正弦波 锯齿波用wave.saw() 方形波用wave.square() //生成正弦波 #include "analogWave.h" // Include the library for analog waveform generation analogWave wave(DAC); //define DAC A0 int freq = 100;//正弦波频率 void setup () { Serial.begin(115200); analogReadResolution(14); //模拟信号读取分辨率 wave.sine(freq); //生成正弦波 wave.amplitude(0.5); //改变信号幅值 } void loop() { Serial.println(analogRead(A1)); // DAC output }    3.使用运算放大器放大正弦波信号 参考官网 OPAMP 的电路图以及课堂中老师讲解的电路图。        A0接收信号传递给A1,A1为放大前信号,A3为放大后信号。 实际连接电路图:     #include <OPAMP.h> #include "analogWave.h" // Include the library for analog waveform generation analogWave wave(DAC); //define DAC A0 int freq = 100;//正弦波频率 void setup () { Serial.begin(250000); analogReadResolution(14); //模拟信号读取分辨率 wave.sine(freq); //形成正弦波 wave.amplitude(0.5); //改变信号幅值 OPAMP.begin(OPAMP_SPEED_HIGHSPEED); //以高速率开启运算放大器 } void loop() { Serial.print(analogRead(A1)); // DAC output Serial.print(" "); Serial.println(analogRead(A3)); // OPAMP output } 实验结果: 放大正弦波信号视频    

  • 发表了主题帖: 【Follow me第二季第2期】搭建环境并开启第一步Blink / 串口打印Hello EEWorld!

    本帖最后由 eew_HqZm7h 于 2024-10-9 11:26 编辑 任务步骤: 1.下载Arduino,搭建环境 2.串口打印Hello EEWorld!   1.下载Arduino,搭建环境 在官网Arduino 下载    选择对应的操作系统,填写好邮箱后就可以下载了   2.串口打印Hello EEWorld!   和一般的开发语言一样,直接使用打印函数即可,但是得首先设置串口通信的波特率,输出窗口的波特率需要和程序中的一致 void setup() {   // put your setup code here, to run once:    Serial.begin(115200);    Serial.print("hello world"); } void loop() {   // put your main code here, to run repeatedly: }  实验结果:

  • 加入了学习《【Follow me第二季第2期】+开发板硬件介绍和实现任务一 LED灯闪烁和串口打印》,观看 【Follow me第二季第2期】实现任务二 驱动12x8点阵LED;用DAC生成正弦波并放大采集

  • 加入了学习《【Follow me第二季第2期】+开发板硬件介绍和实现任务一 LED灯闪烁和串口打印》,观看 【Follow me第二季第2期】+通过外部SHT40温湿度传感器,上传温湿度到HA

  • 加入了学习《FollowMe 第二季:2 - Arduino UNO R4 Wi-Fi 及任务讲解》,观看 Arduino UNO R4 Wi-Fi 及任务讲解

  • 加入了学习《【Follow me第二季第2期】》,观看 【Follow me第二季第2期】

  • 加入了学习《Follow me 第二季第2期dvacos视频》,观看 Follow me 第二季第2期视频

  • 加入了学习《【Follow me 第二季第2期任务】 各个任务实现的展示效果》,观看 【Follow me 第二季第2期任务】MQTT接入到HomeAssistant

  • 加入了学习《【Follow me 第二季第2期任务】 各个任务实现的展示效果》,观看 【Follow me 第二季第2期任务】Blink / 串口打印Hello EEWorld!

  • 加入了学习《【Follow me 第二季第2期任务】 各个任务实现的展示效果》,观看 【Follow me 第二季第2期任务】通过外部SHT40温湿度传感器,上传温湿度到HA,通过HA面板显示数据

  • 加入了学习《【Follow me 第二季第2期任务】 各个任务实现的展示效果》,观看 【Follow me 第二季第2期任务】驱动12x8点阵LED;用DAC生成正弦波;OPAMP放大DAC信号;用ADC采集并上传波形

最近访客

< 1/1 >

统计信息

已有1人来访过

  • 芯积分:81
  • 好友:--
  • 主题:5
  • 回复:0

留言

你需要登录后才可以留言 登录 | 注册


现在还没有留言