-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Zigbee): Support for sleepy device + Power Source and battery le…
…vel + Humidity sensor cluster (#10551) * feat(zigbee): Add power source and battery level * fix(zigbee): Make sure Indentify callback in not null * fix(zigbee): Remove leftovers and use workaround for mac capabilities * feat(zigbee) Add humidity optional cluster + fixes * feat(zigbee): Add common Zigbee.h header file * fix(zigbee): remove leftover code * ci(pre-commit): Apply automatic fixes * fix(zigbee): Fix codespell issues --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b170e87
commit d1df696
Showing
20 changed files
with
406 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Arduino-ESP32 Zigbee Temperature and Humidity Sensor Sleepy Device Example | ||
|
||
This example demonstrates how to use the Zigbee library to create an end device temperature/humidity sensor and use it as a Home Automation (HA) extended temperature sensor. | ||
|
||
# Supported Targets | ||
|
||
Currently, this example supports the following targets. | ||
|
||
| Supported Targets | ESP32-C6 | ESP32-H2 | | ||
| ----------------- | -------- | -------- | | ||
|
||
## Temperature Sensor Functions | ||
|
||
1. Initialize a Zigbee temperature and humidity sensor. | ||
2. Measure temperature and humidity values. | ||
3. Report the measured values to the Zigbee network. | ||
4. Put the device to sleep to save power. | ||
|
||
## Hardware Required | ||
|
||
* ESP32-H2 or ESP32-C6 development board | ||
* A USB cable for power supply and programming | ||
|
||
### Configure the Project | ||
|
||
In this example, to demonstrate the functionality the chip temperature is used and reported as temperature and humidity. | ||
Set the Button GPIO by changing the `BUTTON_PIN` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2). | ||
|
||
#### 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 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) |
125 changes: 125 additions & 0 deletions
125
libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/Zigbee_Temp_Hum_Sensor_Sleepy.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2024 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 temperature and humidity sensor Sleepy device. | ||
* | ||
* The example demonstrates how to use Zigbee library to create an end device temperature and humidity sensor. | ||
* The sensor is a Zigbee end device, which is reporting data to the Zigbee network. | ||
* | ||
* 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 Jan Procházka (https://github.com/P-R-O-C-H-Y/) | ||
*/ | ||
|
||
#ifndef ZIGBEE_MODE_ED | ||
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "Zigbee.h" | ||
|
||
#define BUTTON_PIN 9 //Boot button for C6/H2 | ||
#define TEMP_SENSOR_ENDPOINT_NUMBER 10 | ||
|
||
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ | ||
#define TIME_TO_SLEEP 55 /* Sleep for 55s will + 5s delay for establishing connection => data reported every 1 minute */ | ||
|
||
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); | ||
|
||
/************************ Temp sensor *****************************/ | ||
void meausureAndSleep() { | ||
// Measure temperature sensor value | ||
float temperature = temperatureRead(); | ||
|
||
// Use temparture value as humidity value to demonstrate both temperature and humidity | ||
float humidity = temperature; | ||
|
||
// Update temperature and humidity values in Temperature sensor EP | ||
zbTempSensor.setTemperature(temperature); | ||
zbTempSensor.setHumidity(humidity); | ||
|
||
// Report temperature and humidity values | ||
zbTempSensor.reportTemperature(); | ||
zbTempSensor.reportHumidity(); | ||
|
||
log_d("Temperature: %.2f°C, Humidity: %.2f%", temperature, humidity); | ||
|
||
// Put device to deep sleep | ||
esp_deep_sleep_start(); | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
// Init button switch | ||
pinMode(BUTTON_PIN, INPUT_PULLUP); | ||
|
||
// Configure the wake up source and set to wake up every 5 seconds | ||
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | ||
|
||
// Optional: set Zigbee device name and model | ||
zbTempSensor.setManufacturerAndModel("Espressif", "SleepyZigbeeTempSensorTest"); | ||
|
||
// Set minimum and maximum temperature measurement value (10-50°C is default range for chip temperature measurement) | ||
zbTempSensor.setMinMaxValue(10, 50); | ||
|
||
// Set tolerance for temperature measurement in °C (lowest possible value is 0.01°C) | ||
zbTempSensor.setTolerance(1); | ||
|
||
// Set power source to battery and set battery percentage to measured value (now 100% for demonstration) | ||
// The value can be also updated by calling zbTempSensor.setBatteryPercentage(percentage) anytime | ||
zbTempSensor.setPowerSource(ZB_POWER_SOURCE_BATTERY, 100); | ||
|
||
// Add humidity cluster to the temperature sensor device with min, max and tolerance values | ||
zbTempSensor.addHumiditySensor(0, 100, 1); | ||
|
||
// Add endpoint to Zigbee Core | ||
Zigbee.addEndpoint(&zbTempSensor); | ||
|
||
// Create a custom Zigbee configuration for End Device with keep alive 10s to avoid interference with reporting data | ||
esp_zb_cfg_t zigbeeConfig = ZIGBEE_DEFAULT_ED_CONFIG(); | ||
zigbeeConfig.nwk_cfg.zed_cfg.keep_alive = 10000; | ||
|
||
// When all EPs are registered, start Zigbee in End Device mode | ||
Zigbee.begin(&zigbeeConfig, false); | ||
|
||
// Wait for Zigbee to start | ||
while (!Zigbee.isStarted()) { | ||
delay(100); | ||
} | ||
|
||
// Delay 5s to allow establishing connection with coordinator, needed for sleepy devices | ||
delay(5000); | ||
} | ||
|
||
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 | ||
Zigbee.factoryReset(); | ||
} | ||
} | ||
} | ||
|
||
// Call the function to measure temperature and put the device to sleep | ||
meausureAndSleep(); | ||
} |
6 changes: 6 additions & 0 deletions
6
libraries/Zigbee/examples/Zigbee_Temp_Hum_Sensor_Sleepy/ci.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Zigbee library header file for includes of all Zigbee library headers. | ||
|
||
#pragma once | ||
|
||
// Core | ||
#include "ZigbeeCore.h" | ||
#include "ZigbeeEP.h" | ||
|
||
// Endpoints | ||
#include "ep/ZigbeeLight.h" | ||
#include "ep/ZigbeeSwitch.h" | ||
#include "ep/ZigbeeColorDimmableLight.h" | ||
#include "ep/ZigbeeColorDimmerSwitch.h" | ||
#include "ep/ZigbeeTempSensor.h" | ||
#include "ep/ZigbeeThermostat.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.