451浏览
查看: 451|回复: 2

[求助] 求助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();
}


TueMarch-202203083871..png
TueMarch-202203089236..png

muzyli  管理员

发表于 2022-3-9 09:42:42

把需要上报和订阅的两个TOPIC改下:
const char * subTopic = "/sys/a1XcjmmmPnM/sensor/thing/service/property/set";//****set

const char * pubTopic = "/sys/a1XcjmmmPnM/sensor/thing/event/property/post";//******post
回复

使用道具 举报

njwy1978  学徒

发表于 2022-6-1 15:55:05

本帖最后由 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界面上实时显示曲线。不知道怎么解决。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2022 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail