NodeMCU(ESP8266)客户端断开连接时出现异常28



我在nodemcu v3(ESP8266(上有一个websocket服务器和一个用Qt制作的websocket客户端。当客户端断开连接时,服务器会给出异常28,我不知道为什么。

这是使用ESP异常解码器解码的堆栈。

ecoding stack results
0x4020b7f8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c     line 180
0x4020b7f8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c     line 180
0x40207851: _printf_common at ../../../.././newlib/libc/stdio/nano-    vfprintf_i.c line 94
0x4020b7f8: __ssputs_r at ../../../.././newlib/libc/stdio/nano-vfprintf.c     line 180
0x40207c0c: _printf_i at ../../../.././newlib/libc/stdio/nano-    vfprintf_i.c line 241
0x4020baa0: _svfprintf_r at ../../../.././newlib/libc/stdio/nano-    vfprintf.c line 531
0x4020d151: glue2esp_linkoutput at glue-esp/lwip-esp.c line 299
0x4020bc58: _svfprintf_r at ../../../.././newlib/libc/stdio/nano-    vfprintf.c line 641
0x4020d4a6: new_linkoutput at glue-lwip/lwip-git.c line 259
0x40213a64: etharp_output_LWIP2 at core/ipv4/etharp.c line 882
0x4020453c: WebSockets::handleWebsocketWaitFor(WSclient_t*, unsigned int)     at C:UsersaurelOneDriveDocumentsArduinolibrariesarduinoWebSockets-    mastersrcWebSockets.cpp line 296
0x40209639: _vsnprintf_r at ../../../.././newlib/libc/stdio/vsnprintf.c     line 73
0x40209639: _vsnprintf_r at ../../../.././newlib/libc/stdio/vsnprintf.c     line 73
0x4020967c: vsnprintf at ../../../.././newlib/libc/stdio/vsnprintf.c line     42
0x40205a68: Print::printf(char const*, ...) at C:UsersaurelAppData    LocalArduino15packagesesp8266hardwareesp82662.4.2coresesp8266    Print.cpp line 63
0x402028d0: webSocketEvent(unsigned char, WStype_t, unsigned char*,     unsigned int) at D:WorkspaceArduinoarduino-health-care-systemArduino    WebSocketServerApp/WebSocketServerApp.ino line 69
0x402028fc: webSocketEvent(unsigned char, WStype_t, unsigned char*,     unsigned int) at D:WorkspaceArduinoarduino-health-care-systemArduino    WebSocketServerApp/WebSocketServerApp.ino line 71
0x4010020c: _umm_free at C:UsersaurelAppDataLocalArduino15packages    esp8266hardwareesp82662.4.2coresesp8266umm_mallocumm_malloc.c line     1295
0x401006dc: free at C:UsersaurelAppDataLocalArduino15packages    esp8266hardwareesp82662.4.2coresesp8266umm_mallocumm_malloc.c line     1755
0x4010020c: _umm_free at C:UsersaurelAppDataLocalArduino15packages    esp8266hardwareesp82662.4.2coresesp8266umm_mallocumm_malloc.c line     1295
0x402068a4: std::_Function_handler ::_M_invoke(std::_Any_data const&,     unsigned char, WStype_t, unsigned char*, unsigned int) at c:usersaurel    appdatalocalarduino15packagesesp8266toolsxtensa-lx106-elf-    gcc1.20.0-26-gb404fb9-2xtensa-lx106-elfincludec++4.8.2/functional line     2073
0x40206b04: WebSocketsServer::runCbEvent(unsigned char, WStype_t,     unsigned char*, unsigned int) at C:UsersaurelOneDriveDocumentsArduino    librariesarduinoWebSockets-mastersrc/WebSocketsServer.h line 182
0x402048c6: WebSocketsServer::clientDisconnect(WSclient_t*) at C:Users    aurelOneDriveDocumentsArduinolibrariesarduinoWebSockets-master    srcWebSocketsServer.cpp line 585
0x40206b41: WebSocketsServer::clientIsConnected(WSclient_t*) at C:Users    aurelOneDriveDocumentsArduinolibrariesarduinoWebSockets-master    srcWebSocketsServer.cpp line 611
0x40205371: WebSocketsServer::handleClientData() at C:Usersaurel    OneDriveDocumentsArduinolibrariesarduinoWebSockets-master    srcWebSocketsServer.cpp line 671
0x402053fd: WebSocketsServer::loop() at C:UsersaurelOneDriveDocuments    ArduinolibrariesarduinoWebSockets-mastersrcWebSocketsServer.cpp line 135
0x40202b14: loop() at D:WorkspaceArduinoarduino-health-care-system    ArduinoWebSocketServerApp/WebSocketServerApp.ino line 58
0x402065d1: esp_schedule() at C:UsersaurelAppDataLocalArduino15    packagesesp8266hardwareesp82662.4.2coresesp8266core_esp8266_main.cpp     line 95
0x4020663c: loop_wrapper() at C:UsersaurelAppDataLocalArduino15    packagesesp8266hardwareesp82662.4.2coresesp8266core_esp8266_main.cpp     line 125

这是nodemcu上的代码。一个简单的websocket服务器,它接收串行数据并将其发送到客户端。

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <SoftwareSerial.h>
SoftwareSerial s(D6,D5);
String data;
int ledPin = D7;
const char* ssid = "blablabla";
const char* password = "blablabla";

WebSocketsServer webSocket = WebSocketsServer(81);
void setup() {
Serial.begin(9600);
s.begin(9600);
WiFi.setAutoReconnect(true);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
webSocket.begin();
webSocket.onEvent(webSocketEvent);
pinMode(ledPin, OUTPUT);    
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(ledPin, HIGH);        
} else {
digitalWrite(ledPin, LOW);
}
webSocket.loop();

if (s.available()>0)
{
data=s.readString();
webSocket.broadcastTXT(data); 
Serial.println(data);  
}

}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t     length){

if (type == WStype_TEXT){
for(int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
s.print(String((char *)payload));
Serial.println();     
}
}

我用这个替换了代码,现在它可以工作了。

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
#include <Hash.h>
// Replace with your network credentials
const char* ssid = "";
const char* password = "";
WebSocketsServer webSocket = WebSocketsServer(81);
ESP8266WebServer server(80);   //instantiate server at port 80 (http port)
String page = "";
int LEDPin = 13;
void setup(void){
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
delay(1000);
Serial.begin(9600);
WiFi.begin(ssid, password); //begin WiFi connection
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
Serial.println("Web server started!");
}
void loop(void){
webSocket.loop();
server.handleClient();
if (Serial.available() > 0){
char c[] = {(char)Serial.read()};
webSocket.broadcastTXT(c, sizeof(c));
}
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){
if (type == WStype_TEXT){
for(int i = 0; i < length; i++) Serial.print((char) payload[i]);
Serial.println();
}
if(type == WStype_CONNECTED)
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %srn", num,     ip[0], ip[1], ip[2], ip[3], payload);    
}
}

最新更新