[SOLVED] Serial.print prints "0 : 00" in the callback function of PubSubClient

Hi all,

I have PubSubClient sending and receiving MQTT messages in various programs. While trying to debug a crash, I’ve run into instances where Serial.print prints "“0 : 00” instead of what I was expecting. I suspect this is related to my crash. Can anyone give me a hint as to what I need to investigate (e.g., memory issue, etc.)?

When I run

// Function called when an MQTT message arrives on a subscribed channel. Here it switches the solenoid valves.
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print(", time: ");
  Serial.print(millis());
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  // Convert payload to json if topic is one of the valves.
  if (strncmp(topic, "garden/valvex", 12) == 0) {
    StaticJsonBuffer<jsonSize> jsonBuffer;
    JsonObject&  jsonPayload = jsonBuffer.parseObject(payload);
    const char * mqttValve = jsonPayload["valve"];
    int mqttValue = jsonPayload["value"];
    int mqttPin = 0;
    Serial.println("got here 2");
  }
}

I get, as expected:

Message arrived [garden/valve1, time: 24948] {"valve": "In1", "value": 0}
got here 2

However, if I add “Serial.println(“got here 1”);” (the only difference between the two codes)

// Function called when an MQTT message arrives on a subscribed channel. Here it switches the solenoid valves.
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print(", time: ");
  Serial.print(millis());
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  // Convert payload to json if topic is one of the valves.
  if (strncmp(topic, "garden/valvex", 12) == 0) {
    Serial.println("got here 1");
    StaticJsonBuffer<jsonSize> jsonBuffer;
    JsonObject&  jsonPayload = jsonBuffer.parseObject(payload);
    const char * mqttValve = jsonPayload["valve"];
    int mqttValue = jsonPayload["value"];
    int mqttPin = 0;
    Serial.println("got here 2");
  }
}

I get:

Message arrived [garden/valve1, time: 26969] {"valve": "In1", "value": 0}
0 : 00
got here 2

I can put Serial.prints in various places, or even comment out some of the strncmp lines, and I get "0 : 00"s in reproducible places. I can’t find a pattern, though.

And ideas welcome.

Regards,
Sean

So the first code works and the second code didn't? If that's the case can you point out what you add/remove/modify from the second code?

The only difference is that I add the line "Serial.println("got here 1");" to the second code.

One problem I can see is that you create jason object locally, which I don't think you should do. Because jason object allocate a lot of dynamic memory.

arduino_new:
One problem I can see is that you create jason object locally, which I don't think you should do. Because jason object allocate a lot of dynamic memory.

Thanks new, it seems like you are right: if I comment out all json the problem goes away. I think I made it local because I use it twice: once for MQTT callback, and once for publish. I'll have to google a bit to find out how best to create them.

Regards,
Sean

In case anyone else searches here, it doesn't seem that it has to do with where I created the json object (all the examples I found create it in the same place; I believe because it needs instances in the send and receive parts of MQTT), but I do believe it has to do with memory. When I comment out loading my lcd display from the main loop, the problem disappears.

Also, I have moved the code from the ESP8266 to the ESP32, which has significantly more RAM. Now everything runs fine. Almost certainly a memory issue.

"garden/valvex"

Even with my fat fingers, I can count that this is 13 characters.

  if (strncmp(topic, "garden/valvex", 12) == 0) {

So, why 12 here?