Hi all. What am I doing wrong with this array?
My code is huge, so I will post the relevant bits here and hopefully someone can see the error...
First I declare:
#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(&Serial3, 0xF8); // This is only used for the initial address programming
PZEM004Tv30 pzem1(&Serial3, 0x01);
PZEM004Tv30 pzem2(&Serial3, 0x02);
PZEM004Tv30 pzem3(&Serial3, 0x03);
PZEM004Tv30 pzem4(&Serial3, 0x04);
PZEM004Tv30 pzem5(&Serial3, 0x05);
Then set up the array etc:
#define NUM_PZEM 5
PZEM004Tv30 *pzem_array[NUM_PZEM] = { &pzem1, &pzem2, &pzem3, &pzem4, &pzem5 };
float Voltage;
float Power;
float Frequency;
float Energy;
float Current;
float Pf;
boolean NoACdata = 0;
You then have to apply 230v power to the 5x modules individually, and re-address them to be 0x01, 0x02, 0x03, 0x04 and 0x05 (They start at default 0xF8).
I then quiz the modules with Serial.println(pzem#.getAddress()); to confirm the address has changed.
I set up a thread to check this data once a second (all working fine):
threads.addThread(ReadPZEM);
Then, when I call my routine to check the data one at a time, I get issues. It locks up trying to read module 5 (00x5). This is an array issue. if I mess around with the array, I can get to read the fifth module, but then they all seem to get out of step.
void ReadPZEM(int index) { // Index is the module we are reading
while (1) {
NoACdata = 0;
index++;
if (index > 5) {
index = 1;
}
threads.delay(1000); // Sampling time
Voltage = pzem_array[index-1]->voltage();
if (!isnan(Voltage)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" Voltage: "));
Serial.print(Voltage);
Serial.println(F("V"));
} else {
Serial.print(F("No live supply on input: "));
Serial.print(index);
NoACdata = 1;
}
Current = pzem_array[index-1]->current();
if (!isnan(Current)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" Current: "));
Serial.print(Current);
Serial.println(F("A"));
} else if (NoACdata == 0) {
Serial.print(F("Error reading PZEM"));
Serial.print(index);
Serial.println(F(" current"));
}
Power = pzem_array[index-1]->power();
if (!isnan(Power)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" Power: "));
Serial.print(Power);
Serial.println(F("W"));
} else if (NoACdata == 0) {
Serial.print(F("Error reading PZEM"));
Serial.print(index);
Serial.println(F(" power"));
}
Energy = pzem_array[index-1]->energy();
if (!isnan(Energy)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" Energy: "));
Serial.print(Energy);
Serial.println(F("kWh"));
} else if (NoACdata == 0) {
Serial.print(F("Error reading PZEM"));
Serial.print(index);
Serial.println(F(" energy"));
}
Frequency = pzem_array[index-1]->frequency();
if (!isnan(Frequency)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" Frequency: "));
Serial.print(Frequency);
Serial.println(F("Hz"));
} else if (NoACdata == 0) {
Serial.print(F("Error reading PZEM"));
Serial.print(index);
Serial.println(F(" frequency"));
}
Pf = pzem_array[index-1]->pf();
if (!isnan(Pf)) {
Serial.print(F("PZEM"));
Serial.print(index);
Serial.print(F(" PF: "));
Serial.println(Pf);
} else if (NoACdata == 0) {
Serial.print(F("Error reading PZEM"));
Serial.print(index);
Serial.println(F(" power factor"));
}
Serial.println(" ");
threads.yield();
}
}
What have I messed up where?
I have spent HOURS on this. I think I may just go back to 5x the same routine as that works.
The array index was originally set outside this routine in the main loop, but I have moved it into this subroutine for now.