Reconnection handeling

This commit is contained in:
Lukas Bachschwell 2019-05-18 15:56:56 +02:00
parent b734aa8763
commit 9042775f1c
4 changed files with 38 additions and 17 deletions

View File

@ -1,5 +1,5 @@
{
"board": "esp8266:esp8266:d1",
"configuration": "xtal=80,vt=flash,exception=disabled,eesz=4M,ip=lm2f,dbg=Disabled,lvl=None____,wipe=none,baud=921600",
"port": "/dev/cu.usbserial-144220"
"port": "/dev/cu.wchusbserial141230"
}

View File

@ -13,7 +13,7 @@ platform = espressif32
board = esp32-poe
framework = arduino
upload_port=/dev/cu.wchusbserial144220
upload_port=/dev/cu.wchusbserial141230
monitor_baud = 115200
build_flags = -DLOG_LOCAL_LEVEL=ESP_LOG_DEBUG

View File

@ -8,7 +8,10 @@
#include <ETH.h>
bool input1active = true;
static bool eth_connected = false;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
@ -17,7 +20,7 @@ const char *PARAM_MESSAGE = "message";
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf("Hello Client %u :)", client->id());
client->printf("Client: %u", client->id());
client->ping();
} else if (type == WS_EVT_DISCONNECT) {
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
@ -33,18 +36,23 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
if (info->opcode == WS_TEXT) {
for (size_t i = 0; i < info->len; i++) {
msg += (char)data[i];
}
Serial.print("message: ");
Serial.println(msg);
}
if (info->opcode == WS_TEXT) {
// Responding before the first heartbeat is through will lead to disconnects
// client->text("I got your text message");
}
if (info->opcode == WS_TEXT)
client->text("I got your text message");
} else {
Serial.printf("Error: Message in multiple Frames");
}
}
Serial.println();
}
void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); }
@ -90,6 +98,8 @@ void setup() {
WiFi.onEvent(WiFiEvent);
ETH.begin();
pinMode(5, INPUT_PULLUP);
while (!eth_connected) {
delay(1000);
}
@ -124,6 +134,23 @@ void setup() {
MDNS.addService("http", "tcp", 80);
}
void loop() {}
void loop() {
if (digitalRead(5) != input1active) {
delay(10);
if (digitalRead(5) != input1active) {
input1active = digitalRead(5);
if (!input1active) {
Serial.println("Turn on");
ws.textAll("blinkLamp");
} else {
Serial.println("Turn off");
ws.textAll("noBlinkLamp");
}
}
}
delay(100);
}
///////////////////////////

View File

@ -18,7 +18,7 @@ long blinkTimer = 0;
long connectTimer = 0;
bool foundIp = false;
IPAddress ip;
IPAddress ip; // = IPAddress(192, 168, 0, 111); // FIXME:
WebSocketsClient webSocket;
@ -75,10 +75,12 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
switch (type) {
case WStype_DISCONNECTED:
Serial.println("[WSc] Disconnected!");
foundIp = false;
break;
case WStype_CONNECTED: {
Serial.print("[WSc] Connected to controller");
webSocket.sendTXT("event: con lamp: 1");
foundIp = true;
} break;
case WStype_TEXT:
Serial.print("[WSc] get text:");
@ -90,14 +92,6 @@ void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
noBlinkLamp();
Serial.println("Disable Blinking!");
}
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
Serial.println("[WSc] get binary ");
// send data to server
// webSocket.sendBIN(payload, length);
break;
}
}
@ -151,7 +145,7 @@ void browseService(const char *service, const char *proto) {
webSocket.onEvent(webSocketEvent);
// webSocket.setAuthorization("user", "Password");
webSocket.setReconnectInterval(3000);
// webSocket.enableHeartbeat(15000, 3000, 2);
webSocket.enableHeartbeat(15000, 3000, 2);
}
}
}