Skip to content

Commit

Permalink
Created Zigbee Endpoint for Window Covering. (#10914)
Browse files Browse the repository at this point in the history
* feat(zigbee): Added Endpoint for Window Covering (#10913)

* Added example Sketch.

* Added window covering to CMakeLists.txt.

* feat(zigbee): Window covering tilt support and fixes

* fix(zigbee): Fix typos

* ci(pre-commit): Apply automatic fixes

* fix(ci): Fixes of typos

---------

Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 4, 2025
1 parent 6a11276 commit db0bbad
Show file tree
Hide file tree
Showing 9 changed files with 757 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ set(ARDUINO_LIBRARY_Zigbee_SRCS
libraries/Zigbee/src/ep/ZigbeeCarbonDioxideSensor.cpp
libraries/Zigbee/src/ep/ZigbeeContactSwitch.cpp
libraries/Zigbee/src/ep/ZigbeeDoorWindowHandle.cpp
libraries/Zigbee/src/ep/ZigbeeWindowCovering.cpp
)

set(ARDUINO_LIBRARY_BLE_SRCS
Expand Down
69 changes: 69 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Window_Covering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Arduino-ESP32 Window Covering Example

This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) window covering device.

To see if the communication with your Zigbee network works, use the Serial monitor and watch for output there.

# Supported Targets

Currently, this example supports the following targets.

| Supported Targets | ESP32-C6 | ESP32-H2 |
| ----------------- | -------- | -------- |

## Hardware Required

* A USB cable for power supply and programming
* Board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_Window_Covering example
* Zigbee network / coordinator (Other board with switch examples or Zigbee2mqtt or ZigbeeHomeAssistant like application)

### Configure the Project

#### Using Arduino IDE

To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).

* Before Compile/Verify, select the correct board: `Tools -> Board`.
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
* Select Tools / USB CDC On Boot: "Enabled"
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
* Optional: Set debug level to verbose to see all logs from Zigbee stack: `Tools -> Core Debug Level: Verbose`.

## Troubleshooting

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.
You can do the following:

* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
* Add to the sketch `Zigbee.factoryReset();` to reset the device and Zigbee stack.

By default, the coordinator network is closed after rebooting or flashing new firmware.
To open the network you have 2 options:

* Open network after reboot by setting `Zigbee.setRebootOpenNetwork(time);` before calling `Zigbee.begin();`.
* In application you can anytime call `Zigbee.openNetwork(time);` to open the network for devices to join.

***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***

* **LED not blinking:** Check the wiring connection and the IO selection.
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.

If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).

## Contribute

To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)

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!

Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.

## Resources

* Official ESP32 Forum: [Link](https://esp32.com)
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright 2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @brief This example demonstrates Zigbee Window Covering.
*
* The example demonstrates how to use Zigbee library to create a end device window covering device.
* The window covering is a Zigbee end device, which is moving the blinds (lift+tilt) and reporting
* its current position to the Zigbee network.
*
* Use setCoveringType() to set the type of covering (blind, shade, etc.).
*
* The example also demonstrates how to use the button to manually control the lift position.
*
* Proper Zigbee mode must be selected in Tools->Zigbee mode
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
*
* Please check the README.md for instructions and more detailed description.
*
* Created by hennikul and Jan Procházka (https://github.com/P-R-O-C-H-Y/)
*/

#ifndef ZIGBEE_MODE_ED
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
#endif

#include "ZigbeeCore.h"
#include "ep/ZigbeeWindowCovering.h"

#define ZIGBEE_COVERING_ENDPOINT 10
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button

#define MAX_LIFT 200 // centimeters from open position (0-900)
#define MIN_LIFT 0

#define MAX_TILT 40 // centimeters from open position (0-900)
#define MIN_TILT 0

uint16_t currentLift = MAX_LIFT;
uint8_t currentLiftPercentage = 100;

uint16_t currentTilt = MAX_TILT;
uint8_t currentTiltPercentage = 100;

ZigbeeWindowCovering zbCovering = ZigbeeWindowCovering(ZIGBEE_COVERING_ENDPOINT);

void setup() {
Serial.begin(115200);

// Init button for factory reset
pinMode(BUTTON_PIN, INPUT_PULLUP);

// Optional: set Zigbee device name and model
zbCovering.setManufacturerAndModel("Espressif", "WindowBlinds");

// Set proper covering type, it defines which attributes are available
zbCovering.setCoveringType(BLIND_LIFT_AND_TILT);

// Set configuration: operational, online, not commands_reversed, lift / tilt closed_loop, lift / tilt encoder_controlled
zbCovering.setConfigStatus(true, true, false, true, true, true, true);

// Set mode: not motor_reversed, calibration_mode, not maintenance_mode, not leds_on
zbCovering.setMode(false, true, false, false);

// Set limits of motion
zbCovering.setLimits(MIN_LIFT, MAX_LIFT, MIN_TILT, MAX_TILT);

// Set callback function for open, close, filt and tilt change, stop
zbCovering.onOpen(fullOpen);
zbCovering.onClose(fullClose);
zbCovering.onGoToLiftPercentage(goToLiftPercentage);
zbCovering.onGoToTiltPercentage(goToTiltPercentage);
zbCovering.onStop(stopMotor);

// Add endpoint to Zigbee Core
Serial.println("Adding ZigbeeWindowCovering endpoint to Zigbee Core");
Zigbee.addEndpoint(&zbCovering);

// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
Serial.println("Calling Zigbee.begin()");
if (!Zigbee.begin()) {
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
}
Serial.println("Connecting to network");
while (!Zigbee.connected()) {
Serial.print(".");
delay(100);
}
Serial.println();

// Set initial position
zbCovering.setLiftPercentage(currentLiftPercentage);
zbCovering.setTiltPercentage(currentTiltPercentage);
}

void loop() {
// Checking button for factory reset
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(BUTTON_PIN) == LOW) {
delay(50);
if ((millis() - startTime) > 3000) {
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Serial.printf("Resetting Zigbee to factory settings, reboot.\n");
Zigbee.factoryReset();
delay(30000);
}
}
// Manual lift control simulation by pressing button
manualControl();
}
delay(500);
}

void fullOpen() {
/* This is where you would trigger your motor to go to full open state, currentLift should
be updated until full open has been reached in order to provide feedback to controller of actual position
The stop can be always called, so the movement can be stopped at any time */

// Our cover updates instantly!
currentLift = MAX_LIFT;
currentLiftPercentage = 100;
Serial.println("Opening cover");
// Update the current position
zbCovering.setLiftPercentage(currentLiftPercentage);
}

void fullClose() {
/* This is where you would trigger your motor to go to full close state, currentLift should
be updated until full close has been reached in order to provide feedback to controller of actual position
The stop can be always called, so the movement can be stopped at any time */

// Our cover updates instantly!
currentLift = MIN_LIFT;
currentLiftPercentage = 0;
Serial.println("Closing cover");
// Update the current position
zbCovering.setLiftPercentage(currentLiftPercentage);
}

void goToLiftPercentage(uint8_t liftPercentage) {
/* This is where you would trigger your motor to go towards liftPercentage, currentLift should
be updated until liftPercentage has been reached in order to provide feedback to controller */

// Our simulated cover updates instantly!
currentLift = (liftPercentage * MAX_LIFT) / 100;
currentLiftPercentage = liftPercentage;
Serial.printf("New requested lift from Zigbee: %d (%d)\n", currentLift, liftPercentage);

// Update the current position
zbCovering.setLiftPercentage(currentLiftPercentage); //or setLiftPosition()
}

void goToTiltPercentage(uint8_t tiltPercentage) {
/* This is where you would trigger your motor to go towards tiltPercentage, currentTilt should
be updated until tiltPercentage has been reached in order to provide feedback to controller */

// Our simulated cover updates instantly!
currentTilt = (tiltPercentage * MAX_TILT) / 100;
currentTiltPercentage = tiltPercentage;
Serial.printf("New requested tilt from Zigbee: %d (%d)\n", currentTilt, tiltPercentage);

// Update the current position
zbCovering.setTiltPercentage(currentTiltPercentage); //or setTiltPosition()
}

void stopMotor() {
// Motor can be stopped while moving cover toward current target, when stopped the actual position should be updated
Serial.println("Stopping motor");
// Update the current position of both lift and tilt
zbCovering.setLiftPercentage(currentLiftPercentage);
zbCovering.setTiltPercentage(currentTiltPercentage);
}

void manualControl() {
// Simulate lift percentage move by increasing it by 20% each time
currentLiftPercentage += 20;
if (currentLiftPercentage > 100) {
currentLiftPercentage = 0;
}
zbCovering.setLiftPercentage(currentLiftPercentage);
// Also setLiftPosition() can be used to set the exact position instead of percentage
}
6 changes: 6 additions & 0 deletions libraries/Zigbee/examples/Zigbee_Window_Covering/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"fqbn_append": "PartitionScheme=zigbee,ZigbeeMode=ed",
"requires": [
"CONFIG_SOC_IEEE802154_SUPPORTED=y"
]
}
1 change: 1 addition & 0 deletions libraries/Zigbee/src/Zigbee.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
#include "ep/ZigbeeCarbonDioxideSensor.h"
#include "ep/ZigbeeContactSwitch.h"
#include "ep/ZigbeeDoorWindowHandle.h"
#include "ep/ZigbeeWindowCovering.h"
2 changes: 1 addition & 1 deletion libraries/Zigbee/src/ZigbeeEP.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class ZigbeeEP {
virtual void zbAttributeRead(uint16_t cluster_id, const esp_zb_zcl_attribute_t *attribute) {};
virtual void zbReadBasicCluster(const esp_zb_zcl_attribute_t *attribute); //already implemented
virtual void zbIdentify(const esp_zb_zcl_set_attr_value_message_t *message);
virtual void zbWindowCoveringMovementCmd(const esp_zb_zcl_window_covering_movement_message_t *message) {};
virtual void zbReadTimeCluster(const esp_zb_zcl_attribute_t *attribute); //already implemented

virtual void zbIASZoneStatusChangeNotification(const esp_zb_zcl_ias_zone_status_change_notification_message_t *message) {};
virtual void zbIASZoneEnrollResponse(const esp_zb_zcl_ias_zone_enroll_response_message_t *message) {};

Expand Down
26 changes: 26 additions & 0 deletions libraries/Zigbee/src/ZigbeeHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static esp_err_t zb_configure_report_resp_handler(const esp_zb_zcl_cmd_config_re
static esp_err_t zb_cmd_ias_zone_status_change_handler(const esp_zb_zcl_ias_zone_status_change_notification_message_t *message);
static esp_err_t zb_cmd_ias_zone_enroll_response_handler(const esp_zb_zcl_ias_zone_enroll_response_message_t *message);
static esp_err_t zb_cmd_default_resp_handler(const esp_zb_zcl_cmd_default_resp_message_t *message);
static esp_err_t zb_window_covering_movement_resp_handler(const esp_zb_zcl_window_covering_movement_message_t *message);

// Zigbee action handlers
[[maybe_unused]]
Expand All @@ -28,6 +29,9 @@ static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id,
case ESP_ZB_CORE_IAS_ZONE_ENROLL_RESPONSE_VALUE_CB_ID:
ret = zb_cmd_ias_zone_enroll_response_handler((esp_zb_zcl_ias_zone_enroll_response_message_t *)message);
break;
case ESP_ZB_CORE_WINDOW_COVERING_MOVEMENT_CB_ID:
ret = zb_window_covering_movement_resp_handler((esp_zb_zcl_window_covering_movement_message_t *)message);
break;
case ESP_ZB_CORE_CMD_DEFAULT_RESP_CB_ID: ret = zb_cmd_default_resp_handler((esp_zb_zcl_cmd_default_resp_message_t *)message); break;
default: log_w("Receive unhandled Zigbee action(0x%x) callback", callback_id); break;
}
Expand Down Expand Up @@ -198,4 +202,26 @@ static esp_err_t zb_cmd_default_resp_handler(const esp_zb_zcl_cmd_default_resp_m
return ESP_OK;
}

static esp_err_t zb_window_covering_movement_resp_handler(const esp_zb_zcl_window_covering_movement_message_t *message) {
if (!message) {
log_e("Empty message");
}
if (message->info.status != ESP_ZB_ZCL_STATUS_SUCCESS) {
log_e("Received message: error status(%d)", message->info.status);
}

log_v(
"Received message: endpoint(%d), cluster(0x%x), command(0x%x), payload(%d)", message->info.dst_endpoint, message->info.cluster, message->command,
message->payload
);

// List through all Zigbee EPs and call the callback function, with the message
for (std::list<ZigbeeEP *>::iterator it = Zigbee.ep_objects.begin(); it != Zigbee.ep_objects.end(); ++it) {
if (message->info.dst_endpoint == (*it)->getEndpoint()) {
(*it)->zbWindowCoveringMovementCmd(message); //method zbWindowCoveringMovementCmd must be implemented in specific EP class
}
}
return ESP_OK;
}

#endif //SOC_IEEE802154_SUPPORTED && CONFIG_ZB_ENABLED
Loading

0 comments on commit db0bbad

Please # to comment.