Skip to content

Commit a42cb64

Browse files
committed
feat(zigbee): Add AC DC ElectricalMeasurement support
1 parent 6f92b60 commit a42cb64

File tree

13 files changed

+1927
-0
lines changed

13 files changed

+1927
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
301301
libraries/Zigbee/src/ep/ZigbeeWindSpeedSensor.cpp
302302
libraries/Zigbee/src/ep/ZigbeeIlluminanceSensor.cpp
303303
libraries/Zigbee/src/ep/ZigbeePM25Sensor.cpp
304+
libraries/Zigbee/src/ep/ZigbeeElectricalMeasurement.cpp
304305
)
305306

306307
set(ARDUINO_LIBRARY_BLE_SRCS
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Arduino-ESP32 Zigbee AC Electrical Measurement Example
2+
3+
This example shows how to configure the Zigbee router device and use it as a Home Automation (HA) AC electrical measurement device that reports voltage, current, power, and frequency measurements.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## AC Electrical Measurement Functions
13+
14+
* After this board first starts up, it would be configured locally to report AC electrical measurements:
15+
- AC voltage in volts (0-300.00V)
16+
- AC current in amps (0-10.000A)
17+
- AC power in watts (0-3200.0W)
18+
- AC frequency in hertz (0-65.000Hz)
19+
* Holding the button (BOOT) for more than 3 seconds will trigger a factory reset of the device.
20+
* The device reports measurements every 2 seconds with simulated values.
21+
22+
## Measurement Precision
23+
24+
The example demonstrates how to set up proper measurement precision using multiplier and divisor values:
25+
* Voltage: 1/100 = 0.01V (1 unit = 10mV)
26+
* Current: 1/1000 = 0.001A (1 unit = 1mA)
27+
* Power: 1/10 = 0.1W (1 unit = 100mW)
28+
* Frequency: 1/1000 = 0.001Hz (1 unit = 1mHz)
29+
30+
These settings ensure accurate reporting of measurements with proper decimal precision in the Zigbee network.
31+
32+
## Hardware Required
33+
34+
* A USB cable for power supply and programming
35+
36+
### Configure the Project
37+
38+
Set the ADC GPIO by changing the `analogPin` variable. By default, it's the pin `A0`.
39+
Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2).
40+
41+
#### Using Arduino IDE
42+
43+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
44+
45+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
46+
* Select the Router Zigbee mode: `Tools -> Zigbee mode: Zigbee Router`
47+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
48+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
49+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
50+
51+
## Troubleshooting
52+
53+
If the Router device flashed with this example is not connecting to the coordinator, erase the flash of the Router device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator.
54+
You can do the following:
55+
56+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
57+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
58+
59+
By default, the coordinator network is closed after rebooting or flashing new firmware.
60+
To open the network you have 2 options:
61+
62+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
63+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
64+
65+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
66+
67+
* **LED not blinking:** Check the wiring connection and the IO selection.
68+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
69+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
70+
71+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
72+
73+
## Contribute
74+
75+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
76+
77+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
78+
79+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
80+
81+
## Resources
82+
83+
* Official ESP32 Forum: [Link](https://esp32.com)
84+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
85+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
86+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
87+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @brief This example demonstrates Zigbee electrical AC measurement device.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a router device that measures
19+
* AC electrical parameters like voltage, current, power, power factor and frequency.
20+
*
21+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
22+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
23+
*
24+
* Please check the README.md for instructions and more detailed description.
25+
*
26+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
27+
*/
28+
29+
// Recommended to use Router mode, as this type of device is expected to be mains powered.
30+
#ifndef ZIGBEE_MODE_ZCZR
31+
#error "Zigbee coordinator/router mode is not selected in Tools->Zigbee mode"
32+
#endif
33+
34+
#include "Zigbee.h"
35+
36+
/* Zigbee AC measurement device configuration */
37+
#define AC_ELECTRICAL_MEASUREMENT_ENDPOINT_NUMBER 1
38+
39+
uint8_t analogPin = A0;
40+
uint8_t button = BOOT_PIN;
41+
42+
ZigbeeElectricalMeasurement zbElectricalMeasurement = ZigbeeElectricalMeasurement(AC_ELECTRICAL_MEASUREMENT_ENDPOINT_NUMBER);
43+
44+
void onAnalogOutputChange(float analog_output) {
45+
Serial.printf("Received analog output change: %.1f\r\n", analog_output);
46+
}
47+
48+
void setup() {
49+
Serial.begin(115200);
50+
Serial.println("Starting...");
51+
52+
// Init button switch
53+
pinMode(button, INPUT_PULLUP);
54+
55+
// Set analog resolution to 10 bits
56+
analogReadResolution(10);
57+
58+
// Optional: set Zigbee device name and model
59+
zbElectricalMeasurement.setManufacturerAndModel("Espressif", "ZigbeeElectricalMeasurementAC");
60+
61+
// Add analog clusters to Zigbee Analog according your needs
62+
zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A);
63+
zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A);
64+
zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A);
65+
zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER_FACTOR, ZIGBEE_AC_PHASE_TYPE_A);
66+
zbElectricalMeasurement.addACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC); // frequency is not phase specific (shared)
67+
68+
// Optional: set Multiplier/Divisor for the measurements
69+
zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, 1, 100); // 1/100 = 0.01V (1 unit of measurement = 0.01V = 10mV)
70+
zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, 1, 1000); // 1/1000 = 0.001A (1 unit of measurement = 0.001A = 1mA)
71+
zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, 1, 10); // 1/10 = 0.1W (1 unit of measurement = 0.1W = 100mW)
72+
zbElectricalMeasurement.setACMultiplierDivisor(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, 1, 1000); // 1/1000 = 0.001Hz (1 unit of measurement = 0.001Hz = 1mHz)
73+
74+
// Optional: set Min/max values for the measurements
75+
zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, 0, 30000); // 0-300.00V
76+
zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, 0, 10000); // 0-10.000A
77+
zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, 0, 32000); // 0-3200.0W
78+
zbElectricalMeasurement.setACMinMaxValue(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, 0, 65000); // 0-65.000Hz
79+
80+
// Optional: set power factor for the measurements
81+
zbElectricalMeasurement.setACPowerFactor(ZIGBEE_AC_PHASE_TYPE_A, 98); // 100 = 1.00, 0 = 0.00, -100 = -1.00
82+
83+
// Add endpoints to Zigbee Core
84+
Zigbee.addEndpoint(&zbElectricalMeasurement);
85+
86+
Serial.println("Starting Zigbee...");
87+
// When all EPs are registered, start Zigbee in Router mode
88+
if (!Zigbee.begin(ZIGBEE_ROUTER)) {
89+
Serial.println("Zigbee failed to start!");
90+
Serial.println("Rebooting...");
91+
ESP.restart();
92+
} else {
93+
Serial.println("Zigbee started successfully!");
94+
}
95+
Serial.println("Connecting to network");
96+
while (!Zigbee.connected()) {
97+
Serial.print(".");
98+
delay(100);
99+
}
100+
Serial.println("Connected");
101+
102+
// Optional: Add reporting for AC measurements (this is overriden by HomeAssistant ZHA if used as a Zigbee coordinator)
103+
zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 10); // report every 30 seconds if value changes by 10 (0.1V)
104+
zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 100); // report every 30 seconds if value changes by 10 (0.1A)
105+
zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, 0, 30, 10); // report every 30 seconds if value changes by 10 (1W)
106+
zbElectricalMeasurement.setACReporting(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, 0, 30, 100); // report every 30 seconds if value changes by 100 (0.1Hz)
107+
}
108+
109+
void loop() {
110+
static uint32_t timeCounter = 0;
111+
112+
static uint8_t randomizer = 0;
113+
// Read ADC value as current to demonstrate the measurements and update the electrical measurement values every 2s
114+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
115+
uint16_t voltage = 23000 + randomizer; // 230.00 V
116+
uint16_t current = analogReadMilliVolts(analogPin); // demonstrates 0-3.3A
117+
int16_t power = ((voltage/100) * (current/1000) * 10); //calculate power in W
118+
uint16_t frequency = 50135; // 50.000 Hz
119+
Serial.printf("Updating AC voltage to %d (0.01V), current to %d (mA), power to %d (0.1W), frequency to %d (mHz)\r\n", voltage, current, power, frequency);
120+
121+
// Update the measurements
122+
zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A, voltage);
123+
zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A, current);
124+
zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A, power);
125+
zbElectricalMeasurement.setACMeasurement(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC, frequency);
126+
127+
// Report the measurements
128+
zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_VOLTAGE, ZIGBEE_AC_PHASE_TYPE_A);
129+
zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_CURRENT, ZIGBEE_AC_PHASE_TYPE_A);
130+
zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_POWER, ZIGBEE_AC_PHASE_TYPE_A);
131+
zbElectricalMeasurement.reportAC(ZIGBEE_AC_MEASUREMENT_TYPE_FREQUENCY, ZIGBEE_AC_PHASE_TYPE_NON_SPECIFIC);
132+
133+
randomizer+=10;
134+
}
135+
136+
// Checking button for factory reset and reporting
137+
if (digitalRead(button) == LOW) { // Push button pressed
138+
// Key debounce handling
139+
delay(100);
140+
int startTime = millis();
141+
while (digitalRead(button) == LOW) {
142+
delay(50);
143+
if ((millis() - startTime) > 3000) {
144+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
145+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
146+
delay(1000);
147+
Zigbee.factoryReset();
148+
}
149+
}
150+
}
151+
delay(100);
152+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee_zczr,ZigbeeMode=zczr",
3+
"requires": [
4+
"CONFIG_ZB_ENABLED=y"
5+
]
6+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Arduino-ESP32 Zigbee AC Electrical MultiPhase Measurement Example
2+
3+
This example shows how to configure the Zigbee router device and use it as a Home Automation (HA) AC electrical measurement device that reports voltage, current, power, and frequency measurements across three phases.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## AC Electrical Measurement Functions
13+
14+
* After this board first starts up, it would be configured locally to report AC electrical measurements:
15+
- AC voltage in volts (0-300.00V) for each phase (A, B, C)
16+
- AC current in amps (0-10.000A) for each phase (A, B, C)
17+
- AC power in watts (0-3200.0W) for each phase (A, B, C)
18+
- AC frequency in hertz (0-65.000Hz) shared across all phases
19+
* Holding the button (BOOT) for more than 3 seconds will trigger a factory reset of the device.
20+
* The device reports measurements every 2 seconds with simulated values.
21+
22+
## Measurement Precision
23+
24+
The example demonstrates how to set up proper measurement precision using multiplier and divisor values:
25+
* Voltage: 1/100 = 0.01V (1 unit = 10mV)
26+
* Current: 1/1000 = 0.001A (1 unit = 1mA)
27+
* Power: 1/10 = 0.1W (1 unit = 100mW)
28+
* Frequency: 1/1000 = 0.001Hz (1 unit = 1mHz)
29+
30+
These settings ensure accurate reporting of measurements with proper decimal precision in the Zigbee network.
31+
32+
## Hardware Required
33+
34+
* A USB cable for power supply and programming
35+
36+
### Configure the Project
37+
38+
Set the ADC GPIO by changing the `analogPin` variable. By default, it's the pin `A0`.
39+
Set the Button GPIO by changing the `button` variable. By default, it's the pin `BOOT_PIN` (BOOT button on ESP32-C6 and ESP32-H2).
40+
41+
#### Using Arduino IDE
42+
43+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
44+
45+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
46+
* Select the Router Zigbee mode: `Tools -> Zigbee mode: Zigbee Router`
47+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
48+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
49+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
50+
51+
## Troubleshooting
52+
53+
If the Router device flashed with this example is not connecting to the coordinator, erase the flash of the Router device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator.
54+
You can do the following:
55+
56+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
57+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
58+
59+
By default, the coordinator network is closed after rebooting or flashing new firmware.
60+
To open the network you have 2 options:
61+
62+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
63+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
64+
65+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
66+
67+
* **LED not blinking:** Check the wiring connection and the IO selection.
68+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
69+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
70+
71+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
72+
73+
## Contribute
74+
75+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
76+
77+
If you have any **feedback** or **issue** to report on this example/library, please open an issue or fix it by creating a new PR. Contributions are more than welcome!
78+
79+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
80+
81+
## Resources
82+
83+
* Official ESP32 Forum: [Link](https://esp32.com)
84+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
85+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
86+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
87+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)

0 commit comments

Comments
 (0)