当PubSubClient设置mqttserver时,ESP8266崩溃

时间:2017-06-18 08:16:43

标签: mqtt esp8266 arduino-ide arduino-esp8266

我使用的是使用pubsubclient库控制的NodeMCU(ESP12E)。 当我打电话给client.setServer(mqtt_server, 1883);时,NodeMCU崩溃了 消息:

Exception (28):
    epc1=0x4000bf80 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000096c depc=0x00000000

似乎setServer操作与ESP8266WebServer冲突。
我试过了:this但问题没有解决。
这是我的代码:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>

// Khởi tạo biến toàn cục
const char* mqtt_server = "iot.eclipse.org";

WiFiClient espClient;
PubSubClient client;
ESP8266WebServer *server;
void setup() {
  EEPROM.begin(512);
  Serial.begin(9600);
  //I tried this
  server = new (ESP8266WebServer);
  //Check wifi, if failed -> run as web server
  if (!setup_wifi()) {
    wifiStartServerMode();
  }

  //.....Some function to get username and password from EEPROM

  // Call back-end using new local WiFiClient
  //  String serverResponse = registerDevice(mac, username, userpassword, 3);
  //If ok, serser response this:
  String serverResponse = "null;null;root/phan/abc";
  if (serverResponse.indexOf(';') > 0) {
    Serial.println("start mqtt client");
    client.setClient(espClient);

    ////////////////
    //CRASHED HERE//
    ///////////////
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
    String tempTopic = getValue(serverResponse, ';', 2);
    for (int i = 0; i < tempTopic.length(); i++) {
      topic += tempTopic[i];
    }
    //Đăng ký nhận tín hiệu
    client.subscribe(topic);
    errConnect2BackEnd = false;
    stConnectBroker = true;
  } else if (serverResponse.equals("failed")) {//Không thể kết nối server
    Serial.println("Can't connect to server");
    //TODO: Báo lỗi, không kết nối server
  } else {
    Serial.print("Error: ");
    Serial.println(serverResponse);
    //TODO: Nhập sai thông tin đăng nhập -> reset để nhập lại (nhấn nút, xóa EEPROM)
  }
  Serial.println("End of setup");

}

功能开始在循环中运行服务器并在完成后停止

  //-> run as web server

  void wifiStartServerMode() {
  //Active AP_ST_MODE
  setupAP_STA_Mode();
  //Run server->handleClient(); in loop
  viewRunConfig();
  server->stop();
  server->close();
  delay(100);
  delete(server);
  setup_Station_Mode();
}

当函数“wifiStartServerMode”未运行时,它仍会崩溃。

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

如果你的算法是正确的。请检查指针.....并移动其他文件。
这似乎是Arduino IDE的bug,当我复制这个函数时,代码到其他文件。
它有效。