/** * _____ ____ ____ _______ _ * |_ _||_ \ / _||_ __ \ / \ * | | | \/ | | |__) | / _ \ * _ | | | |\ /| | | __ / / ___ \ * | |__' | _| |_\/_| |_ _| | \ \_ _/ / \ \_ * `.____.' |_____||_____||____| |___||____| |____| * * ESP Energy Monitor v0.6.6 by Jorge Assunção * * Based on a project by timseebeck @ https://community.home-assistant.io/t/power-monitoring-with-an-xtm18s-and-mqtt/16316 * Remote Debug over Telnet by JoaoLopesF @ https://github.com/JoaoLopesF/ESP8266-RemoteDebug-Telnet * * See Github for instructions on how to use this code: https://github.com/jorgeassuncao/ESP8266-Energy-Monitor * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU General Public License version 2 as published by the Free Software Foundation. * * You can change your personal user data (wifi access, MQTT server, etc) in the "config/userdata.h" file */ //************* INCLUDE LIBRARIES ************************************************************************ //******************************************************************************************************** #include #include #include #include #include //#include "RemoteDebug.h" #include "userdata.h" //************* PROJECT AND VERSION ********************************************************************** //******************************************************************************************************** const char* proj_ver = "MAID - Energy Monitor v0.6.6 (15/10/2017)"; // Project name and version //************* CONFIG DEBUG ***************************************************************************** //******************************************************************************************************** //RemoteDebug Debug; // Remote debug type //************* CONFIG WEBSERVER ************************************************************************* //******************************************************************************************************** ESP8266WebServer server(80); // Webserver port //************* CONFIG PINS ***************************************************************************** //******************************************************************************************************** #define DIGITAL_INPUT_SENSOR 14 // Digital input D6 int LED_pin = 13; // Internal LED in NodeMCU #define BRIGHT 150 // Maximum LED intensity (1-500) #define INHALE 1500 // Breathe-in time in milliseconds #define PULSE INHALE*1000/BRIGHT // Pulse #define REST 1000 // Pause between breathes //************* CONFIG PULSES PER KWH ******************************************************************** //******************************************************************************************************** #define PULSE_FACTOR 2000 // Number of pulses of the meter per kWh //************* CONFIG OTHER GLOBALS ********************************************************************* //******************************************************************************************************** unsigned long SEND_FREQUENCY = 15000; // Minimum time between send in milliseconds volatile unsigned long pulseCount = 0; // Variable - volatile unsigned long lastBlink = 0; // Variable - double kwh; // Variable - unsigned long lastSend; // Variable - WiFiClient espClient; // Initiate wifi PubSubClient client(espClient); // Initiate MQTT long lastMsg = 0; // Variable - char msg[50]; // Variable - char wattString[7]; // Variable - char kwhString[7]; // Variable - double kwhaccum; // Variable - char kwhaccumString[7]; // Variable - float kwhReading; // Variable - uint32_t mmLastTime = 0; // Variable - uint32_t mmTimeSeconds = 0; // Variable - int N = 1; // Variable - Number of times to loop int runXTimes = 0; // Variable - Count times looped byte mac[6]; // Variable - MAC address char myBuffer[15]; // Variable - MAC string buffer uint8_t MAC_array[6]; // Variable - char MAC_char[18]; // Variable - //************* CONFIG WEBSERVER ROOT PAGE *************************************************************** //******************************************************************************************************** String getPage(){ // Create webpage content String page = ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += host_name; page += ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += ""; page += "

"; page += "
"; page += "
"; page += "
"; page += "
"; page += "

"; page += proj_ver; page += "

"; page += "

This project uses a MQTT topic to store the accumulated kWh value, so that when there is a reboot, reset or power off the value won't be lost. The subscribed topic to get the accumulated kWh value is "; page += mqtt_topic_sub_1; page += "

"; page += "
"; page += "
"; page += ""; page += "Attention! This page auto-refreshes every 15 seconds."; page += "
"; page += "
"; page += "
"; page += "
"; page += "
"; page += "

Current Consumption

"; page += "

"; page += kwhString; page += " kWh

"; page += "
"; page += "
"; page += "

Current Power

"; page += "

"; page += wattString; page += " Watt

"; page += "
"; page += "
"; page += "

Accum Consumption

"; page += "

"; page += kwhaccumString; page += " kWh (total)

"; page += "
"; page += "
"; page += "
"; page += "
"; page += "
"; page += ""; page += "

Warning!

Sometimes the board does not restart correctly. If you can't get back to this page after one minute, you need to reset on the board directly on the fisical reset button.  "; page += ""; page += "
"; page += "
"; page += "
"; page += "
"; page += "copyright

"; page += "
"; page += "
"; page += ""; page += ""; return page; } //************* SETUP WIFI SERVER ************************************************************************ //******************************************************************************************************** void handleRoot() { // Handle "root" page server.send ( 200, "text/html", getPage() ); } void handleNotFound(){ // Handle "not found" page String message = "Page or File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i0;ii--){ // Breath out digitalWrite(LED_pin, LOW); // LED on delayMicroseconds(ii*10); // Wait digitalWrite(LED_pin, HIGH); // LED off delayMicroseconds(PULSE-ii*10); // Wait ii--; delay(0); // Prevent watchdog firing } delay(REST); // Pause before repeat if (!client.connected()) { // If client disconnects... reconnect(); // ...connect again } client.loop(); unsigned long now = millis(); bool sendTime = now - lastSend > SEND_FREQUENCY; // Only send values at a maximum frequency if (sendTime) { Serial.println("- - - - - - - - - - - -"); // Block separator to serial interface// // Debug.println("- - - - - - - - - - - -"); // Block separator to telnet debug interface dtostrf(kwh, 2, 4, kwhString); // Convert Current kWh to string client.publish(mqtt_topic_kwh,kwhString); // Publish Current kWh to MQTT topic Serial.print("- Current kWh: "); Serial.println(kwhString); // Send Current kWh to serial interface // Debug.printf("- Current kWh: "); Debug.println(kwhString); // Send Current kWh to telnet debug interface lastSend = now; // Update the send time after publishing double watt = kwh * 1000.0 * 3600.0 / (double)SEND_FREQUENCY * 1000.0; // Calculate the power using the energy dtostrf(watt, 4, 2, wattString); // Convert Current Watt to string client.publish(mqtt_topic_watt,wattString); // Publish Current Watt to MQTT topic Serial.print("- Current Watt: "); Serial.println(wattString); // Send Current Watt to serial interface // Debug.printf("- Current Watt: "); Debug.println(wattString); // Send Current Watt to telnet debug interface kwhaccum = kwhReading + ((double)pulseCount/((double)PULSE_FACTOR)); // Calculate the accumulated energy since the begining dtostrf(kwhaccum, 5, 2, kwhaccumString); // Convert Accum kWh (pulses) to string client.publish(mqtt_topic_pulse,kwhaccumString, true); // Publish Accum kWh (pulses) to MQTT topic Serial.print("- Accum kWh: "); Serial.println(kwhaccumString); // Send Accum kWh (pulses) to serial interface // Debug.printf("- Accum kWh: "); Debug.println(kwhaccumString); // Send Accum kWh (pulses) to telnet debug interface kwh = 0; // Reset kWh count String ipaddress = WiFi.localIP().toString(); // Create IP address string char ipchar[ipaddress.length()+1]; ipaddress.toCharArray(ipchar,ipaddress.length()+1); client.publish(mqtt_topic_ip,ipchar); // Publish IP address to MQTT topic Serial.println(); // Block space to serial interface // Debug.println(); // Block space to telnet debug interface } // Debug.handle(); // Remote debug over telnet yield(); // Yielding } // END //********************************************************************************************************