Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add networking logging capability. #1

Merged
merged 3 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

61 changes: 61 additions & 0 deletions src/smartpower2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,12 +115,54 @@ 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
USE_SERIAL.printf("A connection attempt is being made.\n");

if (!logClient)
{
// This is the first client to connect
logClient = logServer.available();

USE_SERIAL.printf("This is the first client to connect.\n");
}
else
{
if (!logClient.connected())
{
// A previous client has disconnected.
// Connect the new client.
logClient.stop();
logClient = logServer.available();

USE_SERIAL.printf("A previous client has disconnected.\n");
}
else
{
// A client connection is already in use.
// Drop the new connection attempt.
WiFiClient tempClient = logServer.available();
tempClient.stop();

USE_SERIAL.printf("A client connection is already in use.\n");
}
}
}


}

void webserver_init(void)
Expand Down Expand Up @@ -667,6 +714,20 @@ void handler(void)
}
}

if (logClient && logClient.connected())
{
// Report the log info
String data = String(volt, 3) + "," + String(ampere, 3) + "," +
String(watt, 3) + "," + String(watth/3600, 3) + "\r\n";

logClient.write(data.c_str());

while(logClient.available())
{
logClient.read();
}
}

lcd_status();
wifi_connection_status();
readSystemReset();
Expand Down