Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5f8c8e2

Browse files
committedJan 30, 2025·
Added example Sketch.
1 parent 1d2c6c1 commit 5f8c8e2

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
 
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Arduino-ESP32 Widnow Covering Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) window covering or port.
4+
5+
To see if the communication with your Zigbee network works, use the Serial monitor and watch for output there.
6+
7+
# Supported Targets
8+
9+
Currently, this example supports the following targets.
10+
11+
| Supported Targets | ESP32-C6 | ESP32-H2 |
12+
| ----------------- | -------- | -------- |
13+
14+
## Hardware Required
15+
16+
* A USB cable for power supply and programming
17+
* Board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_Window_Covering example
18+
* Zigbee network / coordinator (Other board with switch examples or Zigbee2mqtt or ZigbeeHomeAssistant like application)
19+
20+
### Configure the Project
21+
22+
#### Using Arduino IDE
23+
24+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
25+
26+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
27+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
28+
* Select Tools / USB CDC On Boot: "Enabled"
29+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
30+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
31+
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.
32+
33+
## Troubleshooting
34+
35+
If the End device flashed with this example is not connecting to the coordinator, erase the flash of the End device before flashing the example to the board. It is recommended to do this if you re-flash the coordinator.
36+
You can do the following:
37+
38+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
39+
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.
40+
41+
By default, the coordinator network is closed after rebooting or flashing new firmware.
42+
To open the network you have 2 options:
43+
44+
* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
45+
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.
46+
47+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
48+
49+
* **LED not blinking:** Check the wiring connection and the IO selection.
50+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
51+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
52+
53+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
54+
55+
## Contribute
56+
57+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
58+
59+
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!
60+
61+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
62+
63+
## Resources
64+
65+
* Official ESP32 Forum: [Link](https://esp32.com)
66+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
67+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
68+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
69+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#ifndef ZIGBEE_MODE_ED
2+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
3+
#endif
4+
5+
#include "ZigbeeCore.h"
6+
#include "ep/ZigbeeWindowCovering.h"
7+
8+
9+
#define ZIGBEE_COVERING_ENDPOINT 10
10+
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
11+
12+
#define MAX_LIFT 200 // centimeters from open position
13+
#define MIN_LIFT 0
14+
15+
uint8_t currentLift = MAX_LIFT;
16+
uint8_t reportedLift = MIN_LIFT;
17+
18+
ZigbeeWindowCovering zbCovering = ZigbeeWindowCovering(ZIGBEE_COVERING_ENDPOINT);
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
23+
Serial.println("ESP32S3 initialization completed!");
24+
25+
// Init button for factory reset
26+
pinMode(BUTTON_PIN, INPUT_PULLUP);
27+
28+
//Optional: set Zigbee device name and model
29+
zbCovering.setManufacturerAndModel("Espriff", "Arduino");
30+
zbCovering.setCoveringType(ROLLERSHADE);
31+
32+
// operational, online, not commands_reversed, closed_loop, encoder_controlled
33+
zbCovering.setConfigStatus(true, true, false, true, true, true, true);
34+
35+
// not motor_reversed, calibration_mode, not maintenance_mode, not leds_on
36+
zbCovering.setMode(false, true, false, false);
37+
38+
zbCovering.setLimits(MIN_LIFT, MAX_LIFT, 0, 0);
39+
40+
// Set callback function for light change
41+
zbCovering.onGoToLiftPercentage(goToLiftPercentage);
42+
zbCovering.onStop(stopMotor);
43+
44+
//Add endpoint to Zigbee Core
45+
Serial.println("Adding ZigbeeWindowCovering endpoint to Zigbee Core");
46+
Zigbee.addEndpoint(&zbCovering);
47+
48+
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
49+
Serial.println("Calling Zigbee.begin()");
50+
if (!Zigbee.begin()) {
51+
Serial.println("Zigbee failed to start!");
52+
Serial.println("Rebooting...");
53+
ESP.restart();
54+
}
55+
Serial.println("Connecting to network");
56+
while (!Zigbee.connected()) {
57+
Serial.print(".");
58+
delay(100);
59+
}
60+
reportState();
61+
Serial.println();
62+
}
63+
64+
void loop() {
65+
// Checking button for factory reset
66+
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
67+
// Key debounce handling
68+
delay(100);
69+
int startTime = millis();
70+
while (digitalRead(BUTTON_PIN) == LOW) {
71+
delay(50);
72+
if ((millis() - startTime) > 3000) {
73+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
74+
Serial.printf("Resetting Zigbee to factory settings, reboot.\n");
75+
Zigbee.factoryReset();
76+
delay(30000);
77+
}
78+
}
79+
}
80+
81+
reportState();
82+
83+
delay(500);
84+
}
85+
86+
void goToLiftPercentage(uint8_t liftPercentage) {
87+
/* This is where you would trigger your motor to go towards liftPercentage, currentLift should
88+
be updated until liftPercentage has been reached in order to provide feedback to controller */
89+
90+
// Our cover updates instantly!
91+
currentLift = (liftPercentage * MAX_LIFT) / 100;
92+
93+
Serial.printf("New requsted lift from Zigbee: %d (%d)\n", currentLift, liftPercentage);
94+
}
95+
96+
void reportState() {
97+
if (reportedLift == currentLift) {
98+
return;
99+
}
100+
101+
Serial.printf("Reporting new state: %d (%d)\n", currentLift, reportedLift);
102+
103+
reportedLift = currentLift;
104+
105+
// This is where current state is reported to controller, to provide feedback on where the shade is at currently
106+
zbCovering.setLiftPosition((uint16_t) reportedLift);
107+
}
108+
109+
110+
void stopMotor() {
111+
// Motor can be stopped while moving cover toward current target
112+
Serial.println("Stopping motor");
113+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
3+
"requires": [
4+
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
5+
]
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.