morrissinger / ESP8266-Websocket

A websocket library for the ESP-8266.
305 stars 118 forks source link

Socket Timeout after some time #14

Open niklasdoerfler opened 8 years ago

niklasdoerfler commented 8 years ago

Hey,

I'm using this library to listen to a websocket in client mode. I'm trying to control a led stip with the esp8266 module. Everything works fine, but after some time the socket doesn't get the data from the server. Is there a way to reset the controller if socket connection is lost?

Thanks in advance :)

My code:

#include <ESP8266WiFi.h>
#include <WebSocketClient.h>
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>     //Local WebServer used to serve the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic

char path[] = "/";
char host[] = "foo.bar";
int port = 9000;

WebSocketClient webSocketClient;
WiFiClient client;

void setup() {
  Serial.begin(115200);
  WiFiManager wifiManager;
  //wifiManager.resetSettings();

  //Initial LED settings
  delay(10);
  pinMode(12, OUTPUT); 
  pinMode(13, OUTPUT); 
  digitalWrite(12,HIGH);
  digitalWrite(13,LOW);
  delay(400);
  digitalWrite(12,LOW);

  //try to connect to Wifi Access Point or create a hotspot if no suitable network found
  //first parameter is name of access point, second is the password
  wifiManager.autoConnect("MyWiFi", "myPassphrase");

  delay(1000);

  // Connect to the websocket server
  if (client.connect(host, port)) {
    Serial.println("Connected");
  } else {
    Serial.println("Connection failed.");
    while(1) {
      // Hang on failure
    }
  }

  // Handshake with the server
  webSocketClient.path = path;
  webSocketClient.host = host;
  if (webSocketClient.handshake(client)) {
    Serial.println("Handshake successful");
    digitalWrite(12,HIGH);
  } else {
    Serial.println("Handshake failed.");
    while(1) {
      // Hang on failure
    }  
  }
}

void loop() {
  String data;

  if (client.connected()) {
    Serial.println("Try to get data...");
    webSocketClient.getData(data);
    if (data.length() > 0) {
      Serial.print("Received data: ");
      Serial.println(data);

      if(data=="open") //keyword to switch on the LED strip
        digitalWrite(13,HIGH);
      else //keyword to switch off the LED strip
        digitalWrite(13,LOW);
    }

  } else {
    Serial.println("Client disconnected.");
    while (1) {
      // Hang on disconnect.
    }
  }

  // wait to fully let the client disconnect
  delay(2000);

}
modabasi commented 8 years ago

You can just use;

ESP.reset();

niklasdoerfler commented 8 years ago

Thank you for your answer! I replaced the last while loop with your code, but it doesn't really help me with my actual problem, not knowing when the Websockets disconnects. It only resets the ESP when the WiFi connection gets lost.

Does anybody know how to notice if the websocket connection is lost?

Thanks in advance!

deepikavira commented 8 years ago

I Use the if condition each time before listening/sending data over the socket. if (clientsio.connected())

On Sat, Apr 30, 2016 at 3:19 PM, Niklas Dörfler notifications@github.com wrote:

Thank you for your answer! I replaced the last while loop with your code, but it doesn't really help me with my actual problem, not knowing when the Websockets disconnects. It only resets the ESP when the WiFi connection gets lost.

Does anybody know how to notice if the websocket connection is lost?

Thanks in advance!

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/morrissinger/ESP8266-Websocket/issues/14#issuecomment-215999461

niklasdoerfler commented 8 years ago

Yes, thats what I also do. But it only checks, if the WiFi connection is connected. I want to check, if the socket is still working. Therefore I need some method like this from the class 'WebSocketClient'...

deepikavira commented 8 years ago

@niklasdoerfler Did you try using any other method to detect websocket connection lost? the client.connected() method works whenever I reset my server. i.e it detects that the server connection was completely dropped. But other than that if there is some random issue due to which socket connection is lost then this function call doesn't really help

mauricioribeiro commented 7 years ago

Hi everyone! I'm also having websocket timeout problem. Is there some WebSocketClient method that checks if websocket connection was lost?

doit4fun commented 6 years ago

you can do a heart beat between server and client and if not working then disconnect and reconnect again

mauricioribeiro commented 6 years ago

I know it's late, but anyway, I'm gonna share how I solved this problem: I decided to use the native ESP firmware (it's written in Lua, but don't worry! It has a good documentation and it's simple to understand, trust me :D).

There is a module to work with websocket and worked consistently well in my project. Here is the documentation about the module: https://nodemcu.readthedocs.io/en/master/en/modules/websocket/

I hope it helps! Hugs!

mars000 commented 5 years ago

hi @mauricioribeiro can you share your code or example how you actually solved this ? do you still use the following websocket library = https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.h

sw-tt-chandershekharsuthar commented 5 years ago

@modabasi @niklasdoerfler I'm using Arduino IDE 1.6.5 and NodeMcu(ESP8266) as web-socket client_Demo Example My host is same "echo.websocket.org" with port 80 and geting that kind of response

Connecting to Local_wifi_Network ........ WiFi connected IP address: 172.161.131.153 Connected Waiting... Waiting... Waiting... Waiting... Waiting... Handshake successful ..............(After just that I got ) Client disconnected. Client disconnected.

So there is just 2 or 3 Sec between "Handshake successful" and "Client disconnected" I think there was problem in Connection Timeout but when I found #define TIMEOUT_IN_MS 10000 So I think there are some other problem Let me know if Someone did that connection successfully.

I need to try WSS (with port 443)