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

问题描述 投票:0回答:1

我正在用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。

任何帮助将不胜感激!!

arduino mqtt publish-subscribe esp32
1个回答
0
投票

尝试摆脱字符串,它们会破坏您的堆并导致崩溃。定义全局固定的charBuffers(足够大以容纳最大的消息)和helper(tmp字符)以进行转换和其他操作)不要在循环中定义const字符,而是在设置之前进行操作,因此编译器将它们放置在堆栈中并且不使用堆在运行时。我将您的代码更改为此原则,并在消息之间增加了额外的延迟:

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

}

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

© www.soinside.com 2019 - 2024. All rights reserved.