307|0

384

帖子

2

TA的资源

纯净的硅(初级)

楼主
 

【Follow me第二季第2期】--进阶扩展任务--UNO R4 温湿度传感器--HA显示--MQTTX显示 [复制链接]

 

演示视频:

閹绢厽鏂侀崳銊ュ鏉炶棄銇戠拹锟�: 閺堫亝顥呭ù瀣煂Flash Player閿涘矁顕崚锟�鐎瑰顥�
task3

UNO R4的温湿度传感器,HA显示:

1、任务目标

(1)UNO R4读取温湿度传感器数据。

(2)搭建HA服务器,采用docker方式。

(3)通过ESP32-S3模块发送,HA服务端显示当前温湿度。

2、参考文档

(1)https://docs.arduino.cc/tutorials/uno-r4-wifi/qwiic/

(2)https://docs.arduino.cc/tutorials/uno-r4-wifi/wifi-examples/

(3)https://zhuanlan.zhihu.com/p/421109780

(4)https://www.emqx.com/zh/blog/the-easiest-guide-to-getting-started-with-mqtt

3、分析

(1)MQTT是一种轻量级、基于发布-订阅模式的消息传输协议。

一个典型的MQTT示意图如下图所示。图中涉及以下几个关键概念:

  • MQTT客户端:发送或接受消息的设备,如图,左侧有1个客户端,右侧有3个客户端。
  • MQTT broker:如图中绿色六边形,可以理解为消息传输的中心或者消息传输的中转站。所有消息都先到MQTT broker,在到客户端。
  • 主题:发送或者接受消息时,为了区分消息内容所设置的主题。如图中 Temperature 就是一个主题,用于传输温度数据。

 

 

 

4、代码

  • #include <WiFiS3.h>
  • #include <UnoWiFiDevEd.h>
  • #include "Adafruit_SHT4x.h"
  • #include <Arduino_JSON.h>
  • #include <ArduinoMqttClient.h>
  • #if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
  • #include <WiFiNINA.h>
  • #elif defined(ARDUINO_SAMD_MKR1000)
  • #include <WiFi101.h>
  • #elif defined(ARDUINO_ARCH_ESP8266)
  • #include <ESP8266WiFi.h>
  • #elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_GIGA) || defined(ARDUINO_OPTA)
  • #include <WiFi.h>
  • #elif defined(ARDUINO_PORTENTA_C33)
  • #include <WiFiC3.h>
  • #elif defined(ARDUINO_UNOR4_WIFI)
  • #include <WiFiS3.h>
  • #endif
  • Adafruit_SHT4x sht4 = Adafruit_SHT4x();
  • ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  • char ssid[] = "wifi_name"; // your network SSID (name)
  • char pass[] = "wifi_password"; // your network password (use for WPA, or use as key for WEP)
  • int status = WL_IDLE_STATUS; // the WiFi radio's status
  • WiFiClient wifiClient;
  • MqttClient mqttClient(wifiClient);
  • const char broker[] = "broker.emqx.io";
  • int port = 1883;
  • const char topic[] = "homeassistant/sensor/room111/sht40";
  • const long interval = 1000;
  • unsigned long previousMillis = 0;
  • JSONVar jsonDat;
  • 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();
  • Serial.println("Find WIFI");
  • 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;
  • }
  • // set name
  • mqttClient.setUsernamePassword("test_sht40", "test_sht40");
  • Serial.print("Attempting to connect to the MQTT broker: ");
  • Serial.println(broker);
  • if (!mqttClient.connect(broker, port)) {
  • Serial.print("MQTT connection failed! Error code = ");
  • Serial.println(mqttClient.connectError());
  • while (1);
  • }
  • Serial.println("You're connected to the MQTT broker!");
  • Serial.println();
  • }
  • void loop() {
  • // 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();
  • mqttClient.poll();
  • if (currentMillis - previousMillis >= interval) {
  • // save the last time a message was sent
  • previousMillis = currentMillis;
  • sensors_event_t humidity, temp;
  • uint32_t timestamp = millis();
  • sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  • Serial.print("Temperature: ");
  • Serial.print(temp.temperature);
  • Serial.println(" degrees C");
  • Serial.print("Humidity: ");
  • Serial.print(humidity.relative_humidity);
  • Serial.println("% rH");
  • printCurrentNet();
  • int tempeture = temp.temperature;
  • int humi = humidity.relative_humidity;
  • jsonDat["tempeture"] = tempeture;
  • jsonDat["humidity"] = humi;
  • String strData = JSON.stringify(jsonDat);
  • // send message, the Print interface can be used to set the message contents
  • mqttClient.beginMessage(topic);
  • mqttClient.print(strData);
  • mqttClient.endMessage();
  • }
  • }
  • 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();
  • }

 

点赞 关注
个人签名

I-Love-MCU

 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
推荐帖子
初学者应购置些什么?

随着近年来青少年电子爱好者的增多,此类现象十分普遍,一是初学者刚入门不识行情,二是有人借此谋取不当利益。感慨之余仅以一家 ...

移植好的ucos2工程(基于LPC2000和Keil MDK)

欢迎大家提意见,谢谢!!

自制廉价的GPS外接天线

有网友试过,效果确实不错:宿舍窗台上(11楼)不到20秒,显示了时间,再过几秒,就显示了经纬度,没调,就收到4颗心!! 材 ...

给当年磁学没学好的朋友《磁性元器件分册》-赵修科

这是一本很经典的有关磁性元件的书在学校一般磁方面讲得比较少很多人也没有理解什么是磁场上次看到一个人怎么都不理解(很多人仅 ...

解决体重称HX711芯片的隐藏BUG问题

前言 之前做了一个项目,里面有一个体重检测的功能,查阅了很多的资料,最后锁定使用HX711芯片,这个国产的芯片功能集成的很 ...

DSP的嵌入式温度测量系统

为了实现嵌入式温度测量系统,提出了基于MZBB-2铂薄膜热敏与TMS320F2812DSP控制芯片的嵌入式解决方案,完成 ...

体验一款国产USB音频芯片后的所想,所感

芯片特点 1,USB1.0,2.0可以兼容 2,32位的MCU(M0的内核),频率可以达到96MZH, 2,内置DSP,32位的处理能力 3,已经写好ENC ...

综合布线系统光缆分类及其特点?

综合布线系统光缆是一种用于数据传输和通信的电缆,常用于建筑物内部网络和通信系统的布线。光缆采用光纤作为传输介质,能够以光 ...

【小华工控新品HC32F448】03.体验AOS自动运行系统的智能

1.概述 小华HC32F488系列芯片具备了AOS自动运行系统这个外设功能,啥是AOS自动运行系统呢?难道HC32F488芯片内自带了一个能够 ...

NMOS防反接电路,这里左边的D和S应该对调才对吧?

NMOS防反接电路,这里左边的D和S应该对调才对吧? 865275

关闭
站长推荐上一条 1/10 下一条
有奖直播:当AI遇见仿真,会有什么样的电子行业革新之路?
首场直播:Simcenter AI 赋能电子行业研发创新
直播时间:04月15日14:00-14:50

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 1

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表