Hi all.
I have this following sub-routine that checks for data from a PZEM mains voltage monitor. It all works fine.
But, I have five of these modules on the serial bus and for the life of me, I cannot work out how to set up the required arrays so that I can simply use a single routine as I have below, but just change the pzem number (e.g pzem1, pzem2).
Rather than what I currently have, which is this routine repeated 5 times.
Any guidance would be great. I have spent several evenings trying to work it out, but to no avail. Arrays are not my friends
I have this following sub-routine that checks for data from a PZEM mains voltage monitor. It all works fine.
But, I have five of these modules on the serial bus and for the life of me, I cannot work out how to set up the required arrays so that I can simply use a single routine as I have below, but just change the pzem number (e.g pzem1, pzem2).
Rather than what I currently have, which is this routine repeated 5 times.
Code:
void ReadPZEM1() {
Voltage = pzem1.voltage();
if (!isnan(Voltage)) {
Serial.print(F("PZEM1 Voltage: "));
Serial.print(Voltage);
Serial.println(F("V"));
} else {
Serial.println(F("Error reading PZEM1 voltage"));
}
Current = pzem1.current();
if (!isnan(Current)) {
Serial.print(F("PZEM1 Current: "));
Serial.print(Current);
Serial.println(F("A"));
} else {
Serial.println(F("Error reading PZEM1 current"));
}
Power = pzem1.power();
if (!isnan(Power)) {
Serial.print(F("PZEM1 Power: "));
Serial.print(Power);
Serial.println(F("W"));
} else {
Serial.println(F("Error reading PZEM1 power"));
}
Energy = pzem1.energy();
if (!isnan(Energy)) {
Serial.print(F("PZEM1 Energy: "));
Serial.print(Energy, 3);
Serial.println(F("kWh"));
} else {
Serial.println(F("Error reading PZEM1 energy"));
}
Frequency = pzem1.frequency();
if (!isnan(Frequency)) {
Serial.print(F("PZEM1 Frequency: "));
Serial.print(Frequency, 1);
Serial.println(F("Hz"));
} else {
Serial.println(F("Error reading PZEM1 frequency"));
}
Pf = pzem1.pf();
if (!isnan(Pf)) {
Serial.print(F("PZEM1 PF: "));
Serial.println(Pf);
} else {
Serial.println(F("Error reading PZEM1 power factor"));
}
Serial.println();
}
Any guidance would be great. I have spent several evenings trying to work it out, but to no avail. Arrays are not my friends