From f03f4f9412e465006ac7e52944691f3406a880a7 Mon Sep 17 00:00:00 2001 From: Yveaux Date: Fri, 4 Sep 2015 19:54:45 +0200 Subject: [PATCH] Hardened (dis)connections Added support fot multiple sessions (Currently 5 max. Each connection costs RAM!) Each session can send data to the gateway. Data from the gateway will be broadcasted to all connected clients. Added serial logging of client (dis)connections. Inspect serial terminal in case of problems. --- .../Esp8266Gateway/Esp8266Gateway.ino | 147 +++++++++++------- .../examples/Esp8266Gateway/GatewayUtil.h | 9 +- 2 files changed, 100 insertions(+), 56 deletions(-) diff --git a/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino index c5be624..ee828b5 100644 --- a/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino +++ b/libraries/MySensors/examples/Esp8266Gateway/Esp8266Gateway.ino @@ -123,24 +123,34 @@ MySensor gw(transport, hw ); -#define IP_PORT 5003 // The port you want to open +#define IP_PORT 5003 // The port you want to open +#define MAX_SRV_CLIENTS 5 // how many clients should be able to telnet to this ESP8266 // a R/W server on the port -WiFiServer server(IP_PORT); -// handle to open connection -WiFiClient client; +static WiFiServer server(IP_PORT); +static WiFiClient clients[MAX_SRV_CLIENTS]; +static bool clientsConnected[MAX_SRV_CLIENTS]; +static inputBuffer inputString[MAX_SRV_CLIENTS]; -char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface -int inputPos = 0; -bool sentReady = false; +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) -void output(const char *fmt, ... ) { - va_list args; - va_start (args, fmt ); - vsnprintf_P(serialBuffer, MAX_SEND_LENGTH, fmt, args); - va_end (args); - Serial.print(serialBuffer); - server.write(serialBuffer); + +void output(const char *fmt, ... ) +{ + char serialBuffer[MAX_SEND_LENGTH]; + va_list args; + va_start (args, fmt ); + vsnprintf_P(serialBuffer, MAX_SEND_LENGTH, fmt, args); + va_end (args); + Serial.print(serialBuffer); + for (uint8_t i = 0; i < ARRAY_SIZE(clients); i++) + { + if (clients[i] && clients[i].connected()) + { +// Serial.print("Client "); Serial.print(i); Serial.println(" write"); + clients[i].write((uint8_t*)serialBuffer, strlen(serialBuffer)); + } + } } void setup() @@ -153,7 +163,8 @@ void setup() Serial.print("Connecting to "); Serial.println(ssid); (void)WiFi.begin(ssid, pass); - while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) + { delay(500); Serial.print("."); } @@ -168,6 +179,7 @@ void setup() // start listening for clients server.begin(); + server.setNoDelay(true); } @@ -176,47 +188,74 @@ void loop() { checkButtonTriggeredInclusion(); checkInclusionFinished(); - - //check if there are any new clients - if (server.hasClient()) - { - if (client) - { - client.stop(); - } - client = server.available(); - output(PSTR("0;0;%d;0;%d;Gateway startup complete.\n"), C_INTERNAL, I_GATEWAY_READY); - } - if (client) { - if (!client.connected()) { - client.stop(); - } else if (client.available()) { - // read the bytes incoming from the client - char inChar = client.read(); - if (inputPos