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

Delay() inside of "for" loops fail after socket connection is made #58

Closed
uncletammy opened this issue Mar 4, 2016 · 2 comments
Closed

Comments

@uncletammy
Copy link
Contributor

Using the async branch, delay()s inside of "for" loops aren't honored after a websockets connection is established. The same loop performs as expected before the ws connection is made. Is this expected behavior? Is there a workaround?

Hardware

Generic ESP-12E board

Sketch

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

    // Immediately exits the for loop and prints 1-255 in the console
    for(uint8_t c = 255; c > 0; c--) {
          Serial.println(c);
        delay(1000);
    }

}

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

    WiFiMulti.addAP("myNetworkName", "myPassword");

    while(WiFiMulti.run() != WL_CONNECTED) {
        delay(100);
    }

    webSocket.begin("192.168.1.119", 1440);
    webSocket.onEvent(webSocketEvent);


    // Works as expected
    for(uint8_t c = 255; c > 0; c--) {
        Serial.println(c);
        delay(1000);
    }


}

void loop() {

}
@Links2004
Copy link
Owner

yes that is normal, when using async
the webSocketEvent has the similar constraints then an interrupt on ESP.
the code is not running in the "arduino loop" context.
your code can take more time then in a interrupt,
but all thinks that end up calling yield will make problems.

if you want delayed actions on way is to set a flag or save the executing time.
and handle that in the loop.

may reading millis and comparing is working inside the webSocketEvent.

until we port the ESP8266 RTOS SDK this is the only way.
(the port will need some time, and a Stable RTOS SDK)

@uncletammy
Copy link
Contributor Author

@Links2004, thanks for the quick reply. I used a flag as a workaround. My solution was similar to the code below.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
#include <Hash.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

boolean mustDoSomething = false;

void doSomething(){

    // Now also works as expected
    for(uint8_t c = 255; c > 0; c--) {
        Serial.println(c);
        delay(1000);
    }

}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

  mustDoSomething = true;

}

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

    WiFiMulti.addAP("myNetworkName", "myPassword");

    while(WiFiMulti.run() != WL_CONNECTED) {
        delay(100);
    }
    webSocket.begin("192.168.1.119", 1440);
    webSocket.onEvent(webSocketEvent);

    // Works as expected
    for(uint8_t c = 255; c > 0; c--) {
        Serial.println(c);
        delay(1000);
    }


}

void loop() {

  if (mustDoSomething){
    doSomething();
  }
  mustDoSomething = false;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants