Links2004 / arduinoWebSockets

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

Uno R4 Wifi freezing... #909

Open leosoftGR opened 1 week ago

leosoftGR commented 1 week ago

Hello all,

I am using an Uno R4 wifi as a websockets server and I am experienced a strange issue. Whenever I reload my webpage (...containing javascript websockets client) or whenever I try to connect a second client to my arduino, this completely freezing. I thought it might be a memory issue and I tried to create an empty project for testing but same results. Of course I am using the latest 0.4.1 firmware.
Another test i did, was to install a websockets client tester plugin to chrome so, I could try to connect directly from my browser without using my buildin webpage but still the same. Also tried to reduce the WEBSOCKETS_MAX_DATA_SIZE from webshocket.h as an experiment but still no luck.

Any ideas??????

leosoftGR commented 1 week ago

According to this thread :

https://github.com/Links2004/arduinoWebSockets/issues/877

I decided to remove experimentally some small delays of 10ms that I had in my loop, and notice big improvement. I may still not be able to connect two clients at the same time, but at least the first one seems to be stable. I can even reload the page without freezing my box. And if I close it first, then I am able to re open it. With a few words, the first connected client works fine. (...at the moment)

Thanks to @furdog

leosoftGR commented 1 week ago

And according the latest reply from Markus @Links2004 regarding the reasons to avoid a delay, i would like to ask Markus if a heavy loop (from calculations or serving multiple web pages or even too many serial.prints) could have the same luck with delay command or not

Thanks for your time

Links2004 commented 1 week ago

yes any think that takes a long time can have the same affect, a delay is a do nothing for x. it makes no difference if you replace do nothing with do math ;)

what may help is to make sure that all open messages a handled before you do the heavy calculations. by calling the loop function multiple times.

a other option is to place the heavy calculations in a dedicated thread. some ESP32 for example have 2 cores which allows parallel execution (see xTaskCreatePinnedToCore). but not sure what the R4 Wifi is capable of.

leosoftGR commented 1 week ago

Parallel execution attached on a core was my fist thought but unfortunately its not available on UNO R4.

What I've noticed also is that a weak WiFi signal can also cause the same issue. In other words, if you want to have a flawlessly working websocket server, you need to keep your box as much free as possible.

Btw, something else that it maybe important... I tried to reduce WEBSOCKETS_SERVER_CLIENT_MAX from 5 to 1 and prevent my setup from crashes etc, but after compilation my Uno r4 is freezing while entering to main loop.

Links2004 commented 1 week ago

have you enabled the debug output of the websocket lib? this sounds more and more like there is a TCP / Network stack problem on the UNO R4.

if the R4 has printf support then its as easy as setting the DEBUG_ESP_PORT define to for example Serial1 https://github.com/Links2004/arduinoWebSockets/blob/7a4c416082b80dfc2c0da10731effc8d3a69abc6/src/WebSockets.h#L46-L51

if this not the case then a custom function can be called via defining DEBUG_WEBSOCKETS

leosoftGR commented 1 week ago

Unfortunately there is no printf for serial on UNO R4 :(

Links2004 commented 1 week ago

ok that makes it a bit harder to see whats going on, but may try:

 #define DEBUG_WEBSOCKETS(msg)               \ 
     {                                       \ 
         Serial1.print(msg); \ 
         Serial1.flush();             \ 
     } 

with that we can at least get a idea where in the code the problem is, no details of what happening but by searching the msg in the code we can at least get a hint.

leosoftGR commented 1 week ago

You mean the issue regarding the weak signal or regarding WEBSOCKETS_SERVER_CLIENT_MAX ?

Links2004 commented 1 week ago

we can look at both, but make sure to clearly label the logs and conversation.

leosoftGR commented 1 week ago

Its sounds maybe stupid question but since now I am using the Arduino Cloud Editor how can I set there a compiler debug switch ?

Links2004 commented 1 week ago

good question, have never used it. if you can edit the lib files then it my is easier to simply add the define to the WebSockets.h

leosoftGR commented 1 week ago

I will put WebSockets.h and WebSocketsServer.h inside my project so I will be able to edit the defines. Like this:

`#include "WebSocketsVersion.h"

define DEBUG_WEBSOCKETS

ifndef NODEBUG_WEBSOCKETS

ifdef DEBUG_ESP_PORT

define DEBUG_WEBSOCKETS(msg) \

 {                                       \ 
     Serial.print(msg); \ 
     Serial.flush();             \ 
 } 

else

// #define DEBUG_WEBSOCKETS(...) os_printf( __VA_ARGS__ )

endif

endif

ifndef DEBUG_WEBSOCKETS

define DEBUG_WEBSOCKETS(...)

ifndef NODEBUG_WEBSOCKETS

define NODEBUG_WEBSOCKETS

endif

endif

` But after compilation and uploading, no debug message coming on Serial from the library :(

Links2004 commented 1 week ago

my bad, try:

   #define DEBUG_WEBSOCKETS(msg, ...)               \ 
     {                                       \ 
         Serial1.print(msg); \ 
         Serial1.flush();             \ 
     } 
kakopappa commented 1 week ago

Unfortunately there's no printf in ArduinoCore-renesas. When i was working on R4 this is what I did to see the debug messages in the Arduino Serial Monitor.

#include <stdarg.h> // Required for va_list, va_start, va_end

.......

size_t va_printf(const char *format, ...) {
  va_list arg;
  va_start(arg, format);
  size_t ret = vprintf(format, arg);
  va_end(arg);
  return ret;
}

#define DEBUG_WEBSOCKETS(...) va_printf( VA_ARGS )
furdog commented 1 week ago

I will put WebSockets.h and WebSocketsServer.h inside my project so I will be able to edit the defines. Like this:

There so many things that could go wrong here.

If you use heavy computing that blocks other tasks for excessive amount of time, i would recomend you to use some kind of coroutines, duff devices, computed gotos, callbacks, or state machines, or just plain conditionals. It takes some effort to implement, but in single threaded applications it's always good to split monolith tasks into smaller ones, so they work very limited ammount of time.

for example:

void loop() {
  //calculation-heavy routine, takes a lot of time to execute
  for (int i = 0; i < 100000; i++) {
      some_sum += some_calculation();
  }

  //do something with some_sum...

  ...

  //other tasks

  ...
}

This "for" loop will take huge amount of time to execute, so its better to rewrite it like this:

void loop() {
  static int i = 0;

  //Just a simple conditional which takes almost no time to evaluate
  if (i < 100000) {
      some_sum += some_calculation();
      i++;
  } else {
    //do something with some_sum..
    ...

    i = 0;
  }

  //other tasks

  ...
}

These are only examples how to write efficient code on single-threaded devices.

leosoftGR commented 1 week ago

Unfortunately there's no printf in ArduinoCore-renesas. When i was working on R4 this is what I did to see the debug messages in the Arduino Serial Monitor.

I put it in the beginning of my code before loading the websocketsserver.h and here is what I got:

`/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h: In member function 'virtual void WebSocketsServerCore::handleNonWebsocketConnection(WSclient_t*)': /tmp/3819969161/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:42: error: 'VA_ARGS' was not declared in this scope

define DEBUG_WEBSOCKETS(...) va_printf( VA_ARGS )

                                      ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~ /tmp/3819969161/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:42: note: suggested alternative: '_NOARGS'

define DEBUG_WEBSOCKETS(...) va_printf( VA_ARGS )

                                      ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~ /tmp/3819969161/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:31: error: 'va_printf' was not declared in this scope

define DEBUG_WEBSOCKETS(...) va_printf( VA_ARGS )

                           ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~ /tmp/3819969161/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:31: note: suggested alternative: 'vwprintf'

define DEBUG_WEBSOCKETS(...) va_printf( VA_ARGS )

                           ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~`

Any idea????

furdog commented 1 week ago

try __VA_ARGS__ instead of VA_ARGS (github autoformats underscore signs)

leosoftGR commented 1 week ago

Thanks for your time first of all. Here is what i have then:

`/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h: In member function 'virtual void WebSocketsServerCore::handleNonWebsocketConnection(WSclient_t*)': /tmp/826602130/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:31: error: 'va_printf' was not declared in this scope

define DEBUG_WEBSOCKETS(...) va_printf(__VA_ARGS__)

                           ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~ /tmp/826602130/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:11:31: note: suggested alternative: 'vwprintf'

define DEBUG_WEBSOCKETS(...) va_printf(__VA_ARGS__)

                           ^

/home/builder/Arduino/libraries/websockets_2_6_1/src/WebSocketsServer.h:140:9: note: in expansion of macro 'DEBUG_WEBSOCKETS' DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] no Websocket connection close.\n", client->num); ^~~~ `

Do i have to change anything and in websockets.h ???

furdog commented 1 week ago

It says that va_printf is not defined. Did you paste code provided by kakopappa correctly? va_printf must be defined before use.

I suggest you to create separate .h file with all the definitions provided and include it in all .cpp .ino or in common header files at the very start. Don't forget to use #pragma once at the beggining of your .h file, so it won't cause errors when included twice.

leosoftGR commented 1 week ago

When I paste the code before #include Now I got this strange message:

/tmp/2495880276/R4WiFi_led_matrix/R4WiFi_led_matrix.ino:422:34: error: 'WStype_t' has not been declared void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){

Now cannot recognize WStype_t

Here is my code:

`#include // Required for va_list, va_start, va_end

size_t va_printf(const char *format, ...) { va_list arg; va_start(arg, format); size_t ret = vprintf(format, arg); va_end(arg); return ret; }

define DEBUG_WEBSOCKETS(...) va_printf( __ VA_ARGS __)

include

WebSocketsServer webSocket = WebSocketsServer(81);

`

furdog commented 1 week ago

You did everything right, it's just Arduino auto-prototyping messes with some definitions. It's very sneaky and odd behaviour. Tho it could be fixed with some manual prototyping, but i suggest you to move all your code from yourprojectname.ino into some separate .cpp file, then add #include <Arduino.h> at the beggining of that file. You may remain your .ino file completely empty, it won't affect anything. Auto-prototyping behaviour will not spread onto .cpp files, thus why it's the most simple solution.

leosoftGR commented 1 week ago

I created a debug.h file :

``#include // Required for va_list, va_start, va_end

size_t va_printf(const char *format, ...) { va_list arg; va_start(arg, format); size_t ret = vprintf(format, arg); va_end(arg); return ret; }

define DEBUG_WEBSOCKETS(...) va_printf( __ VA_ARGS __)

And then I managed to include it before WebSocketsServer.h :

`#include "debug.h"

include

WebSocketsServer webSocket = WebSocketsServer(81);

Now the program compiles without any issue but unfortunately nothing coming on serial monitor :(

Thanks for your time

furdog commented 1 week ago

Are you sure that arduinoWebSockets/src/WebSockets.h Includes your debug.h file? You must modify arduinoWebSockets library in order for this to work.

If you just define it inside your main project file it will not have any effect on the library behaviour and thus it will not output anything.

Sorry, I was unclear early.

My original statement brought confusion:

Make sure you include files you added inside your project and not the default library. Better remove default library, so you definitely could see if arduino uses correct files.

This is pure bullshit from me.

I meant that if you copied ALL files from arduinoWebSockets into your project, you must make sure you don't include the original library, so arduino builds your changed copy. I also missed the fact that definition of va_printf inside your main project file won't do a thing. I'm really sorry for the confusion.

To make it all easy, just fork arduinoWebSockets library and include your debug.h into arduinoWebSockets/src/WebSockets.h

leosoftGR commented 1 week ago

I did but unfortunately no debug messages comes into my serial console :(

leosoftGR commented 1 week ago

Hello again @Links2004

Since I was not able to debug your library with Uno R4, Is there any thought on how can I play with TCP / Network stack so I check if I can have any improvement regarding my issues?

Once again, thanks for your time

Links2004 commented 1 week ago

does the Uno R4 has vsnprintf I may can adapt the debug coed it it has.

leosoftGR commented 1 week ago

In this post says that could work. Check this out:

https://github.com/mobizt/ESP-Mail-Client/discussions/344

However, even when I add the following into my code:

`#include // Required for va_list, va_start, va_end

size_t va_printf(const char *format, ...) { va_list arg; va_start(arg, format); size_t ret = vprintf(format, arg); va_end(arg); return ret; }`

And change the line 53 of your WebSockets.h to:

#define DEBUG_WEBSOCKETS(...) va_printf( __ VA_ARGS __)

Unfortunately it does not gives me anything on my serial console :(

I really would like to participate for solving the issue on Uno R4 because currently your library is the only one that works with is box.

kakopappa commented 1 week ago

Hi @leosoftGR

Just tried this and seems to work. hope it helps

https://github.com/kakopappa/arduinoWebSockets/commit/713a36ea5649d52e18d06cf701027951693b7353

#define DEBUG_WEBSOCKETS(format, ...) \
    [&]() -> size_t { \
        char buf[256]; /* Adjust buffer size as needed */ \
        size_t ret = snprintf(buf, sizeof(buf), format, ##__VA_ARGS__); \
        Serial.print(buf); \
        return ret; \
    }()

image

Links2004 commented 1 week ago

have created a branch printf_debug_port set the define DEBUG_PORT to the Serial port you use.

platformio.ini example:

build_flags = -DDEBUG_PORT=Serial
leosoftGR commented 1 week ago

I moved my project to a local arduino ide 2.3 and a platform.txt file created in the same directory of my project. So I added: build_flags=-DDEBUG_ESP_PORT=Serial ..also tryed: build.extra_flags=-DDEBUG_ESP_PORT=Serial ...as well: build.flags=-DDEBUG_ESP_PORT=Serial

Then, on line 49 of WebShockets.h changed printf to print

And then compiled....

All ok but still no debug output in my serial console :(

Am I that much stupid or I miss something else?

leosoftGR commented 1 week ago

Hi @leosoftGR

Just tried this and seems to work. hope it helps

kakopappa@713a36e

#define DEBUG_WEBSOCKETS(format, ...) \
    [&]() -> size_t { \
        char buf[256]; /* Adjust buffer size as needed */ \
        size_t ret = snprintf(buf, sizeof(buf), format, ##__VA_ARGS__); \
        Serial.print(buf); \
        return ret; \
    }()

I managed to put your code on line 53 of WebShockets.h and then compile it. Nothing came to my console.

Do I have to declare something else in platform.txt ?

kakopappa commented 1 week ago

Hi @leosoftGR Just tried this and seems to work. hope it helps kakopappa@713a36e

#define DEBUG_WEBSOCKETS(format, ...) \
    [&]() -> size_t { \
        char buf[256]; /* Adjust buffer size as needed */ \
        size_t ret = snprintf(buf, sizeof(buf), format, ##__VA_ARGS__); \
        Serial.print(buf); \
        return ret; \
    }()

I managed to put your code on line 53 of WebShockets.h and then compile it. Nothing came to my console.

Do I have to declare something else in platform.txt ?

Screenshot is from Arduino IDE. baud rate 115200

leosoftGR commented 6 days ago

Finally I managed to solve my issue. What I did is to completely uninstall IDE and reinstall it with re-downloading of libraries. Then, the solution of @kakopappa worked flawlessly. And therefore, here is my debug output for @Links2004

`... [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0] client connection lost. [WS-Server][0] client disconnected. [WS-Server][0] new client [WS-Server][0][handleHeader] RX: GET / HTTP/1.1 [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Host: 192.168.1.80:81 [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Connection: Upgrade [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Pragma: no-cache [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Cache-Control: no-cache [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Upgrade: websocket [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Origin: chrome-extension://mlkkeillgbkknlahfljkjcjgkohpmoab [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Sec-WebSocket-Version: 13 [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Accept-Encoding: gzip, deflate [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Accept-Language: en-US,en;q=0.9 [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Sec-WebSocket-Key: wsqHTMdsoGgCj5nGJDrskQ== [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] RX: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][0][handleHeader] Header read fin. [WS-Server][0][handleHeader] - cURL: / [WS-Server][0][handleHeader] - cIsUpgrade: 1 [WS-Server][0][handleHeader] - cIsWebsocket: 1 [WS-Server][0][handleHeader] - cKey: wsqHTMdsoGgCj5nGJDrskQ== [WS-Server][0][handleHeader] - cProtocol: [WS-Server][0][handleHeader] - cExtensions: permessage-deflate; client_max_window_bits [WS-Server][0][handleHeader] - cVersion: 13 [WS-Server][0][handleHeader] - base64Authorization: [WS-Server][0][handleHeader] - cHttpHeadersValid: 1 [WS-Server][0][handleHeader] - cMandatoryHeadersCount: 0 [WS-Server][0][handleHeader] Websocket connection incoming. [WS-Server][0][handleHeader] - sKey: lJEhwR9fhifuI5cyBDHpestib4Q= [WS-Server][0][handleHeader] handshake HTTP/1.1 101 Switching Protocols

Server: arduino-WebSocketsServer

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Version: 13

Sec-WebSocket-Accept: lJEhwR9fhifuI5cyBDHpestib4Q=

[write] n: zu t: 190 [WS][0][headerDone] Header Handling Done. [WS][0][sendFrame] ------- send message frame ------- [WS][0][sendFrame] fin: 1 opCode: 9 mask: 0 length: 0 headerToPayload: 0 [write] n: zu t: 2 [WS][0][sendFrame] sending Frame Done (16275us). [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [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: 10 [WS][0][handleWebsocket] mask: 1 payloadLen: 0 [WS][0][handleWebsocketWaitFor] size: 6 cWsRXsize: 2 [readCb] n: zu t: 4 [WS][0][handleWebsocketWaitFor][readCb] size: 6 ok: 1 [WS][0][handleWebsocket] ------- read massage frame ------- [WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 10 [WS][0][handleWebsocket] mask: 1 payloadLen: 0 [WS][0][handleWebsocket] get pong () [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. ...... [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [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: 1 [WS][0][handleWebsocket] mask: 1 payloadLen: 3 [WS][0][handleWebsocketWaitFor] size: 6 cWsRXsize: 2 [readCb] n: zu t: 4 [WS][0][handleWebsocketWaitFor][readCb] size: 6 ok: 1 [WS][0][handleWebsocket] ------- read massage frame ------- [WS][0][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 1 [WS][0][handleWebsocket] mask: 1 payloadLen: 3 [readCb] n: zu t: 3 [WS][0][handleWebsocket] text: aaa [WS][0][sendFrame] ------- send message frame ------- [WS][0][sendFrame] fin: 1 opCode: 1 mask: 0 length: 122 headerToPayload: 0 [WS][0][sendFrame] text: Sat, 21 September 2024, 17:03:33 Up:0+00:01:00;1733;1743.63;0.53;2.89;0.20;1;8.00;5309;5324.34;1624.86;13.54;6.435;3.40;15 [write] n: zu t: 2 [write] n: zu t: 122 [WS][0][sendFrame] sending Frame Done (44404us). [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. ...... [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1] client connection lost. [WS-Server][1] client disconnected. [WS-Server][1] new client [WS-Server][1][handleHeader] RX: GET / HTTP/1.1 [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Host: 192.168.1.80:81 [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Connection: Upgrade [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Pragma: no-cache [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Cache-Control: no-cache [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Upgrade: websocket [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Origin: chrome-extension://mlkkeillgbkknlahfljkjcjgkohpmoab [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Sec-WebSocket-Version: 13 [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Accept-Encoding: gzip, deflate [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Accept-Language: en-US,en;q=0.9 [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Sec-WebSocket-Key: +8HvmThv5jNntBwoybGCcA== [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] RX: Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][1][handleHeader] Header read fin. [WS-Server][1][handleHeader] - cURL: / [WS-Server][1][handleHeader] - cIsUpgrade: 1 [WS-Server][1][handleHeader] - cIsWebsocket: 1 [WS-Server][1][handleHeader] - cKey: +8HvmThv5jNntBwoybGCcA== [WS-Server][1][handleHeader] - cProtocol: [WS-Server][1][handleHeader] - cExtensions: permessage-deflate; client_max_window_bits [WS-Server][1][handleHeader] - cVersion: 13 [WS-Server][1][handleHeader] - base64Authorization: [WS-Server][1][handleHeader] - cHttpHeadersValid: 1 [WS-Server][1][handleHeader] - cMandatoryHeadersCount: 0 [WS-Server][1][handleHeader] Websocket connection incoming. [WS-Server][1][handleHeader] - sKey: cJjg18claBZiPxj1wqYbsKpgJbI= [WS-Server][1][handleHeader] handshake HTTP/1.1 101 Switching Protocols

Server: arduino-WebSocketsServer

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Version: 13

Sec-WebSocket-Accept: cJjg18claBZiPxj1wqYbsKpgJbI=

[write] n: zu t: 190 [WS][1][headerDone] Header Handling Done. [WS][1][sendFrame] ------- send message frame ------- [WS][1][sendFrame] fin: 1 opCode: 9 mask: 0 length: 0 headerToPayload: 0 [write] n: zu t: 2 [WS][1][sendFrame] sending Frame Done (16312us). [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. [WS-Server][2] new client [WS][1][handleWebsocketWaitFor] size: 2 cWsRXsize: 0 [readCb] n: zu t: 2 [WS][1][handleWebsocketWaitFor][readCb] size: 2 ok: 1 [WS][1][handleWebsocket] ------- read massage frame ------- [WS][1][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 10 [WS][1][handleWebsocket] mask: 1 payloadLen: 0 [WS][1][handleWebsocketWaitFor] size: 6 cWsRXsize: 2 [readCb] n: zu t: 4 [WS][1][handleWebsocketWaitFor][readCb] size: 6 ok: 1 [WS][1][handleWebsocket] ------- read massage frame ------- [WS][1][handleWebsocket] fin: 1 rsv1: 0 rsv2: 0 rsv3 0 opCode: 10 [WS][1][handleWebsocket] mask: 1 payloadLen: 0 [WS][1][handleWebsocket] get pong () [WS-Server][2] client connection lost. [WS-Server][2] client disconnected. `

In the above test, I connected successfully with my first websocket client, and then tried to connect a second one and my Uno R4 freezed.....

Links2004 commented 6 days ago

the logs show 2 web socket clients connecting one after the other, which look mostly normal. the data rate is very slow 44ms for 122bytes or 2.8kb/s is way below normal for a WiFi connection.

the strange part is that there is a TCP client connecting and disconnecting all the time.

see all the:

[WS-Server][$] new client
[WS-Server][$] client connection lost.
[WS-Server][$] client disconnected.

is the TCP server of the R4 always saying that it has a new client?

leosoftGR commented 6 days ago

is the TCP server of the R4 always saying that it has a new client?

This is my test routine:

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length){ switch (type) { case WStype_DISCONNECTED: //Serial.print(F("WebSockets: [")); //Serial.print(num); //Serial.println(F("] Disconnected!")); break; case WStype_CONNECTED: // send message to server when Connected //Serial.print(F("WebSockets: [")); //Serial.print(num); //Serial.println(F("] Connected!")); break; case WStype_TEXT: webSocket.sendTXT(num, "Sat, 21 September 2024, 17:03:33 Up:0+00:01:00;1733;1743.63;0.53;2.89;0.20;1;8.00;5309;5324.34;1624.86;13.54;6.435;3.40;15"); break; case WStype_BIN: // send data to server // webSocket.sendBIN(payload, length); //break; case WStype_ERROR: //Serial.println(F("[WebSocket] Error!")); //break; case WStype_FRAGMENT_TEXT_START: case WStype_FRAGMENT_BIN_START: case WStype_FRAGMENT: case WStype_FRAGMENT_FIN: break; } }

Have no idea why there are disconnections...