-
Notifications
You must be signed in to change notification settings - Fork 554
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
If I put a delay in the loop, it stopped connecting to the server #877
Comments
any answer ? |
Simply DO NOT put any DELAY in loop with websockets. You could run websockets in separate thread if you want to use delays so hard: void WS_thread(void *pvParameters)
{
while (true)
webSocket.loop();
}
void setup()
{
//...
xTaskCreatePinnedToCore(
WS_thread, //Task function
"WS_thread", //Task name
10000, //Stack size for task
NULL,
1, // Task priority
NULL,
1 // processor CORE the task is being run on (0 or 1)
);
}
void loop()
{
//Your blocking code here ...
delay(2000);
} Alternatively you could use non-blocking approach for timers: static clock_t timestamp_prev = 0;
static clock_t timestamp = 0;
clock_t get_delta_time_ms()
{
clock_t delta;
timestamp = millis();
delta = timestamp - timestamp_prev;
timestamp_prev = timestamp;
return delta;
}
//...
clock_t my_timer = 0;
void loop()
{
//Calculate time delta and put it into the variable.
//Call get_delta_time_ms() ONLY ONCE per loop (this is important).
clock_t delta = get_delta_time_ms();
//Increment your timer by delta
my_timer += delta;
//Check if 2 seconds has elapsed
if (my_timer >= 2000) {
my_timer -= 2000;
//my_timer = 0; //same as above, but less precise
//Do some of your task here
}
//This websocket loop is not blocked by any command or delay
webSocket.loop();
} Also take note not to use ANY blocking functions inside websocket callback, or it will crash your system easily. P.S. The examples i have provided may contain syntax errors. |
Thanks for this information @furdog Seems that even a delay of just 10ms can cause freezings and/or crashes. |
the point at which a delay is problematic depends on multiple factors.
|
Thanks for the details Markus @Links2004 |
This helped me to run WS Client outside the loop(), but is very interesting that I had a small project 6 months ago (version 2.4.1) that is running until today in a loop() with delay(3000)! Now I tried to make that project work w/o success :(
webSocket.loop(); for me doesn't make sense as ESP32 should do another tasks before sending some info to server! |
webSocket.loop() does more than just sends your messages. |
@Links2004 What if I need to make some LED strip animation, looks like ws.loop add some 20 ms to the loop easily... Which can be problematic for animations ? I wanted to throttle, but as written this is not a good idea :/ what do you suggests |
I have this piece of code:
void loop() { webSocket.loop(); delay(2000); }
when I set the delay, the websocket cannot connect in any way and constantly log 'Disconnected'. But if I remove the delay, everything connects fine
I'm using ESP32
The text was updated successfully, but these errors were encountered: