Last active
November 3, 2022 12:03
-
-
Save ochui/7ff158fb34f7d0bd31233ea2ec8f87d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <PZEM004Tv30.h> | |
#include <SoftwareSerial.h> | |
#define MIN_TRIGGER_VOLTAGE = 120.0 | |
#define DEBOUNCE = 10000 | |
#define POWER_RELAY_1 = 2 | |
#define POWER_RELAY_2 = 13 | |
#define POWER_RELAY_TYPE = "ACTIVE_LOW" // ACTIVE_LOW or ACTIVE_HIGH | |
SoftwareSerial pzemSWSerial(4, 5); //rx tx 4 5 | |
PZEM004Tv30 pzem(pzemSWSerial); | |
SoftwareSerial pzemSWSerial2(12, 14); //rx tx 12 14 | |
PZEM004Tv30 pzem2(pzemSWSerial2); | |
void setup() { | |
/* Debugging serial */ | |
Serial.begin(115200); | |
pinMode(POWER_RELAY_1, OUTPUT); | |
pinMode(POWER_RELAY_2, OUTPUT); | |
digitalWrite(POWER_RELAY_1, HIGH); | |
digitalWrite(POWER_RELAY_2, HIGH); | |
} | |
void switchPower() | |
void loop() { | |
// Read the data from the sensor1 | |
float voltage = pzem.voltage(); | |
float current = pzem.current(); | |
float power = pzem.power(); | |
float energy = pzem.energy(); | |
float frequency = pzem.frequency(); | |
float pf = pzem.pf(); | |
// Read the data from the sensor2 | |
float voltage2 = pzem2.voltage(); | |
float current2 = pzem2.current(); | |
float power2 = pzem2.power(); | |
float energy2 = pzem2.energy(); | |
float frequency2 = pzem2.frequency(); | |
float pf2 = pzem2.pf(); | |
if (voltage > MIN_TRIGGER_VOLTAGE && voltage2 > MIN_TRIGGER_VOLTAGE) { | |
// Power is available on both line | |
Serial.println("Power is available on both line"); | |
} | |
if (voltage > MIN_TRIGGER_VOLTAGE && voltage2 < MIN_TRIGGER_VOLTAGE) { | |
digitalWrite(POWER_RELAY_1, LOW); | |
digitalWrite(POWER_RELAY_2, HIGH); | |
} | |
if (voltage2 > MIN_TRIGGER_VOLTAGE && voltage1 < MIN_TRIGGER_VOLTAGE) { | |
digitalWrite(POWER_RELAY_1, HIGH); | |
digitalWrite(POWER_RELAY_2, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment