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

Getting disconnected after 1 or 2 messages if sent from input interrupt handler #27

Closed
rkammela opened this issue Dec 17, 2015 · 2 comments

Comments

@rkammela
Copy link

I am testing WebSocketClient.ino on Adafruit HUZZAH ESP8266 breakout using node webSocketServer. I modified WebSocketClient.ino to add input interrupt from push button which sends a text message. First message works and then it disconnects.

Here is the code:

#include <Arduino.h>

#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 lenght) {


    switch(type) {
        case WStype_DISCONNECTED:
            Serial.printf("[WSc] Disconnected!\n");
            break;
        case WStype_CONNECTED:
            {
                Serial.printf("[WSc] Connected to url: %s\n",  payload);

                // send message to server when Connected
                webSocket.sendTXT("Connected");
            }
            break;
        case WStype_TEXT:
            Serial.printf("[WSc] get text: %s\n", payload);

            // send message to server
            // webSocket.sendTXT("message here");
            break;
        case WStype_BIN:
            Serial.printf("[WSc] get binary lenght: %u\n", lenght);
            hexdump(payload, lenght);

            // send data to server
            // webSocket.sendBIN(payload, lenght);
            break;
    }

}


void inputChanged() {
   Serial.printf("inputChanged\n");
   webSocket.sendTXT("inputChanged");
}

void setup() {

    Serial.begin(115200);

    Serial.setDebugOutput(true);

    for(uint8_t t = 4; t > 0; t--) {
        Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }


    WiFiMulti.addAP("KWN", "xxxxxxxx");


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

    webSocket.begin("192.168.0.10", 81);
    webSocket.onEvent(webSocketEvent);

    pinMode(0, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(0), inputChanged, RISING);

}



void loop() {
    webSocket.loop();

}

Here is what i get from serial monitor:

[SETUP] BOOT WAIT 4...
scandone
state: 0 -> 2 (b0)
[SETUP] BOOT WAIT 3...
reconnect
state: 2 -> 0 (0)
f 0, [SETUP] BOOT WAIT 2...
scandone
state: 0 -> 2 (b0)
[SETUP] BOOT WAIT 1...
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with KWN, channel 11
dhcp client start...
state: 5 -> 0 (0)
rm 0
f 0, scandone
f 0, scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 4
cnt 

connected with KWN, channel 11
dhcp client start...
ip:192.168.0.16,mask:255.255.255.0,gw:192.168.0.1
[WSc] Connected to url: /
[WSc] get text: Hallo Client!
[WSc] get text: Connected
inputChanged
[WSc] get text: inputChanged
inputChanged
inputChanged
[WSc] Disconnected!

Here is what i see on the server side:

Thu Dec 17 2015 03:35:57 GMT-0600 (Central Standard Time) Connection accepted.
Received Message: Connected
Received Message: inputChanged
Thu Dec 17 2015 03:37:25 GMT-0600 (Central Standard Time) Peer ::ffff:192.168.0.16 disconnected.

sendTXT works fine if i placed it in loop as shown below and it doesn't get disconnected.

void loop() {
    webSocket.loop();


        static unsigned long last = 0;

        if(abs(millis() - last) > 1000) {
        webSocket.sendTXT(String(millis()));
        last = millis();

}

But if i press the button it disconnects. If i remove the sendTXT from button interrupt handler , connection stays alive.

Any idea what could be causing this issue?

@Links2004
Copy link
Owner

you do network operations in the interrupt wich is not allowed.
i recommend you set a flag in the IRQ and handle that then in the loop.

@rkammela
Copy link
Author

It makes sense, thanks for the quick reply.

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