Browse Source

Final V1

master
José Valdiviesso 1 year ago
parent
commit
49b1e9c35f
  1. 35
      ESP32-SolarPowerMonitor.ino

35
ESP32-SolarPowerMonitor.ino

@ -1,4 +1,5 @@
#include <Arduino.h>
#include <HardwareSerial.h>
#include "WiFi.h"
#include "WiFiMulti.h"
#include "src/InfluxDB-Client-for-Arduino/src/InfluxDbClient.h"
@ -10,7 +11,7 @@ InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
PZEM004Tv30 pzem(&Serial2);
// Data point
Point sensor("solar_status");
Point sensor("Solar_1");
void ConnectToWiFiMulti() {
WiFi.mode(WIFI_STA);
@ -27,7 +28,7 @@ void ConnectToWiFiMulti() {
void ConnectToInflux() {
// Add constant tags - only once
sensor.addTag("Phase", "Solar 1");
sensor.addTag("device", "ESP32-Solar");
// Check server connection
if (client.validateConnection()) {
@ -43,53 +44,63 @@ void setup() {
Serial.begin(115200);
ConnectToWiFiMulti();
ConnectToInflux();
// Add constant tags - only once
sensor.addTag("device", "ESP32-Solar");
}
void loop() {
float voltage = pzem.voltage();
sensor.clearFields();
sensor.addField("wifi-rssi", WiFi.RSSI());
float voltage = pzem.voltage();
if(!isnan(voltage)){
sensor.addField("voltage", voltage);
Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
} else {
Serial.println("Error reading voltage");
}
float current = pzem.current();
if(!isnan(current)){
sensor.addField("current", current);
Serial.print("Current: "); Serial.print(current); Serial.println("A");
} else {
Serial.println("Error reading current");
}
float power = pzem.power();
if(!isnan(power)){
sensor.addField("power", power);
Serial.print("Power: "); Serial.print(power); Serial.println("W");
} else {
Serial.println("Error reading power");
}
float energy = pzem.energy();
if(!isnan(energy)){
sensor.addField("energy", energy);
Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
} else {
Serial.println("Error reading energy");
}
float frequency = pzem.frequency();
if(!isnan(frequency)){
sensor.addField("freq", frequency);
Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
} else {
Serial.println("Error reading frequency");
}
float pf = pzem.pf();
if(!isnan(pf)){
sensor.addField("power-factor", pf);
Serial.print("PF: "); Serial.println(pf);
} else {
Serial.println("Error reading power factor");
}
Serial.println();
delay(2000);
// 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(5000);
}

Loading…
Cancel
Save