This commit is contained in:
José Valdiviesso 2020-12-21 11:45:39 +00:00
parent 4a5be5b588
commit 0d6dbcf504
10 changed files with 311 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.h

View File

@ -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.

View File

@ -0,0 +1,121 @@
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <InfluxDbClient.h>
#include <PZEM004Tv30.h>
#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);
}

12
PowerBox-3Phase/README.md Normal file
View File

@ -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

View File

@ -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";

View File

@ -1,3 +1,12 @@
# ESP32-PowerMonitoring # ESP32-PowerMonitoring
ESP32 + PZEM-004T-v30 Power Monitoring With InfluxDB & Grafana 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

View File

@ -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.

12
SolarMonitoring/README.md Normal file
View File

@ -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

View File

@ -0,0 +1,99 @@
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <InfluxDbClient.h>
#include <PZEM004Tv30.h>
#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);
}

View File

@ -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";