Closed micheldufresne closed 1 month ago
When you assign the addresses, are you making sure to only have the one PZEM connected on the bus and attached to the AC power supply at a time?
Hello, Yes I am sure. Il solve my problem using IDE arduino and this code :
/* Origin Copyright (c) 2021 Jakub Mandula Copyright (c) 2022 Cyril Poissonnier
Each PZEM modules has two addresses. A default general address (0xF8), which every module will listen on by default. And a custom address.
The custom address can be used if multiple PZEM modules are to be used on a single ModBUS.
This script will use the general address in order to set the custom address of the connected PZEM module.
Therefore make sure only one PZEM module is connected at a time! Otherwise all connected modules will receive the same custom address.
the address can be change by typing new address in serial monitor.
*/
PZEM004Tv30 pzem(Serial1, PZEM_RX_PIN, PZEM_TX_PIN);
/***
String reading;
void setup() { Serial.begin(115200); }
void loop() { static uint8_t addr = SET_ADDRESS;
// Print out current custom address
Serial.print("Previous address: 0x");
Serial.println(pzem.readAddress(), HEX);
Serial.println("enter new address: ( 1 - 254 )"); //Prompt User for
input while (Serial.available()==0) {} //Wait for user input reading = Serial.readString(); //Read user input and hold it in a variable int intread = reading.toInt(); addr = intread;
// Set the custom address
Serial.print("Setting address to: 0x");
Serial.println(addr, HEX);
if(!pzem.setAddress(addr))
{
// Setting custom address failed. Probably no PZEM connected
Serial.println("Error setting address.");
} else {
// Print out the new custom address
Serial.print("Current address: 0x");
Serial.println(pzem.readAddress(), HEX);
Serial.println();
}
delay(1000); affichePzem(pzem);
delay(5000); }
void affichePzem(PZEM004Tv30 pz) { Serial.print("Custom Address:"); Serial.println(pz.readAddress(), HEX);
// Read the data from the sensor
float voltage = pz.voltage();
float current = pz.current();
float power = pz.power();
float energy = pz.energy();
float frequency = pz.frequency();
float pf = pz.pf();
// Check if the data is valid
if(isnan(voltage)){
Serial.println("Error reading voltage");
} else if (isnan(current)) {
Serial.println("Error reading current");
} else if (isnan(power)) {
Serial.println("Error reading power");
} else if (isnan(energy)) {
Serial.println("Error reading energy");
} else if (isnan(frequency)) {
Serial.println("Error reading frequency");
} else if (isnan(pf)) {
Serial.println("Error reading power factor");
} else {
float coutjournee=power*0.0036;
// Print the values to the Serial console
Serial.printf("Voltage: %5.1f V ",voltage);//
Serial.print(voltage); Serial.println("V"); Serial.printf("Current: %4.2f A ",current);// Serial.print(current); Serial.println("A"); Serial.printf("soit %.3f € par jour\n",coutjournee); Serial.printf("Power: %6.2f W ",power); // Serial.print(power); Serial.println("W"); Serial.printf("Energy: %7.3f kWh\n",energy);// Serial.print(energy,3); Serial.println("kWh");
Serial.printf("Frequency: %4.1f Hz ",frequency); //
Serial.print(frequency, 1); Serial.println("Hz"); Serial.printf("PF: %.1f\n",pf); // Serial.println(pf);
}
Serial.println();
}
and not this one with visualcode :
/ An example on how to use ESP32 hardware serial with PZEM004T /
PZEM004Tv30 pzemLaveVaisselle(Serial1,pinRX,pinTX,0x05);
void initpzem(PZEM004Tv30 pz,uint8_t addr) { delay(1000); pz.setAddress(addr); delay(1000); Serial.print("Current address:"); Serial.println(pz.getAddress()); Serial.println(); delay(3000); }
void setup() { Serial.begin(115200); initpzem(pzemLaveVaisselle); }
void affichePzem(PZEM004Tv30 pz) { Serial.print("Custom Address:"); Serial.println(pz.readAddress(), HEX);
// Read the data from the sensor
float voltage = pz.voltage();
float current = pz.current();
float power = pz.power();
float energy = pz.energy();
float frequency = pz.frequency();
float pf = pz.pf();
// Check if the data is valid
if(isnan(voltage)){
Serial.println("Error reading voltage");
} else if (isnan(current)) {
Serial.println("Error reading current");
} else if (isnan(power)) {
Serial.println("Error reading power");
} else if (isnan(energy)) {
Serial.println("Error reading energy");
} else if (isnan(frequency)) {
Serial.println("Error reading frequency");
} else if (isnan(pf)) {
Serial.println("Error reading power factor");
} else {
float coutjournee=power*0.0036;
// Print the values to the Serial console
Serial.printf("Voltage: %5.1f V ",voltage);//
Serial.print(voltage); Serial.println("V"); Serial.printf("Current: %4.2f A ",current);// Serial.print(current); Serial.println("A"); Serial.printf("soit %.3f € par jour\n",coutjournee); Serial.printf("Power: %6.2f W ",power); // Serial.print(power); Serial.println("W"); Serial.printf("Energy: %7.3f kWh\n",energy);// Serial.print(energy,3); Serial.println("kWh");
Serial.printf("Frequency: %4.1f Hz ",frequency); //
Serial.print(frequency, 1); Serial.println("Hz"); Serial.printf("PF: %.1f\n",pf); // Serial.println(pf);
}
Serial.println();
}
void loop() { Serial.print("laveVaisselle "); affichePzem(pzemLaveVaisselle); delay(1000); }
I have 6 pzem on the same uart and it's ok.
Thank you for your job
Michel DUFRESNE
Le mar. 28 mai 2024 à 13:33, Jakub Mandula ***@***.***> a
écrit :
> When you assign the addresses, are you making sure to only have the one
> PZEM connected on the bus and attached to the AC power supply at a time?
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/mandulaj/PZEM-004T-v30/issues/124#issuecomment-2134994262>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/ALPSWQJZI3RCBD2E3MQZXETZERTRLAVCNFSM6AAAAABH6WZQK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMZUHE4TIMRWGI>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
So does the setting of the address above 4
work now? What is the exact issue? If it has been solved could you please close the issue?
It's okay now
Hello I bought 3 pzem-004t 10A and then 3 others of 100A. I gave the addresses 1, 2, 3, 4 to the first but impossible to give the addresses 5 and 6. When I give an address >4 the pzem takes the address 1. I use this code: void initpzem(PZEM004Tv30 pz,uint8_t addr) { pz.setAddress(addr); delay(1000); Serial.print("Current address:"); Serial.println(pz.getAddress()); } Is this normal? Is it impossible to use 6 pzem? THANKS