我正在使用其mqttAPI将传感器读数发布到thingSpeak。我已经在Huzzah esp8266开发板上部署了我的项目。在发布一个值之后,我得到了ESP异常9、28。我想我在loop()函数方面遇到了问题。这是我的代码
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);
}
}
}
我已经订阅了该主题,它给出了正确的结果,甚至在reconnectMQTT()函数中进行了发布。
答案 0 :(得分:0)
可能还有其他问题,但是要开始:
int topicLength = topicString.length();
char topicBuffer[topicLength];
topicString.toCharArray(topicBuffer,topicLength+1);
您要声明topicLength
个字节的缓冲区,然后将topicLength + 1
个字节复制到其中。您总是将缓冲区溢出一个字节。您需要声明其长度为topicLength + 1
个字节,以便为C语言\0
字符串终止符留出空间。所以:
int topicLength = topicString.length();
char topicBuffer[topicLength+1];
topicString.toCharArray(topicBuffer,topicLength+1);
或者更好:
int topicLength = topicString.length()+1;
char topicBuffer[topicLength];
topicString.toCharArray(topicBuffer,topicLength);
稍后与dataBuffer
以及pubTopic
,messageBuffer
和将String
转换为char
数组的任何其他地方相同。
另外,请注意您的行
subTopic.toCharArray(subTopicBuffer,subTopicLength);
不要将长度加1,而将其他地方的长度加1。