MycilaPZEM004Tv3
Arduino / ESP32 library for the PZEM-004T v3 Power and Energy monitor
Please read this good article first:
And also have a look at this inspiring and well documented GitHub project:
This project is an adaptation of the project above, focusing only on ESP32 and add support for async reading and async energy reset.
- Support multiple PZEM-004T v3 devices on the same RX/TX port
- Support reading and setting addresses of PZEM-004T v3 devices
- Blocking and non-blocking mode
- Core, stack size, priority and read timeout can be configured
- Automatically detect / search for PZEM-004T v3 devices
- Energy reset live at runtime, in both async and normal mode
- Available metrics:
volatile float current = 0; // A
volatile float energy = 0; // kWh
volatile float power = 0; // W
volatile float powerFactor = 0;
volatile float voltage = 0; // V
volatile uint8_t frequency = 0; // Hz
Usage
Have a look at all the examples in the examples folder.
There is a getter for each metric.
Blocking mode
Mycila::PZEM pzem;
void setup() {
pzem.begin(&Serial1, 14, 27);
}
void loop() {
if (pzem.read()) {
// access values
}
delay(1000);
}
Non-Blocking mode (async)
Mycila::PZEM pzem;
void setup() {
pzem.begin(&Serial1, 14, 27, MYCILA_PZEM_DEFAULT_ADDRESS, true);
}
Calling read() is not necessary.
Energy reset
pzem.resetEnergy();
Read and set address
pzem.readAddress();
pzem.readAddress(true); // will read teh address and use it as the new address
pzem.setAddress(0x42);
Start a PZEM with a specific address
pzem.begin(&Serial1, 14, 27, address);
Json support
Json support is optional.
If you need it, please add this compilation flag to activate it: -D MYCILA_PZEM_JSON_SUPPORT
and do not forget to include the ArduinoJson library:
#include <ArduinoJson.h>
Multiple PZEM-004T v3 devices on the same RX/TX port
- Connect the first PZEM-004T v3 to the RX/TX port of the ESP32
- Restart and set the address of the first PZEM-004T v3 (you can use the
SetAddress.ino
file) - Disconnect the first PZEM-004T v3 and connect the second one instead
- Restart and set the address of the second PZEM-004T v3
- Connect both PZEM-004T v3 to the RX/TX port of the ESP32
- Restart and use both with their own address