Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.89k stars 556 forks source link

Error messages with ESP32 usage #473

Open friedemannm opened 5 years ago

friedemannm commented 5 years ago

Hi!

I'm using this library since a while on the ESP8266 where it worked like a charm! Now I switched to the ESP32 with the following warnings:

[E][WiFiClient.cpp:261] setSocketOption(): 1006 : 9 [E][WiFiClient.cpp:282] setOption(): fail on fd -1, errno: 9, "Bad file number"

Besides this, we have connection problems. There are random disconnects from time to time. Does anybody has an Idea where to start debugging this?

Please let me know If further informations are needed.

Thanks in advance, Friedemann

Links2004 commented 5 years ago

Hi,

what ESP8266 core version do you use? the lines in you error message do not line up with the latest version: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/WiFiClient.cpp#L261

may enable the build in debug messages of the lib via the IDE menu to get more context to when the error happens.

friedemannm commented 5 years ago

Thanks a lot for the fast reply! There was a typo in my post - I switched from ESP8266 to ESP32. I think it makes more sense now ;) Sorry for that... The warning comes up while connecting via SSL to a websocket server. I'm using the SHA Hash to check the server certificate.

Links2004 commented 5 years ago

Does that only happen with a SSL connection?

friedemannm commented 5 years ago

Hi! Sorry for late reply. Yes, the warning only appears when using SSL. The connection loss problem is incredibly unpredictable. For now it could also be a hardware issue related to the current supply... How to enable the debug messages you mentioned in vs code with platformio?

Links2004 commented 5 years ago

for platformio you need to set the defines in your platformio.ini

something like this:

build_flags = -D DEBUG_ESP_PORT=Serial

but since its a SSL only problem its more likely the SSL Implementation then the WebSockets code, since SSL and non SSL share the same code for the data handling. but with the messages we may know more.

if I remember correct then the ESP32 has a code_debug level that gets you more Internal logging too, not sure if it helps in this case.

so may use

build_flags = -D DEBUG_ESP_PORT=Serial -DESP32 -DCORE_DEBUG_LEVEL=5
friedemannm commented 5 years ago

Hey!

Sorry for the long delay - Here are the logs of such a disconnect scenario. In meanwhile I found out that it isn't only a ESP32 problem. The logs posted below come from an 8266. For me it's not too clear what exactly happened. Obviously a timeout somewhere... But what can I do to avoid this?

Thank you very much and alle the best, Friedemann

Message received: ping
[WS][0][sendFrame] ------- send message frame -------
[WS][0][sendFrame] fin: 1 opCode: 2 mask: 1 length: 18 headerToPayload: 0
[WS][0][sendFrame] pack to one TCP package...
[write] n: zu t: 24
[WS][0][sendFrame] sending Frame Done (16144us).
[WS][0][sendFrame] ------- send message frame -------
[WS][0][sendFrame] fin: 1 opCode: 2 mask: 1 length: 73 headerToPayload: 0
[WS][0][sendFrame] pack to one TCP package...
[write] n: zu t: 79
[WS][0][sendFrame] sending Frame Done (15244us).
[WS][0][handleWebsocketWaitFor] size: 2 cWsRXsize: 0
[readCb] n: zu t: 2
[WS][0][handleWebsocketWaitFor][readCb] size: 2 ok: 1
[WS][0][handleWebsocket] ------- read massage frame -------
[WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0  opCode: 2
[WS][0][handleWebsocket] mask: 0 payloadLen: 25
[readCb] n: zu t: 25
Message received: ACK
Ack.
bcn_timout,ap_probe_send_start
ap_probe_send over, rest wifi status to disassoc
state: 5 -> 0 (1)
rm 0
pm close 7
[WS-Client] connection lost.
[WS-Client] client disconnected.
friedemannm commented 5 years ago

Again a disconnect - this time with another debug message...

WS][0][sendFrame] ------- send message frame -------
[WS][0][sendFrame] fin: 1 opCode: 2 mask: 1 length: 73 headerToPayload: 0
[WS][0][sendFrame] pack to one TCP package...
[write] n: zu t: 79
[WS][0][sendFrame] sending Frame Done (14810us).
state: 5 -> 2 (7c0)
rm 0
pm close 7
[WS-Client] connection lost.
[WS-Client] client disconnected.
Links2004 commented 5 years ago

the

state: 5 -> 2

and

state: 5 -> 0 (1)

are WiFi state changes so you WiFi connection is instabile, its not a TCP or Websocket problem we are looking at here.

if possible you can try to use a other AP / Router.

may the WiFi.onStationModeDisconnected helps you to debug this since it can provide you with a reason for a disconnect.

https://github.com/esp8266/Arduino/blob/74819a763bfb6e9890a57411dcea4aba221a778d/doc/esp8266wifi/generic-examples.rst

https://github.com/esp8266/Arduino/blob/273f4000f0dcb936e457cba3e71d824a7dfb9007/libraries/ESP8266WiFi/src/ESP8266WiFiType.h#L68-L99

https://github.com/esp8266/Arduino/blob/273f4000f0dcb936e457cba3e71d824a7dfb9007/libraries/ESP8266WiFi/src/ESP8266WiFiType.h#L114-L119

a-silent88 commented 5 years ago

hi, you solved the problem [E][WiFiClient.cpp:261] setSocketOption(): 1006 : 9?

friedemannm commented 5 years ago

Hey! I'm still not sure if its really a problem or just a warning. But the answer is no. I'm still getting this warning. Do you also have problems with random reconnects?

a-silent88 commented 5 years ago

No, I don't have problems with reconnects, my esp32 just freezes :-))) Thank you for answer.

a-silent88 commented 5 years ago

Try using class instance WiFiClientSecure, my error is gone. (http://www.iotsharing.com/2017/08/how-to-use-https-in-arduino-esp32.html) Sorry for my english

a-silent88 commented 5 years ago

My esp32 restarts if I tried to go beyond the heap limit char array[10]; char i = array[10]; or something like that uint8_t j = 1000;

fcojacuna commented 2 years ago

I know its kinda late, but I had the same error. The issue was that I was trying to configure the connection before it started, for example if you call .setTimeout() before.begin initializes the connection. The error will always appear.