Sensor PZEM004T - xoseperez/espurna GitHub Wiki
Property | Value |
---|---|
Manufacturer | PEACEFAIR |
Product page | AliExpress |
Wiki page | ESPurna Wiki |
Build flag |
PZEM004T_SUPPORT or an older version, PZEM004TV30_SUPPORT for the newer one |
These are cheap Energy monitors devices, equipped with a TTL UART communication interface, it can monitor Voltage, Current, Power, and Energy.
NB.: These devices are 5V, usually, ESP devices are using ESP8266 which is 5V tolerant [1], Sonoff Basic devices have 5V onboard, so you don't need to mod your PZEM004T to work with ESP8266 devices.
You only need to connect 4 pins to ESPurna device to work:
- VCC
- TX
- RX
- GND
ESPurna's GPIO vary among devices, check the documentation of your device.
You must set the build flag PZEM004T_SUPPORT
or PZEM004TV30_SUPPORT
to 1
to enable this sensor.
Nothing is needed to be done in this single configuration, the device address will be set to the device on power on, works out of the box.
See PR #2283
Multiple devices should be similarly supported, but were never implemented or tested. This requires manually changing each device's address to something unique and adding additional code instantiating 2nd (or 3rd, or 4th) sensor.
Default address is set to the stock value. When using PZ.ADDRESS
, don't forget to update this value too in case of the factory reset.
#ifndef PZEM004TV30_ADDRESS
#define PZEM004TV30_ADDRESS 0xF8
#endif
By default, sensor configuration will use the first available port:
// configures uart port
#define UART_SUPPORT 1
#define UART1_BAUDRATE 9600
#define UART1_RX_PIN 15
#define UART1_TX_PIN 15
// configures sensor on the 1st port
#define PZEM004TV30_SUPPORT 1
// replace default UART0 config with a software port
#define UART_SUPPORT 1
#define UART_SOFTWARE_SUPPORT 1
#define UART1_BAUDRATE 9600
#define UART1_RX_PIN 4
#define UART1_TX_PIN 5
#define PZEM004TV30_SUPPORT 1
#define PZEM004TV30_PORT 1
// UART2_... is configured elsewhere
#define PZEM004TV30_PORT 2
Integer, ms. How much to wait between receiving serial data. Defaults to flag value (200 ms)
Boolean, 0 or 1. Enable additional debug messages for Modbus protocol and it's responses. Disabled by default.
Integer, 1...247. Configures the Modbus address, defaults to the flag value. Does not actively change the address, it needs to be updated by successfully calling pz.address <address>
.
Set the Modbus address, any number from 1 to 247. By default, device comes pre-set with the address 0xF8, which cannot be set manually.
Differently than the first implementation (See https://github.com/xoseperez/espurna/commit/3704d366637), this version (See PR #1304) pools
device(s) magnitudes every PZEM004T_READ_INTERVAL
. A magnitude of a device is read on each sensor tick
.
This will offload the main thread, as the PZEM004T can take up to 1000ms to answer the request. Same
magnitudes are read one after another, so they can be better synced.
Each PZEM004T device has an internal device address(Coded in the form of an IP Address but doesn't need to conform, you can have 0.0.0.0 for example), which can be used to create a pseudo-master/slave bus, allowing to connect multiple devices to the same UART, thereby eliminating software serials, etc.
ESPurna firmware has all the instrumentation to build this kind of bus.
To create a master/slave UART bus, you need a resistor(10K) and some Small Signal Schottky Diode(Like BAT43), the connection diagram is the following:
You need to specify all available addresses in config/sensors.h
#define PZEM004T_ADDRESSES "1.1.1.1 2.2.2.2 3.3.3.3"
By default, sensor configuration will use the first available port:
// configures uart port
#define UART_SUPPORT 1
#define UART1_BAUDRATE 9600
#define UART1_RX_PIN 15
#define UART1_TX_PIN 15
// configures sensor on the 1st port
#define PZEM004T_SUPPORT 1
Stock device address is unset! You either need to configure these addresses using the vendor software or by using PZ.ADDRESS
command (see below).
PZEM004T sensor implementation provides 3 commands to manage the device(s). When specified ADDRESS
,
it is the address set on the device, like 192.168.1.1
, 1.1.1.1
, etc. When specified DEVICE_INDEX
,
is the internal's device index, the order is defined by the addresses in the PZEM004T_ADDRESSES
build
flag. The command's output is: Device DEVICE_INDEX/ADDRESS
.
Allows you to read and to set the address.
Usage: pz.address [ADDRESS]
If ADDRESS
is not provided, it will read the addresses of the devices configured in the firmware, eg:
pz.address
192.168.1.1
192.168.1.2
192.168.1.3
+OK
If ADDRESS
is provided, it will issue a set address
command to the bus. If the command doesn't return
a +OK
the command has failed.
pz.address 192.168.1.1
+OK
NB.: Keep in mind that you must have ONLY ONE device connected to the bus when issuing this command, otherwise, all devices will have the same address and a collision will occur on the response, the result is undetermined.