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

NodeMCU (ESP8266) Exception 28 when a client disconnect

我在 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:\Users\aurel\OneDrive\Documents\Arduino\libraries\arduinoWebSockets-    master\src\WebSockets.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:\Users\aurel\AppData    \Local\Arduino15\packages\esp8266\hardware\esp8266.4.2\cores\esp8266    \Print.cpp line 63
0x402028d0: webSocketEvent(unsigned char, WStype_t, unsigned char*,     unsigned int) at D:\Workspace\Arduino\arduino-health-care-system\Arduino    \WebSocketServerApp/WebSocketServerApp.ino line 69
0x402028fc: webSocketEvent(unsigned char, WStype_t, unsigned char*,     unsigned int) at D:\Workspace\Arduino\arduino-health-care-system\Arduino    \WebSocketServerApp/WebSocketServerApp.ino line 71
0x4010020c: _umm_free at C:\Users\aurel\AppData\Local\Arduino15\packages    \esp8266\hardware\esp8266.4.2\cores\esp8266\umm_malloc\umm_malloc.c line     1295
0x401006dc: free at C:\Users\aurel\AppData\Local\Arduino15\packages    \esp8266\hardware\esp8266.4.2\cores\esp8266\umm_malloc\umm_malloc.c line     1755
0x4010020c: _umm_free at C:\Users\aurel\AppData\Local\Arduino15\packages    \esp8266\hardware\esp8266.4.2\cores\esp8266\umm_malloc\umm_malloc.c line     1295
0x402068a4: std::_Function_handler ::_M_invoke(std::_Any_data const&,     unsigned char, WStype_t, unsigned char*, unsigned int) at c:\users\aurel    \appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-    gcc.20.0-26-gb404fb9-2\xtensa-lx106-elf\include\c++.8.2/functional line     2073
0x40206b04: WebSocketsServer::runCbEvent(unsigned char, WStype_t,     unsigned char*, unsigned int) at C:\Users\aurel\OneDrive\Documents\Arduino    \libraries\arduinoWebSockets-master\src/WebSocketsServer.h line 182
0x402048c6: WebSocketsServer::clientDisconnect(WSclient_t*) at C:\Users    \aurel\OneDrive\Documents\Arduino\libraries\arduinoWebSockets-master    \src\WebSocketsServer.cpp line 585
0x40206b41: WebSocketsServer::clientIsConnected(WSclient_t*) at C:\Users    \aurel\OneDrive\Documents\Arduino\libraries\arduinoWebSockets-master    \src\WebSocketsServer.cpp line 611
0x40205371: WebSocketsServer::handleClientData() at C:\Users\aurel    \OneDrive\Documents\Arduino\libraries\arduinoWebSockets-master    \src\WebSocketsServer.cpp line 671
0x402053fd: WebSocketsServer::loop() at C:\Users\aurel\OneDrive\Documents    \Arduino\libraries\arduinoWebSockets-master\src\WebSocketsServer.cpp line 135
0x40202b14: loop() at D:\Workspace\Arduino\arduino-health-care-system    \Arduino\WebSocketServerApp/WebSocketServerApp.ino line 58
0x402065d1: esp_schedule() at C:\Users\aurel\AppData\Local\Arduino15    \packages\esp8266\hardware\esp8266.4.2\cores\esp8266\core_esp8266_main.cpp     line 95
0x4020663c: loop_wrapper() at C:\Users\aurel\AppData\Local\Arduino15    \packages\esp8266\hardware\esp8266.4.2\cores\esp8266\core_esp8266_main.cpp     line 125

这是 nodemcu.A 简单的 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: %s\r\n", num,     ip[0], ip[1], ip[2], ip[3], payload);    
            }
}