ESP32 Arduino PubSub 发布变量字符数组错误?

ESP32 Arduino PubSub publish error for variable char array?

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

在下面的循环中,我注意到我的 "Inside loop" 已发布,但是,变量 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(大到足以接受最大的消息)和帮助程序(tmp 字符)用于转换和其他事情)不要在循环中定义 const 字符,在设置之前执行它,以便编译器将它们放在堆栈中并且不使用堆在运行时。我根据这个原则更改了您的代码,并在消息之间添加了额外的延迟:

const char topic[13] = "prototype001"; // goes to the stack
char jsonObjChar [256] = '[=10=]'; // set it large enough goes to the stack not heap!
char numBuffer [16] = '[=10=]'; //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();

}

如果有效 - 很好。如果不是,下一个调试步骤将是查看服务器接收到的内容。