diff --git a/platformio.ini b/platformio.ini index 7600bb27fc28beec7e76e0eb303048b0fce6bcce..e7a518cf46209579d53e38f7e43596cce66b0289 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,3 +25,7 @@ board = esp12e upload_speed = 921600 board_f_cpu = 160000000L build_flags = -Wl,-Tesp8266.flash.4m.ld +lib_deps = WebSockets + Simpletimer + LiquidCrystal_I2C + diff --git a/src/smartpower2.ino b/src/smartpower2.ino index 911a120f12116ef76828f047026dcd333d299bab..64874f4dc03003b51d7a099e07eae9b6fca26a2d 100644 --- a/src/smartpower2.ino +++ b/src/smartpower2.ino @@ -10,6 +10,11 @@ #include <mcp4652.h> #include <LiquidCrystal_I2C.h> +#define CONSOLE_PORT 23 + +WiFiServer logServer(CONSOLE_PORT); +WiFiClient logClient; + #define USE_SERIAL Serial #define MAX_LCD_SSID_LENGTH 12 @@ -110,12 +115,46 @@ void setup() { webserver_init(); timerId = timer.setInterval(1000, handler); + + // Log + logServer.begin(); + logServer.setNoDelay(true); } void loop() { server.handleClient(); webSocket.loop(); timer.run(); + + + if (logServer.hasClient()) + { + // A connection attempt is being made + if (!logClient) + { + // This is the first client to connect + logClient = logServer.available(); + } + else + { + if (!logClient.connected()) + { + // A previous client has disconnected. + // Connect the new client. + logClient.stop(); + logClient = logServer.available(); + } + else + { + // A client connection is already in use. + // Drop the new connection attempt. + WiFiClient tempClient = logServer.available(); + tempClient.stop(); + } + } + } + + } void webserver_init(void) @@ -667,6 +706,15 @@ void handler(void) } } + if (logClient && logClient.connected()) + { + // Report the log info + String data = String(volt) + "," + String(ampere) + "," + + String(watt, 3) + "," + String(watth/3600, 3) + "\r\n"; + + logClient.write(data.c_str()); + } + lcd_status(); wifi_connection_status(); readSystemReset();