Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 panics when ArduinoWebsockets initiates a websocket connection #10

Closed
lalten opened this issue May 20, 2019 · 11 comments
Closed

ESP32 panics when ArduinoWebsockets initiates a websocket connection #10

lalten opened this issue May 20, 2019 · 11 comments
Assignees
Labels
resolved the issue was resolved usage queshtion Queshtions about using the library, its interface and so on

Comments

@lalten
Copy link

lalten commented May 20, 2019

My ESP32 panics when ArduinoWebsockets initiates a websocket connection.

Code running on ESP32:

#include <Arduino.h>
#include <WiFiManager.h>
#include <ArduinoWebsockets.h>

WiFiManager wifiManager;

using namespace websockets;
WebsocketsClient client;

static const char SERVER[] = "ws://192.168.1.149"; // my laptop's IP

void onMessageCallback(WebsocketsMessage message) {
    Serial.print("Got Message: ");
    Serial.println(message.data());
}

void onEventsCallback(WebsocketsEvent event, String data) {
    if(event == WebsocketsEvent::ConnectionOpened) {
        Serial.println("Connnection Opened");
    } else if(event == WebsocketsEvent::ConnectionClosed) {
        Serial.println("Connnection Closed");
    } else if(event == WebsocketsEvent::GotPing) {
        Serial.println("Got a Ping!");
    } else if(event == WebsocketsEvent::GotPong) {
        Serial.println("Got a Pong!");
    }
}

void setup()
{
  Serial.begin(115200);
  wifiManager.autoConnect();

  // Setup Callbacks
  client.onMessage(onMessageCallback);
  client.onEvent(onEventsCallback);
  client.connect(SERVER);

  client.send("Hello");
  client.ping();
}

void loop()
{
  client.send(String(millis()));
  client.poll();
  delay(500);
}

On my laptop, I can see an incoming connection:

$ sudo nc -l 80 -vvvv
Listening on [0.0.0.0] (family 0, port 80)
Connection from espressif 62507 received!
GET / HTTP/1.1
Host: 192.168.1.149
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: MDEyMzQ1Njc4OWFiY2RlZg==
Sec-WebSocket-Version: 13

But when I use websocat's server:

sudo websocat -s 80 -vvvv
Listening on ws://127.0.0.1:80/
[INFO  websocat::lints] Auto-inserting the line mode
[DEBUG websocat] Done third phase of interpreting options.
[DEBUG websocat] Done fourth phase of interpreting options.
[DEBUG websocat] Preparation done. Now actually starting.
[INFO  websocat::sessionserve] Serving Message2Line(WsServer(TcpListen(V4(127.0.0.1:80)))) to BroadcastReuser(Line2Message(Stdio)) with Options { websocket_text_mode: true, websocket_protocol: None, websocket_reply_protocol: None, udp_oneshot_mode: false, unidirectional: false, unidirectional_reverse: false, exit_on_eof: true, oneshot: false, unlink_unix_socket: false, exec_args: [], ws_c_uri: "ws://0.0.0.0/", linemode_strip_newlines: false, linemode_strict: false, origin: None, custom_headers: [], custom_reply_headers: [], websocket_version: None, websocket_dont_close: false, one_message: false, no_auto_linemode: false, buffer_size: 65536, broadcast_queue_len: 16, read_debt_handling: Warn, linemode_zero_terminated: false, restrict_uri: None, serve_static_files: [], exec_set_env: false, reuser_send_zero_msg_on_disconnect: false, process_zero_sighup: false, process_exit_sighup: false, socks_destination: None, auto_socks5: None, socks5_bind_script: None, tls_domain: None, tls_insecure: false, max_parallel_conns: None, ws_ping_interval: None, ws_ping_timeout: None }

The ESP32 will die:

*WM: [1] AutoConnect 
*WM: [2] ESP32 event handler enabled 
*WM: [2] Connecting as wifi client... 
*WM: [1] STA static IP:
*WM: [2] setSTAConfig static ip not set 
*WM: [3] WIFI station disconnect 
*WM: [1] Connecting to saved AP: XXXXXXXX
*WM: [3] Using Password: XXXXXXXX
*WM: [3] WiFi station enable 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] AutoConnect: SUCCESS 
*WM: [1] STA IP Address: 192.168.1.29
[E][WiFiClient.cpp:282] setOption(): fail on fd 54, errno: 22, "Invalid argument"
[E][WiFiClient.cpp:365] write(): fail on fd 54, errno: 104, "Connection reset by peer"
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4012d2c0  PS      : 0x00060830  A0      : 0x8012d721  A1      : 0x3ffb1b90  
A2      : 0x00000000  A3      : 0x3ffb1bef  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x00000000  A7      : 0xffffffa7  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x3ffc8dcc  A12     : 0x00000000  A13     : 0x00000001  
A14     : 0x00060e20  A15     : 0x00000000  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000008  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x4012d2c0:0x3ffb1b90 0x4012d71e:0x3ffb1bb0 0x40173692:0x3ffb1be0 0x400dc65b:0x3ffb1c10 0x400dc2e6:0x3ffb1c30 0x400dcefa:0x3ffb1d70 0x400d20ad:0x3ffb1f60 0x400e0abf:0x3ffb1fb0 0x4008b015:0x3ffb1fd0

Rebooting...

@lalten
Copy link
Author

lalten commented May 20, 2019

Interestingly, it works with wscat:

sudo wscat -l 80
listening on port 80 (press CTRL+C to quit)
client connected
< Hello
< 1243
< 1750
< 2257
< 2763
< 3268
< 3773
< 4278
< 4783
< 5289
< 5795

But even if ArduinoWebSockets can't properly connect, it should not panic the kernel.

Edit: s/should/should not/

@gilmaimon gilmaimon added the bug Something isn't working label May 21, 2019
@gilmaimon
Copy link
Owner

gilmaimon commented May 21, 2019

Thank you for the detailed issue. From a glance this seems to be like some bad error handling on my side.
I've labled this issue a bug and will try to re-produce and fix it in a few days.
Meanwhile, it will save me some time if you could:

  • Make sure to are using the latest version of the library (0.4.1)
  • Check if using an esp8266 with the same code and setup works

Thank you

@gilmaimon gilmaimon reopened this May 21, 2019
@lalten
Copy link
Author

lalten commented May 21, 2019

Hi Gil,
lib version is current master.
platformio.ini:

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino

upload_speed = 921600
monitor_speed = 115200

lib_deps =  https://github.com/tzapu/WiFiManager.git#development
            https://github.com/gilmaimon/ArduinoWebsockets

With an ESP8266, same code:

[env:huzzah]
platform = espressif8266
board = huzzah
framework = arduino

upload_speed = 921600
monitor_speed = 115200

lib_deps =  https://github.com/tzapu/WiFiManager.git#development
            https://github.com/gilmaimon/ArduinoWebsockets

Output with sudo wscat -l 80:

*WM: [1] AutoConnect 
*WM: [2] Connecting as wifi client... 
*WM: [1] STA static IP:
*WM: [2] setSTAConfig static ip not set 
*WM: [3] WIFI station disconnect 
*WM: [1] Connecting to saved AP: XXXXXXXX
*WM: [3] Using Password: XXXXXXXX
*WM: [3] WiFi station enable 
*WM: [3] enableSTA PERSISTENT ON 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] AutoConnect: SUCCESS 
*WM: [1] STA IP Address: 192.168.1.92
Connnection Opened
Got a Pong!

Output with sudo websocat -s 80:

*WM: [1] AutoConnect 
*WM: [2] Connecting as wifi client... 
*WM: [1] STA static IP:
*WM: [2] setSTAConfig static ip not set 
*WM: [3] WIFI station disconnect 
*WM: [1] Connecting to saved AP: XXXXXXXX
*WM: [3] Using Password: XXXXXXXX
*WM: [3] WiFi station enable 
*WM: [3] enableSTA PERSISTENT ON 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] AutoConnect: SUCCESS 
*WM: [1] STA IP Address: 192.168.1.92

So no kernel panic (but it also doesn't connect)

@gilmaimon
Copy link
Owner

Thank you @lalten, I am on it.
Few things:

  1. Try running the server using: sudo websocat -s 0.0.0.0:80 so it will be able to accept connections outside of localhost. Does it solves your problem?
  2. Panic is surely not something that supposed, so this is considered a bug either way.

@gilmaimon
Copy link
Owner

@lalten please let me know if you solved the issue.

@elielmarcos
Copy link

Hey @gilmaimon we are with the same problem, when the server turn off the ws connection the esp32 enter in panic mode and we recieve this error:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d4ea0  PS      : 0x00060d30  A0      : 0x800d4f74  A1      : 0x3ffdd8b0  
A2      : 0x00000000  A3      : 0x3ffdd8ff  A4      : 0x00000001  A5      : 0x00000001  
A6      : 0x3ffc27e8  A7      : 0x00000001  A8      : 0x00000000  A9      : 0x00000000  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3ffddf14  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x00000001  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000008  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

Backtrace: 0x400d4ea0:0x3ffdd8b0 0x400d4f71:0x3ffdd8d0 0x4014c4a6:0x3ffdd8f0 0x400d84c7:0x3ffdd920 0x400d7d11:0x3ffdd940 0x400d1e21:0x3ffdda80 0x400885dd:0x3ffddac0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9232
load:0x40080400,len:6400
entry 0x400806a8

I'm using the 0.4.2 version now, and with the 0.4.0 i receieve the same error.

@gilmaimon
Copy link
Owner

gilmaimon commented Jun 1, 2019 via email

@gilmaimon
Copy link
Owner

I believe I have a solution:
As you can see from the backtrace, the panic occurs inside WiFiClient which is part of the esp32's Arduino library. That means, the crash is not in my library exactly.
I've tried to reproduce this kind of crash, and I succeeded. All I had to do is to try and connect without a proper WiFi Connection setup on the device.
Please make sure that you have Wifi before calling connect! If you know connecting to a Wifi can time some time, you can try using this snippet:

for(int i = 0; i < NUM_SECONDS_TO_WAIT && WiFi.status() != WL_CONNECTED; i++) {
      Serial.print(".");
      delay(1000);
  }

I don't know how WifiManager works, but you would need to check that library's docs in order to understand how to do something once a connection is established (Using some sort of callback or blocking mechanism)

I am removing the bug tag, please let me know if you manage to fix this.

@gilmaimon gilmaimon added usage queshtion Queshtions about using the library, its interface and so on and removed bug Something isn't working labels Jun 1, 2019
@gilmaimon gilmaimon self-assigned this Jun 1, 2019
@elielmarcos
Copy link

@gilmaimon , Thank you very much for your quick solution!

Exactly, it's the WiFiClient library that's causing all the problems. I have problems when I am connected to Wi-Fi, after turning off only the WS server the ESP32 is restarted.

My solution was: reinstall esp32 on Arduino with the older version 1.0.1

The newer version (1.0.3) probably updated something from the WiFi library that is causing the problem.

Thank you very much, I hope you find another solution !!!

@gilmaimon
Copy link
Owner

gilmaimon commented Jun 1, 2019

@gilmaimon , Thank you very much for your quick solution!

Exactly, it's the WiFiClient library that's causing all the problems. I have problems when I am connected to Wi-Fi, after turning off only the WS server the ESP32 is restarted.

My solution was: reinstall esp32 on Arduino with the older version 1.0.1

The newer version (1.0.3) probably updated something from the WiFi library that is causing the problem.

Thank you very much, I hope you find another solution !!!

Awesome, glad we pinned down the issue! By the way, give the library a star if you liked it. Hopefull that way more people will use and help improve it.

@lalten, please let me know if I can close the issue 👍

@gilmaimon gilmaimon added the resolved the issue was resolved label Jun 1, 2019
@gilmaimon
Copy link
Owner

I consider this as resolved.
Please open an issue or contact me for anything else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolved the issue was resolved usage queshtion Queshtions about using the library, its interface and so on
Projects
None yet
Development

No branches or pull requests

3 participants