变量char数组的ESP32 Arduino PubSub发布错误



我正在使用Arduino编程ESP32,并使用标准pubsubclient库发布MQTT消息。

在下面的循环中,我注意到我的"内部循环"被发布了,然而,变量jsonObjChar的第二条消息似乎从未发布到主题中?串行监视器中没有任何错误。

void loop(){
String temperature = String(readDHTTemperature());
String humidity = String(readDHTHumidity());
String light = String(readLDRLight());

String jsonObj = "{";    
jsonObj.concat(""deviceId":"123456"");
jsonObj.concat(",");
jsonObj.concat(""messageType":"ambientSensorReading"");
jsonObj.concat(",");
jsonObj.concat(""temperature":"");
jsonObj.concat(temperature);
jsonObj.concat(""");
jsonObj.concat(",");
jsonObj.concat(""humidity":"");
jsonObj.concat(humidity);
jsonObj.concat(""");
jsonObj.concat(",");
jsonObj.concat(""light":"");
jsonObj.concat(light);
jsonObj.concat(""}");
delay(1000);
int jsonObjCharLength = jsonObj.length() + 1;
char jsonObjChar[jsonObjCharLength];
jsonObj.toCharArray(jsonObjChar, jsonObjCharLength);
Serial.println("PREPARED");  
Serial.println(jsonObjChar);  
const char topic[13] = "prototype001";
client.publish(topic, "inside loop");
client.publish(topic, jsonObjChar);
// ... and resubscribe
client.subscribe("prototype001");
delay(5000);
}

变量jsobObjChar似乎很好,当我把它打印到串行监视器时,它看起来像一个普通的字符串(我已经用JSON格式构建了结构,以便在服务器端处理(

20:13:41.494 -> PREPARED
20:13:41.494 -> {"deviceId":"123456","messageType":"ambientSensorReading","temperature":"25.10","humidity":"49.90","light":"2592"}
20:13:46.521 ->

如果有帮助的话,我将使用cloudmqtt。

如有任何帮助,我们将不胜感激!!!

尝试去掉字符串,它们会破坏堆并导致崩溃。定义全局固定charBuffers(足够大,可以接收最大的消息(和helper(tmp chars(进行转换和其他操作。不要在循环中定义const chars。在设置之前先定义,这样编译器就可以将它们放在堆栈中,并且在运行时不使用堆。我把你的代码改成了这个原则,并在消息之间增加了一个额外的延迟:

const char topic[13] = "prototype001"; // goes to the stack
char jsonObjChar [256] = ''; // set it large enough goes to the stack not heap!
char numBuffer [16] = ''; //tmpBuffer for conversion of ints to char
void loop(){
if (!client.connected()) {
reconnect();
}

strcpy (jsonObj, "{");    // Initialize/clear char by using strcpy
strcat(jsonObj, ""deviceId":"123456"");   // strcat append
strcat(jsonObj,",");
strcat(jsonObj,""messageType":"ambientSensorReading"");
strcat(jsonObj,",");
strcat(jsonObj,""temperature":"");
// conversion only needed if temperature is an int, if its char use strcat(jsonObj,readDHTTemperature());
itoa (readDHTTemperature(), numBuffer, 10); // Converts an int to a char array 
strcat(jsonObj,numbuffer);
strcat(jsonObj,""");
strcat(jsonObj,",");
strcat(jsonObj,""humidity":"");
// if already char use strcat(jsonObj,readDHTHumidity());
itoa (readDHTHumidity(), numBuffer, 10);
strcat(jsonObj,numbuffer);
strcat(jsonObj,""");
strcat(jsonObj,",");
strcat(jsonObj,""light":"");
// if char strcat(jsonObj,readLDRLight());
itoa (readLDRLight(),numBuffer. 10);
strcat(jsonObj,numbuffer);
strcat(jsonObj,""}");
delay(1000);

Serial.println("PREPARED");  
Serial.println(jsonObjChar);  
client.publish(topic, "inside loop");
delay(1000); // for test onl<
client.publish(topic, jsonObjChar);
// ... and resubscribe
client.subscribe("prototype001");
delay(5000);
client.loop();
}

如果有效的话,那就好了。如果没有,下一个调试步骤将是查看服务器上接收到的内容。

最新更新