From 0d6dbcf504650b2e17a7b8691f830c9f0d0a85fc Mon Sep 17 00:00:00 2001 From: ZMiguel Valdiviesso Date: Mon, 21 Dec 2020 11:45:39 +0000 Subject: [PATCH] Code V1 --- .gitignore | 1 + PowerBox-3Phase/LICENSE.md | 22 +++++ PowerBox-3Phase/PowerBox-3Phase.ino | 121 ++++++++++++++++++++++++++++ PowerBox-3Phase/README.md | 12 +++ PowerBox-3Phase/config_example.h | 6 ++ README.md | 11 ++- SolarMonitoring/LICENSE.md | 22 +++++ SolarMonitoring/README.md | 12 +++ SolarMonitoring/SolarMonitoring.ino | 99 +++++++++++++++++++++++ SolarMonitoring/config_example.h | 6 ++ 10 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 PowerBox-3Phase/LICENSE.md create mode 100644 PowerBox-3Phase/PowerBox-3Phase.ino create mode 100644 PowerBox-3Phase/README.md create mode 100644 PowerBox-3Phase/config_example.h create mode 100644 SolarMonitoring/LICENSE.md create mode 100644 SolarMonitoring/README.md create mode 100644 SolarMonitoring/SolarMonitoring.ino create mode 100644 SolarMonitoring/config_example.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..299bb98 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.h \ No newline at end of file diff --git a/PowerBox-3Phase/LICENSE.md b/PowerBox-3Phase/LICENSE.md new file mode 100644 index 0000000..5d8c364 --- /dev/null +++ b/PowerBox-3Phase/LICENSE.md @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2020 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/PowerBox-3Phase/PowerBox-3Phase.ino b/PowerBox-3Phase/PowerBox-3Phase.ino new file mode 100644 index 0000000..5106e8e --- /dev/null +++ b/PowerBox-3Phase/PowerBox-3Phase.ino @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include "config.h" + +WiFiMulti WiFiMulti; +InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME); +PZEM004Tv30 pzem[3] = { + PZEM004Tv30(&Serial2, 0x11), + PZEM004Tv30(&Serial2, 0x12), + PZEM004Tv30(&Serial2, 0x13) +}; + +// Data point +Point sensor[4] = { + Point("PowerBox"), + Point("PowerBox"), + Point("PowerBox"), + Point("PowerBox") +}; + +void ConnectToWiFiMulti() { + WiFi.mode(WIFI_STA); + WiFiMulti.addAP(SSID, WiFiPassword); + Serial.println("Connecting Wifi..."); + while (WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void ConnectToInflux() { + // Add constant tags - only once + sensor[0].addTag("Device", "ESP32"); + sensor[1].addTag("Power_Phase", "Phase_1"); + sensor[2].addTag("Power_Phase", "Phase_2"); + sensor[3].addTag("Power_Phase", "Phase_3"); + + // Check server connection + if (client.validateConnection()) { + Serial.print("Connected to InfluxDB: "); + Serial.println(client.getServerUrl()); + } else { + Serial.print("InfluxDB connection failed: "); + Serial.println(client.getLastErrorMessage()); + } +} + +void setup() { + Serial.begin(115200); + ConnectToWiFiMulti(); + ConnectToInflux(); +} + +void loop() { + sensor[0].clearFields(); + sensor[0].addField("wifi-rssi", WiFi.RSSI()); + //read sensors + for(int i=0;i<3;i++){ + sensor[i+1].clearFields(); + float voltage = pzem[i].voltage(); + if(!isnan(voltage)){ + sensor[i+1].addField("voltage", voltage); + } else { + Serial.println("Error reading voltage"); + } + float current = pzem[i].current(); + if(!isnan(current)){ + sensor[i+1].addField("current", current); + } else { + Serial.println("Error reading current"); + } + float power = pzem[i].power(); + if(!isnan(power)){ + sensor[i+1].addField("power", power); + } else { + Serial.println("Error reading power"); + } + float energy = pzem[i].energy(); + if(!isnan(energy)){ + sensor[i+1].addField("energy", energy); + } else { + Serial.println("Error reading energy"); + } + float frequency = pzem[i].frequency(); + if(!isnan(frequency)){ + sensor[i+1].addField("freq", frequency); + } else { + Serial.println("Error reading frequency"); + } + float pf = pzem[i].pf(); + if(!isnan(pf)){ + sensor[i+1].addField("power-factor", pf); + } else { + Serial.println("Error reading power factor"); + } + } + // If no Wifi signal, try to reconnect it + if ((WiFi.RSSI() == 0) && (WiFiMulti.run() != WL_CONNECTED)) + Serial.print("Writing: "); + Serial.println(sensor[0].toLineProtocol()); + Serial.println(sensor[1].toLineProtocol()); + Serial.println(sensor[2].toLineProtocol()); + Serial.println(sensor[3].toLineProtocol()); + // Write point + for(int i=0;i<4;i++){ + if (!client.writePoint(sensor[i])) { + Serial.print(i); + Serial.print(": InfluxDB write failed: "); + Serial.println(client.getLastErrorMessage()); + } + } + //Wait 5s + Serial.println("Wait ~5s"); + delay(4000); +} diff --git a/PowerBox-3Phase/README.md b/PowerBox-3Phase/README.md new file mode 100644 index 0000000..0b56114 --- /dev/null +++ b/PowerBox-3Phase/README.md @@ -0,0 +1,12 @@ +# ESP32-PowerMonitoring (PowerBox/3Phase) + +My take on a power measurement device using a ESP32 & PZEM-004T-v30 + +## Hardware requiered +- ESP32 Dev Board +- PZEM-004T-v30 Sensor +- InfluxDB server + +### Libraries used +- [mandulaj/PZEM-004T-v30](https://github.com/mandulaj/PZEM-004T-v30) - for sensor measuring +- [tobiasschuerg/InfluxDB-Client-for-Arduino](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino) - for influxdb connection \ No newline at end of file diff --git a/PowerBox-3Phase/config_example.h b/PowerBox-3Phase/config_example.h new file mode 100644 index 0000000..7255479 --- /dev/null +++ b/PowerBox-3Phase/config_example.h @@ -0,0 +1,6 @@ +const char *SSID = "ssid"; +const char *WiFiPassword = "password"; + +const char *INFLUXDB_URL = "http://ip:8086"; + +const char *INFLUXDB_DB_NAME = "dbname"; diff --git a/README.md b/README.md index 31288d2..294128b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # ESP32-PowerMonitoring -ESP32 + PZEM-004T-v30 Power Monitoring With InfluxDB & Grafana \ No newline at end of file +My take on a power measurement device using a ESP32 & PZEM-004T-v30 + +## Hardware requiered +- ESP32 Dev Board +- PZEM-004T-v30 Sensor +- InfluxDB server + +### Libraries used +- [mandulaj/PZEM-004T-v30](https://github.com/mandulaj/PZEM-004T-v30) - for sensor measuring +- [tobiasschuerg/InfluxDB-Client-for-Arduino](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino) - for influxdb connection diff --git a/SolarMonitoring/LICENSE.md b/SolarMonitoring/LICENSE.md new file mode 100644 index 0000000..5d8c364 --- /dev/null +++ b/SolarMonitoring/LICENSE.md @@ -0,0 +1,22 @@ + +The MIT License (MIT) + +Copyright (c) 2020 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/SolarMonitoring/README.md b/SolarMonitoring/README.md new file mode 100644 index 0000000..c1863d5 --- /dev/null +++ b/SolarMonitoring/README.md @@ -0,0 +1,12 @@ +# ESP32-PowerMonitoring (Solar) + +My take on a power measurement device using a ESP32 & PZEM-004T-v30 + +## Hardware requiered +- ESP32 Dev Board +- PZEM-004T-v30 Sensor +- InfluxDB server + +### Libraries used +- [mandulaj/PZEM-004T-v30](https://github.com/mandulaj/PZEM-004T-v30) - for sensor measuring +- [tobiasschuerg/InfluxDB-Client-for-Arduino](https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino) - for influxdb connection \ No newline at end of file diff --git a/SolarMonitoring/SolarMonitoring.ino b/SolarMonitoring/SolarMonitoring.ino new file mode 100644 index 0000000..bee5001 --- /dev/null +++ b/SolarMonitoring/SolarMonitoring.ino @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include "config.h" + +WiFiMulti WiFiMulti; +InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME); +PZEM004Tv30 pzem(&Serial2); + +// Data point +Point sensor("Solar_1"); + +void ConnectToWiFiMulti() { + WiFi.mode(WIFI_STA); + WiFiMulti.addAP(SSID, WiFiPassword); + Serial.println("Connecting Wifi..."); + while (WiFiMulti.run() != WL_CONNECTED) { + delay(100); + } + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); +} + +void ConnectToInflux() { + // Add constant tags - only once + sensor.addTag("device", "ESP32-Solar"); + + // Check server connection + if (client.validateConnection()) { + Serial.print("Connected to InfluxDB: "); + Serial.println(client.getServerUrl()); + } else { + Serial.print("InfluxDB connection failed: "); + Serial.println(client.getLastErrorMessage()); + } +} + +void setup() { + Serial.begin(115200); + ConnectToWiFiMulti(); + ConnectToInflux(); +} + +void loop() { + sensor.clearFields(); + sensor.addField("wifi-rssi", WiFi.RSSI()); + float voltage = pzem.voltage(); + if(!isnan(voltage)){ + sensor.addField("voltage", voltage); + } else { + Serial.println("Error reading voltage"); + } + float current = pzem.current(); + if(!isnan(current)){ + sensor.addField("current", current); + } else { + Serial.println("Error reading current"); + } + float power = pzem.power(); + if(!isnan(power)){ + sensor.addField("power", power); + } else { + Serial.println("Error reading power"); + } + float energy = pzem.energy(); + if(!isnan(energy)){ + sensor.addField("energy", energy); + } else { + Serial.println("Error reading energy"); + } + float frequency = pzem.frequency(); + if(!isnan(frequency)){ + sensor.addField("freq", frequency); + } else { + Serial.println("Error reading frequency"); + } + float pf = pzem.pf(); + if(!isnan(pf)){ + sensor.addField("power-factor", pf); + } else { + Serial.println("Error reading power factor"); + } + + // If no Wifi signal, try to reconnect it + if ((WiFi.RSSI() == 0) && (WiFiMulti.run() != WL_CONNECTED)) + Serial.println("Wifi connection lost"); + // Write point + if (!client.writePoint(sensor)) { + Serial.print("InfluxDB write failed: "); + Serial.println(client.getLastErrorMessage()); + } + //Wait 5s + Serial.println("Wait ~5s"); + delay(4000); +} diff --git a/SolarMonitoring/config_example.h b/SolarMonitoring/config_example.h new file mode 100644 index 0000000..7255479 --- /dev/null +++ b/SolarMonitoring/config_example.h @@ -0,0 +1,6 @@ +const char *SSID = "ssid"; +const char *WiFiPassword = "password"; + +const char *INFLUXDB_URL = "http://ip:8086"; + +const char *INFLUXDB_DB_NAME = "dbname";