Skip to content

Commit 659de82

Browse files
authored
feat(zigbee): Add analog value, input and output support
1 parent 81c26b9 commit 659de82

File tree

10 files changed

+300
-217
lines changed

10 files changed

+300
-217
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
293293
libraries/Zigbee/src/ep/ZigbeeContactSwitch.cpp
294294
libraries/Zigbee/src/ep/ZigbeeDoorWindowHandle.cpp
295295
libraries/Zigbee/src/ep/ZigbeeWindowCovering.cpp
296+
libraries/Zigbee/src/ep/ZigbeeAnalog.cpp
296297
)
297298

298299
set(ARDUINO_LIBRARY_BLE_SRCS

libraries/Zigbee/examples/Zigbee_Analog_Sensor/README.md renamed to libraries/Zigbee/examples/Zigbee_Analog_Input_Output/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Arduino-ESP32 Zigbee Analog Sensor Example
1+
# Arduino-ESP32 Zigbee Analog Input Output Example
22

3-
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) simple sensor device type with analog measuring.
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) analog input/output device.
44

55
# Supported Targets
66

@@ -11,7 +11,7 @@ Currently, this example supports the following targets.
1111

1212
## Analog Sensor Functions
1313

14-
* After this board first starts up, it would be configured locally to report an analog value on change or every 30 seconds.
14+
* After this board first starts up, it would be configured locally to report an analog input on change or every 30 seconds.
1515
* By clicking the button (BOOT) on this board, this board will immediately send a report of the current measured value to the network.
1616

1717
## Hardware Required
@@ -20,7 +20,7 @@ Currently, this example supports the following targets.
2020

2121
### Configure the Project
2222

23-
In this example, the internal temperature sensor is used to demonstrate reading of the analog sensors.
23+
Set the ADC GPIO by changing the `analogPin` variable. By default, it's the pin `A0`.
2424
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).
2525

2626
#### Using Arduino IDE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 analog input / output device.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device analog device.
19+
*
20+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
21+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
22+
*
23+
* Please check the README.md for instructions and more detailed description.
24+
*
25+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
26+
* Modified by Pat Clay
27+
*/
28+
29+
#ifndef ZIGBEE_MODE_ED
30+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
31+
#endif
32+
33+
#include "Zigbee.h"
34+
35+
/* Zigbee analog device configuration */
36+
#define ANALOG_DEVICE_ENDPOINT_NUMBER 1
37+
38+
uint8_t analogPin = A0;
39+
uint8_t button = BOOT_PIN;
40+
41+
ZigbeeAnalog zbAnalogDevice = ZigbeeAnalog(ANALOG_DEVICE_ENDPOINT_NUMBER);
42+
43+
void onAnalogOutputChange(float analog_output) {
44+
Serial.printf("Received analog output change: %.1f\r\n", analog_output);
45+
}
46+
47+
void setup() {
48+
Serial.begin(115200);
49+
Serial.println("Starting...");
50+
51+
// Init button switch
52+
pinMode(button, INPUT_PULLUP);
53+
54+
// Set analog resolution to 10 bits
55+
analogReadResolution(10);
56+
57+
// Optional: set Zigbee device name and model
58+
zbAnalogDevice.setManufacturerAndModel("Espressif", "ZigbeeAnalogDevice");
59+
60+
// Add analog sensor cluster to Zigbee Analog accoding your needs
61+
zbAnalogDevice.addAnalogInput();
62+
zbAnalogDevice.addAnalogOutput();
63+
64+
// If analog output cluster is added, set callback function for analog output change
65+
zbAnalogDevice.onAnalogOutputChange(onAnalogOutputChange);
66+
67+
// Add endpoints to Zigbee Core
68+
Zigbee.addEndpoint(&zbAnalogDevice);
69+
70+
Serial.println("Starting Zigbee...");
71+
// When all EPs are registered, start Zigbee in End Device mode
72+
if (!Zigbee.begin()) {
73+
Serial.println("Zigbee failed to start!");
74+
Serial.println("Rebooting...");
75+
ESP.restart();
76+
} else {
77+
Serial.println("Zigbee started successfully!");
78+
}
79+
Serial.println("Connecting to network");
80+
while (!Zigbee.connected()) {
81+
Serial.print(".");
82+
delay(100);
83+
}
84+
Serial.println("Connected");
85+
86+
// Optional: Add reporting for analog input
87+
zbAnalogDevice.setAnalogInputReporting(0, 30, 10); // report every 30 seconds if value changes by 10
88+
}
89+
90+
void loop() {
91+
static uint32_t timeCounter = 0;
92+
93+
// Read ADC value and update the analog value every 2s
94+
if (!(timeCounter++ % 20)) { // delaying for 100ms x 20 = 2s
95+
float analog = (float)analogRead(analogPin);
96+
Serial.printf("Updating analog input to %.1f\r\n", analog);
97+
zbAnalogDevice.setAnalogInput(analog);
98+
99+
// Analog input supports reporting
100+
zbAnalogDevice.reportAnalogInput();
101+
}
102+
103+
// Checking button for factory reset and reporting
104+
if (digitalRead(button) == LOW) { // Push button pressed
105+
// Key debounce handling
106+
delay(100);
107+
int startTime = millis();
108+
while (digitalRead(button) == LOW) {
109+
delay(50);
110+
if ((millis() - startTime) > 3000) {
111+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
112+
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
113+
delay(1000);
114+
Zigbee.factoryReset();
115+
}
116+
}
117+
}
118+
delay(100);
119+
}
120+

libraries/Zigbee/examples/Zigbee_Analog_Sensor/Zigbee_Analog_Sensor.ino

-108
This file was deleted.

libraries/Zigbee/src/Zigbee.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "ep/ZigbeeTempSensor.h"
1616
#include "ep/ZigbeeThermostat.h"
1717
#include "ep/ZigbeePressureSensor.h"
18-
#include "ep/ZigbeeAnalogSensor.h"
18+
#include "ep/ZigbeeAnalog.h"
1919
#include "ep/ZigbeeFlowSensor.h"
2020
#include "ep/ZigbeeOccupancySensor.h"
2121
#include "ep/ZigbeeCarbonDioxideSensor.h"

0 commit comments

Comments
 (0)