4048| 5
|
[求助] 求助esp32上传不了温湿度到阿里云 |
esp32连接上了阿里云但是就是温湿度传不进去 温室读获取是正常的串口显示的是这种东西,应该是传输的问题 /*这是我的代码*/麻烦大佬看看下,我用的是商城给的库 #include <WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include "DFRobot_Aliyun.h" #include "DFRobot_DHT11.h" #define DHT11_PIN 14 /*配置WIFI名和密码*/ const char * WIFI_SSID = "fdb"; const char * WIFI_PASSWORD = "331700918"; /*配置设备证书信息*/ String ProductKey = "a1XcjmmmPnM"; String ClientId = "12345"; String DeviceName = "sensor"; String DeviceSecret = "5a5a9134ebb440bec294f78986a63f27"; /*配置域名和端口号*/ String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com"; uint16_t PORT = 1883; /*需要操作的产品标识符(温度和湿度两个标识符)*/ String TempIdentifier = "temp_1"; String HumiIdentifier = "Humi_1"; /*需要上报和订阅的两个TOPIC*/ const char * subTopic = "/sys/a1XcjmmmPnM/${deviceName}/thing/service/property/set";//****set const char * pubTopic = "/sys/a1XcjmmmPnM/${deviceName}/thing/event/property/post";//******post DFRobot_Aliyun myAliyun; WiFiClient espClient; PubSubClient client(espClient); DFRobot_DHT11 DHT; void connectWiFi(){ Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID,WIFI_PASSWORD); while(WiFi.status() != WL_CONNECTED){ delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("IP Adderss: "); Serial.println(WiFi.localIP()); } void callback(char * topic, byte * payload, unsigned int len){ Serial.print("Recevice ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < len; i++){ Serial.print((char)payload); } Serial.println(); } void ConnectAliyun(){ while(!client.connected()){ Serial.print("Attempting MQTT connection..."); /*根据自动计算的用户名和密码连接到Alinyun的设备,不需要更改*/ if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){ Serial.println("connected"); client.subscribe(subTopic); }else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup(){ Serial.begin(115200); /*连接WIFI*/ connectWiFi(); /*初始化Alinyun的配置,可自动计算用户名和密码*/ myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret); client.setServer(myAliyun.mqtt_server,PORT); /*设置回调函数,当收到订阅信息时会执行回调函数*/ client.setCallback(callback); /*连接到Aliyun*/ ConnectAliyun(); } uint8_t tempTime = 0; void loop(){ if(!client.connected()){ ConnectAliyun(); } /*一分钟上报两次温湿度信息*/ if(tempTime > 10){ tempTime = 0; DHT.read(DHT11_PIN); Serial.print("DHT.temperature="); Serial.println(DHT.temperature); Serial.print("DHT.humidity"); Serial.println(DHT.humidity); client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+TempIdentifier+"\":"+DHT.temperature+",\""+HumiIdentifier+"\":"+DHT.humidity+"},\"method\":\"thing.event.property.post\"}").c_str()); }else{ tempTime++; delay(500); } client.loop(); } |
本帖最后由 njwy1978 于 2022-6-7 19:33 编辑 我也是存在这个问题。阿里云IoT套件能够顺利的连接WiFi,但Web界面不能实时显示传送的温湿度数据。即使按照如下修改上报和订阅的两个TOPIC: const char * subTopic = "/sys/a1XcjmmmPnM/sensor/thing/service/property/set";//****set const char * pubTopic = "/sys/a1XcjmmmPnM/sensor/thing/event/property/post";//******post依然不能够实时在阿里云的Web界面上实时显示曲线。不知道怎么解决。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed