简体   繁体   English

使用ESP8266在mqtt发布期间发生异常

[英]Exception during mqtt publish using ESP8266

I am publishing sensor readings to thingSpeak using its mqttAPI. 我正在使用其mqttAPI将传感器读数发布到thingSpeak。 I have deployed my project on Huzzah esp8266 board. 我已经在Huzzah esp8266开发板上部署了我的项目。 After just publishing one value, I'm getting ESP exception 9, 28. I guess I'm having problem with loop() function. 在发布一个值之后,我得到了ESP异常9、28。我想我在loop()函数方面遇到了问题。 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. 我已经订阅了该主题,它给出了正确的结果,甚至在reconnectMQTT()函数中进行了发布。

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. 您要声明topicLength字节的缓冲区,然后将topicLength + 1个字节复制到其中。 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. 您需要将其声明为topicLength + 1个字节长,以便为C语言\\0字符串终止符留出空间。 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. 稍后对dataBuffer以及pubTopicmessageBuffer和将String转换为char数组的任何其他位置也是如此。

Also, note that your line 另外,请注意您的行

subTopic.toCharArray(subTopicBuffer,subTopicLength); subTopic.toCharArray(subTopicBuffer,subTopicLength);

doesn't add 1 to the length where you do add 1 to the length everywhere else. 不会将长度加1,而在其他任何地方都将长度加1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM