m0_62526997 2022-04-12 17:54 采纳率: 0%
浏览 92
已结题

esp8266联网json数据上报出错,如何解决?

#include <dummy.h>
#include<ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const int subQoS = 1;
String attributetopic = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/get"; //响应获取设备属性主题
String settingtopic = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/set";//响应设备属性设置
// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "daier";
const char* password = "12345678";
const char* mqttServer = "studio-mqtt.heclouds.com";
// MQTT服务端连接用户名密码
const char* mqttUserName = "Qa2qSsXHzi";
const char* mqttPassword = ""

//创建WiFi对象
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_STA);//设置ESP8266工作模式为无线终端模式
connectWifi();
// 设置MQTT服务器和端口号
mqttClient.setServer(mqttServer, 1883);
mqttClient.setCallback(receiveCallback);
// 连接MQTT服务器
connectMQTTServer();
}
void loop()
{
if (!mqttClient.connected())
{
connectMQTTServer();
}
mqttClient.loop();
}
// 连接MQTT服务器。
void connectMQTTServer()
{
// 客户端ID
String clientId = "T-home-bedroom";
if (mqttClient.connect(clientId.c_str(), mqttUserName, mqttPassword))
{
subscribeTopic();
}
}
//响应函数
void receiveCallback(char* topic, byte* payload, unsigned int length)
{
Serial.println(topic);
if (String(topic) == attributetopic)
{
String readvalue = "";
const size_t capacity1 = JSON_ARRAY_SIZE(5) + JSON_OBJECT_SIZE(4);
DynamicJsonDocument doc1(capacity1);
deserializeJson(doc1, payload);
const char* id1 = doc1["id"];
Serial.print(12);
delay(30);
while (Serial.available() > 0)
{
readvalue += (char)(Serial.read());
}
const size_t capacity3 = JSON_OBJECT_SIZE(5);
DynamicJsonDocument doc3(capacity3);
deserializeJson(doc3, readvalue);
const char* hometemp = doc3["hometemp"];
const char* homehumi = doc3["homehumi"];
const char* soiltemp = doc3["soiltemp"];
const char* soilhumi = doc3["soilhumi"];
const char* light = doc3["light"];
String topicString4 = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/get_reply";
char publishTopic[topicString4.length() + 1];
strcpy(publishTopic, topicString4.c_str());
// 建立发布信息。
String messageString = "{"id":"%d","code":200,"data":{"hometemp":%d,"homehumi":%d,"soiltemp":%d,"soilhumi":%d,"light":%d}}";
char publishMsg[messageString.length() + 1];
sprintf(messageString.c_str(), messageString.c_str(),id1, hometemp, homehumi, soiltemp, soilhumi, light);
strcpy(publishMsg, messageString.c_str);
//ESP8266向主题发布信息
mqttClient.publish(publishTopic, publishMsg);
Serial.println(publishMsg);
}
if (String(topic) == settingtopic);
{
const size_t capacity2 = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3) + 60;
DynamicJsonDocument doc2(capacity2);
deserializeJson(doc2, payload);
const char* id2 = doc2["id"];
const char* version2 = doc2["version"];
int allcontrol1 = doc2["params"]["allcontrol"];
if (allcontrol1 != 0)
{
Serial.print(allcontrol1);
}
}
}

// 订阅指定主题
void subscribeTopic()
{
// 订阅属性上报结果通知消息主题
String topicString = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/post/reply";
char subTopic[topicString.length() + 1];
strcpy(subTopic, topicString.c_str());
//订阅获取设备属性主题
String topicString3 = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/get";
char subTopic3[topicString3.length() + 1];
strcpy(subTopic3, topicString3.c_str());
//订阅设置设备属性主题
String topicString2 = "$sys/Qa2qSsXHzi/T-home-bedroom/thing/property/set";
char subTopic2[topicString2.length() + 1];
strcpy(subTopic2, topicString2.c_str());

mqttClient.subscribe(subTopic, subQoS);//订阅主题1
mqttClient.subscribe(subTopic2, subQoS);//订阅主题2
mqttClient.subscribe(subTopic3, subQoS);//订阅主题3
}
void connectWifi()
{
WiFi.begin(ssid, password);
//等待WiFi连接,成功连接后输出成功信息
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
}

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 4月20日
    • 创建了问题 4月12日

    悬赏问题

    • ¥15 TLE9879QXA40 电机驱动
    • ¥15 clion的参数提示怎么关闭
    • ¥20 对于工程问题的非线性数学模型进行线性化
    • ¥15 Mirare PLUS 进行密钥认证?(详解)
    • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
    • ¥20 想用ollama做一个自己的AI数据库
    • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
    • ¥15 请问怎么才能复现这样的图呀
    • ¥15 mifare plus卡认证
    • ¥30 LSTM预测结果是一条直线