Search code examples
arduinomqttesp8266arduino-esp8266

Exception during mqtt publish using ESP8266


I am publishing sensor readings to thingSpeak using its mqttAPI. I have deployed my project on Huzzah esp8266 board. After just publishing one value, I'm getting ESP exception 9, 28. I guess I'm having problem with loop() function. Here's my code

void loop() {

    if(!mqttCli.connected()){
        reconnectMQTT();  
      }
      mqttCli.loop();

      if(millis()-mqttTimer>mqttInterval){
          taskWiFiCallback();
        }

       }
void taskWiFiCallback(){
   Serial.println("task WiFi Started");
    if(!mqttCli.connected()){
        Serial.println("mqtt not connected");
        reconnectMQTT();
        return;
      }

   Serial.println("mqtt Connection established");
   Serial.println("State:" + mqttCli.connected());

   String topicString ="channels/"+String(channelID)+"/publish/"+String(writeAPIKey);
   int topicLength = topicString.length();
   char topicBuffer[topicLength];
   topicString.toCharArray(topicBuffer,topicLength+1);

   Serial.println(topicBuffer);

   String dataString  = String("field1="+ String(tempC,1) + "&field2=" + String(tempF,1) + "&field3=" + String(humid,1));
   int dataLength = dataString.length();
   char dataBuffer[dataLength];
   dataString.toCharArray(dataBuffer,dataLength+1);
   Serial.println(dataBuffer);
   Serial.println(mqttCli.publish(topicBuffer,dataBuffer) ? "Published" : "Not Published Bawa, Do Something");

   mqttTimer = millis();


 }

void reconnectMQTT(){
 Serial.println("setting up mqtt");
 WiFiClient wifiClient;
 mqttCli.setServer(mqtt_server,1883);
 mqttCli.setClient(wifiClient);
 mqttCli.setCallback(&callback);

 // Creating a random client ID
 String clientId = "ESP8266Client-";
 clientId += String(random(0xffff), HEX);

 while(!mqttCli.connected()){
    if(mqttCli.connect(clientId.c_str())){
    String subTopic = String("channels/"+ String(channelID) + 
    "/subscribe/json/" + String(readAPIKey));
    int subTopicLength = subTopic.length();
    char subTopicBuffer[subTopicLength];
    subTopic.toCharArray(subTopicBuffer,subTopicLength);

    String pubTopic ="channels/" + String( channelID ) + 
    "/publish/"+String(writeAPIKey);
    char pubTopicBuffer[pubTopic.length()];
    pubTopic.toCharArray(pubTopicBuffer,pubTopic.length()+1);

    String message = String("field1=" + String(tempC,1) + "&field2=" + 
    String(tempF,1) + "&field3=" + String(humid,1));
    char messageBuffer[message.length()];
    message.toCharArray(messageBuffer,message.length()+ 1);
    Serial.println(messageBuffer);
    Serial.println(mqttCli.publish(pubTopicBuffer,messageBuffer) ? "Published" : "Unpublished");
    Serial.println(mqttCli.subscribe(subTopicBuffer) ? "Subscribed" : "Unsubscribed"); 

  }else{
       Serial.print("failed, rc=");
      Serial.println(mqttCli.state());
      delay(1000);
    }
}

}

I have Subscribed to the topic and it is giving correct results and even publish in reconnectMQTT() function is being published.


Solution

  • There may be other problems, but to start:

    int topicLength = topicString.length();
    char topicBuffer[topicLength];
    topicString.toCharArray(topicBuffer,topicLength+1);
    

    You're declaring a buffer of topicLength bytes and then copying topicLength + 1 bytes into it. You'll always overrun the buffer by one byte. You need to declare it to be topicLength + 1 bytes long in order to have room for the C language \0 string terminator. So:

    int topicLength = topicString.length();
    char topicBuffer[topicLength+1];
    topicString.toCharArray(topicBuffer,topicLength+1);
    

    or, better:

    int topicLength = topicString.length()+1;
    char topicBuffer[topicLength];
    topicString.toCharArray(topicBuffer,topicLength);
    

    Same thing for dataBuffer later as well as pubTopic, messageBuffer and any other place that you turn a String into a char array.

    Also, note that your line

    subTopic.toCharArray(subTopicBuffer,subTopicLength);

    doesn't add 1 to the length where you do add 1 to the length everywhere else.