diff --git a/src/main.cpp b/src/main.cpp index 5367e01..04e3597 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -43,25 +42,27 @@ void setup() { //Initialize CSE7766 sensor _cseSensor.setRX(1); + _cseSensor.setInverted(false); _cseSensor.begin(); // serial: 4800 bps - Serial.begin(115200); - Serial.println("\nInitializing!\n\nConnecting to wifi ssid: " + String(wifiSSID) + " "); + //Serial.begin(115200); + ////Serial.println("\nInitializing!\n\nConnecting to wifi ssid: " + String(wifiSSID) + " "); WiFi.begin(wifiSSID, wifiPassword); while (WiFi.status() != WL_CONNECTED) { // Flash LED - (LED_on) ? digitalWrite(LED_PIN, LOW) : digitalWrite(LED_PIN, HIGH); - delay(500); - Serial.print("."); + (LED_on) ? digitalWrite(LED_PIN, LOW) : digitalWrite(LED_PIN, HIGH); + (LED_on) ? LED_on = false : LED_on = true; + delay(100); + //Serial.print("."); } digitalWrite(LED_PIN, HIGH); - Serial.print("\n\nConnected!"); - Serial.print("\n\nIP address: "); - Serial.print(WiFi.localIP()); + //Serial.print("\n\nConnected!"); + //Serial.print("\n\nIP address: "); + //Serial.print(WiFi.localIP()); // Initialise and OPEN relay pinMode(RELAY_PIN, OUTPUT); @@ -78,7 +79,7 @@ void setup() { size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*); _server.collectHeaders(headerkeys, headerkeyssize ); _server.begin(); - Serial.println("\nHTTP _server started\n\n"); + //Serial.println("\nHTTP _server started\n\n"); } void loop() { @@ -91,15 +92,15 @@ void handleLogin() if (_server.hasHeader("Cookie")) { - Serial.print("Found cookie: "); + //Serial.print("Found cookie: "); String cookie = _server.header("Cookie"); - Serial.println(cookie); + //Serial.println(cookie); } //User Logout (remove cookie) if (_server.hasArg("DISCONNECT")) { - Serial.println("Disconnection"); + //Serial.println("Disconnection"); String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n"; _server.sendContent(header); return; @@ -112,16 +113,16 @@ void handleLogin() { String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n"; _server.sendContent(header); - Serial.println("Log in Successful by user"); + //Serial.println("Log in Successful by user"); return; } //Username or password failed msg = "

Wrong username/password! try again.

"; - Serial.println("Log in Failed"); + //Serial.println("Log in Failed"); } //Login HTML (/login.html) | Converted to string by using included tool (/HTML_to-String.html) - String html =" Iot-controller dashboard

Please log into the wifi power switch


User:
Password:

"; + String html =" SWITCH

Please log in to view the control page


User:
Password:

"; html += msg; html +="




info "; @@ -132,20 +133,20 @@ void handleLogin() //check if user has cookie bool is_authenticated() { - Serial.println("Enter is_authenticated"); + //Serial.println("Enter is_authenticated"); if (_server.hasHeader("Cookie")) { - Serial.print("Found cookie: "); + //Serial.print("Found cookie: "); String cookie = _server.header("Cookie"); - Serial.println(cookie); + //Serial.println(cookie); if (cookie.indexOf("ESPSESSIONID=1") != -1) { - Serial.println("Authentication Successful"); + //Serial.println("Authentication Successful"); return true; } } - Serial.println("Authentication Failed"); + //Serial.println("Authentication Failed"); return false; } @@ -189,17 +190,16 @@ void handelGetData() } //log - Serial.println(); - Serial.println("Client updated webpage"); - Serial.println(); + //Serial.println("Client updated webpage\n\n"); //send JSON string _server.send(200, "application/json", jsonData); } void handleRoot() { - Serial.println("Enter handleRoot"); + //Serial.println("Enter handleRoot"); String header; + _cseSensor.handle(); if (!is_authenticated()) { @@ -209,19 +209,19 @@ void handleRoot() } //Main HTML page (/Index.html) | Converted to string by using included tool (/HTML_to-String.html) - String html =" Iot-controller dashboard

Wifi switch UI

Sensors:

Voltage:
"; + String html =" SWITCH

REMOTE MONITOR & CONTROL

Sensor:

Voltage: "; html += String(_cseSensor.getVoltage()); - html +="

Current:
"; + html +=" V

Current: "; html += String(_cseSensor.getCurrent()); - html +="

ActivePower:
"; + html +=" A

ActivePower: "; html += String(_cseSensor.getActivePower()); - html +="

ApparentPower:
"; + html +=" W

ApparentPower: "; html += String(_cseSensor.getApparentPower()); - html +="

ReactivePower:
"; + html +=" VA

ReactivePower: "; html += String(_cseSensor.getReactivePower()); - html +="

Energy:
"; + html +=" VAr

Energy: "; html += String(_cseSensor.getEnergy()); - html +="

Relay: "; + html +=" kWh

Device Status: "; html += (relayState) ? "ON" : "OFF"; html +="

Update rate:

Update rate:

2000
"; @@ -232,13 +232,13 @@ void handleRoot() { relayState = true; digitalWrite(RELAY_PIN, HIGH); - Serial.println("Relay switched on!"); + //Serial.println("Relay switched on!\n\n"); } if (_server.hasArg("RELAY-OFF")) { relayState = false; digitalWrite(RELAY_PIN, LOW); - Serial.println("Relay switched off!"); + //Serial.println("Relay switched off!\n\n"); } } @@ -259,4 +259,4 @@ void handleNotFound() message += " " + _server.argName(i) + ": " + _server.arg(i) + "\n"; } _server.send(404, "text/plain", message); -} \ No newline at end of file +}