Skip to content

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

Closed
@uncletammy

Description

@uncletammy

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() {

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions