Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.
This repository was archived by the owner on May 17, 2019. It is now read-only.

Socket Timeout after some time #14

Open
@niklasdoerfler

Description

@niklasdoerfler

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);

}

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