当 PubSubClient 设置 mqttserver 时 ESP8266 崩溃
ESP8266 crashes when PubSubClient set mqttserver
我正在使用 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" 不是 运行 时,它仍然会崩溃。
可能是什么问题?
如果你的算法是对的。请检查指针......并移动其他文件。
似乎是 Arduino 的 bug IDE,当我将这个函数、代码复制到其他文件时。
有效。
我正在使用 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" 不是 运行 时,它仍然会崩溃。
可能是什么问题?
如果你的算法是对的。请检查指针......并移动其他文件。
似乎是 Arduino 的 bug IDE,当我将这个函数、代码复制到其他文件时。
有效。