-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat(zigbee): Add Zigbee library #10265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
94cc558
3764810
7776aa5
8ce22e1
3078306
194693a
14b99db
54488a4
a83f695
06b0700
ec60ed4
ef1cc89
97920df
459e4df
fa716f0
db01086
4d18a8c
fa6267a
7693a16
7312de3
95ff61b
495a24f
b000bea
96c18c4
4edfb8c
6e220ec
4eb37aa
39633f1
59335bb
9a771b8
2fdeef7
9a86d6f
9a354ae
f321d43
27ecf23
04e0f75
2eedc2e
3198de3
6e56936
3961642
7d763df
44f5522
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Arduino-ESP32 Zigbee Color Dimmable Light Example | ||
|
||
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) color dimmable light. | ||
|
||
# Supported Targets | ||
|
||
Currently, this example supports the following targets. | ||
|
||
| Supported Targets | ESP32-C6 | ESP32-H2 | | ||
| ----------------- | -------- | -------- | | ||
|
||
## Hardware Required | ||
|
||
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with Zigbee_Color_Dimmer_Switch example) | ||
* A USB cable for power supply and programming | ||
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_Color_Dimmable_Light example | ||
|
||
### Configure the Project | ||
|
||
Set the LED GPIO by changing the `LED_PIN` definition. By default, the LED_PIN is `RGB_BUILTIN`. | ||
|
||
#### 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// 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 Color Dimmable light bulb. | ||
* | ||
* The example demonstrates how to use Zigbee library to create an end device with | ||
* color dimmable light end point. | ||
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. | ||
* | ||
* 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 end device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
#include "ep/ZigbeeColorDimmableLight.h" | ||
|
||
#define LED_PIN RGB_BUILTIN | ||
#define BUTTON_PIN 9 // C6/H2 Boot button | ||
#define ZIGBEE_LIGHT_ENDPOINT 10 | ||
|
||
ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_ENDPOINT); | ||
|
||
/********************* RGB LED functions **************************/ | ||
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) { | ||
float brightness = (float)level / 255; | ||
rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness); | ||
} | ||
|
||
// Create a task on identify call to handle the identify function | ||
void identify(uint16_t time) { | ||
static uint8_t blink = 1; | ||
log_d("Identify called for %d seconds", time); | ||
if (time == 0) { | ||
// If identify time is 0, stop blinking and restore light as it was used for identify | ||
zbColorLight.restoreLight(); | ||
return; | ||
} | ||
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink); | ||
blink = !blink; | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
// Init RMT and leave light OFF | ||
rgbLedWrite(LED_PIN, 0, 0, 0); | ||
|
||
// Init button for factory reset | ||
pinMode(BUTTON_PIN, INPUT); | ||
|
||
// Set callback function for light change | ||
zbColorLight.onLightChange(setRGBLight); | ||
|
||
// Optional: Set callback function for device identify | ||
zbColorLight.onIdentify(identify); | ||
|
||
// Optional: Set Zigbee device name and model | ||
zbColorLight.setManufacturerAndModel("Espressif", "ZBColorLightBulb"); | ||
|
||
// Add endpoint to Zigbee Core | ||
log_d("Adding ZigbeeLight endpoint to Zigbee Core"); | ||
Zigbee.addEndpoint(&zbColorLight); | ||
|
||
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE | ||
log_d("Calling Zigbee.begin()"); | ||
Zigbee.begin(); | ||
} | ||
|
||
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(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Arduino-ESP32 Zigbee Color Dimmer Switch Example | ||
|
||
This example shows how to configure Zigbee Coordinator and use it as a Home Automation (HA) color dimmer light switch. | ||
|
||
# Supported Targets | ||
|
||
Currently, this example supports the following targets. | ||
|
||
| Supported Targets | ESP32-C6 | ESP32-H2 | | ||
| ----------------- | -------- | -------- | | ||
|
||
## Hardware Required | ||
|
||
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee end device (loaded with Zigbee_Color_Dimmable_Light example). | ||
* A USB cable for power supply and programming. | ||
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee coordinator and upload the Zigbee_Color_Dimmable_Light example. | ||
|
||
### Configure the Project | ||
|
||
Set the Button Switch GPIO by changing the `GPIO_SWITCH` 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 Coordinator Zigbee mode: `Tools -> Zigbee mode: Zigbee ZCZR (coordinator/router)`. | ||
* 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 the example `Zigbee_Color_Dimmable_Light` 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,148 @@ | ||
// 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 color dimmer switch. | ||
* | ||
* The example demonstrates how to use Zigbee library to control a RGB light bulb. | ||
* The RGB light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator (Switch). | ||
* To turn on/off the light, push the button on the switch. | ||
* To change the color or level of the light, send serial commands to the switch. | ||
* | ||
* By setting the switch to allow multiple binding, so it can bind to multiple lights. | ||
* Also every 30 seconds, all bound lights are printed to the serial console. | ||
* | ||
* 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_ZCZR | ||
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
#include "ep/ZigbeeColorDimmerSwitch.h" | ||
|
||
/* Switch configuration */ | ||
#define SWITCH_PIN 9 // ESP32-C6/H2 Boot button | ||
#define SWITCH_ENDPOINT_NUMBER 5 | ||
|
||
/* Zigbee switch */ | ||
ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBER); | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
|
||
Serial.begin(115200); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!Serial) { | ||
delay(10); | ||
} | ||
|
||
//Init button switch | ||
pinMode(SWITCH_PIN, INPUT); | ||
|
||
//Optional: set Zigbee device name and model | ||
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch"); | ||
|
||
//Optional to allow multiple light to bind to the switch | ||
zbSwitch.allowMultipleBinding(true); | ||
|
||
//Add endpoint to Zigbee Core | ||
Zigbee.addEndpoint(&zbSwitch); | ||
|
||
//Open network for 180 seconds after boot | ||
Zigbee.setRebootOpenNetwork(180); | ||
|
||
//When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode | ||
Zigbee.begin(ZIGBEE_COORDINATOR); | ||
|
||
Serial.println("Waiting for Light to bound to the switch"); | ||
//Wait for switch to bound to a light: | ||
while (!zbSwitch.isBound()) { | ||
Serial.printf("."); | ||
delay(500); | ||
} | ||
Serial.println(); | ||
} | ||
|
||
void loop() { | ||
// Handle button switch in loop() | ||
if (digitalRead(SWITCH_PIN) == LOW) { // Push button pressed | ||
// Key debounce handling | ||
while (digitalRead(SWITCH_PIN) == LOW) { | ||
delay(50); | ||
} | ||
// Toggle light | ||
zbSwitch.lightToggle(); | ||
} | ||
// Handle serial input to control color and level of the light | ||
if (Serial.available()) { | ||
String command = Serial.readString(); | ||
|
||
if (command == "on") { | ||
zbSwitch.lightOn(); | ||
} else if (command == "off") { | ||
zbSwitch.lightOff(); | ||
} else if (command == "toggle") { | ||
zbSwitch.lightToggle(); | ||
} else if (command == "red") { | ||
zbSwitch.setLightColor(255, 0, 0); | ||
} else if (command == "green") { | ||
zbSwitch.setLightColor(0, 255, 0); | ||
} else if (command == "blue") { | ||
zbSwitch.setLightColor(0, 0, 255); | ||
} else if (command == "white") { | ||
zbSwitch.setLightColor(255, 255, 255); | ||
} else if (command == "color") { | ||
//wait for color value | ||
Serial.println("Enter red value (0-255):"); | ||
while (!Serial.available()) { | ||
delay(100); | ||
} | ||
int red = Serial.parseInt(); | ||
Serial.println("Enter green value (0-255):"); | ||
while (!Serial.available()) { | ||
delay(100); | ||
} | ||
int green = Serial.parseInt(); | ||
Serial.println("Enter blue value (0-255):"); | ||
while (!Serial.available()) { | ||
delay(100); | ||
} | ||
int blue = Serial.parseInt(); | ||
zbSwitch.setLightColor(red, green, blue); | ||
} else if (command == "level") { | ||
//wait for level value | ||
Serial.println("Enter level value (0-255):"); | ||
while (!Serial.available()) { | ||
delay(100); | ||
} | ||
int level = Serial.parseInt(); | ||
zbSwitch.setLightLevel(level); | ||
} else { | ||
Serial.println("Unknown command"); | ||
} | ||
} | ||
|
||
// print the bound devices (lights) every 30 seconds | ||
static uint32_t last_print = 0; | ||
if (millis() - last_print > 30000) { | ||
last_print = millis(); | ||
zbSwitch.printBoundDevices(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 simple Zigbee light bulb. | ||
* | ||
* The example demonstrates how to use Zigbee library to create a end device light bulb. | ||
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator. | ||
* | ||
* 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 end device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
#include "ep/ZigbeeLight.h" | ||
|
||
#define LED_PIN RGB_BUILTIN | ||
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button | ||
#define ZIGBEE_LIGHT_ENDPOINT 10 | ||
|
||
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT); | ||
|
||
/********************* RGB LED functions **************************/ | ||
void setLED(bool value) { | ||
digitalWrite(LED_PIN, value); | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
// Init LED and turn it OFF (if LED_PIN == RGB_BUILTIN, the rgbLedWrite() will be used under the hood) | ||
pinMode(LED_PIN, OUTPUT); | ||
digitalWrite(LED_PIN, LOW); | ||
|
||
// Init button for factory reset | ||
pinMode(BUTTON_PIN, INPUT); | ||
|
||
//Optional: set Zigbee device name and model | ||
zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb"); | ||
|
||
// Set callback function for light change | ||
zbLight.onLightChange(setLED); | ||
|
||
//Add endpoint to Zigbee Core | ||
log_d("Adding ZigbeeLight endpoint to Zigbee Core"); | ||
Zigbee.addEndpoint(&zbLight); | ||
|
||
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE | ||
log_d("Calling Zigbee.begin()"); | ||
Zigbee.begin(); | ||
} | ||
|
||
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(); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
delay(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// 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 simple Zigbee light switch. | ||
* | ||
* The example demonstrates how to use Zigbee library to control a light bulb. | ||
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator (Switch). | ||
* Button switch and Zigbee runs in separate tasks. | ||
* | ||
* 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_ZCZR | ||
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
#include "ep/ZigbeeSwitch.h" | ||
|
||
#define SWITCH_ENDPOINT_NUMBER 5 | ||
|
||
/* Switch configuration */ | ||
#define GPIO_INPUT_IO_TOGGLE_SWITCH 9 | ||
#define PAIR_SIZE(TYPE_STR_PAIR) (sizeof(TYPE_STR_PAIR) / sizeof(TYPE_STR_PAIR[0])) | ||
|
||
typedef enum { | ||
SWITCH_ON_CONTROL, | ||
SWITCH_OFF_CONTROL, | ||
SWITCH_ONOFF_TOGGLE_CONTROL, | ||
SWITCH_LEVEL_UP_CONTROL, | ||
SWITCH_LEVEL_DOWN_CONTROL, | ||
SWITCH_LEVEL_CYCLE_CONTROL, | ||
SWITCH_COLOR_CONTROL, | ||
} SwitchFunction; | ||
|
||
typedef struct { | ||
uint8_t pin; | ||
SwitchFunction func; | ||
} SwitchData; | ||
|
||
typedef enum { | ||
SWITCH_IDLE, | ||
SWITCH_PRESS_ARMED, | ||
SWITCH_PRESS_DETECTED, | ||
SWITCH_PRESSED, | ||
SWITCH_RELEASE_DETECTED, | ||
} SwitchState; | ||
|
||
static SwitchData buttonFunctionPair[] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}}; | ||
|
||
ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER); | ||
|
||
/********************* Zigbee functions **************************/ | ||
static void onZbButton(SwitchData *button_func_pair) { | ||
if (button_func_pair->func == SWITCH_ONOFF_TOGGLE_CONTROL) { | ||
// Send toggle command to the light | ||
zbSwitch.lightToggle(); | ||
} | ||
} | ||
|
||
/********************* GPIO functions **************************/ | ||
static QueueHandle_t gpio_evt_queue = NULL; | ||
|
||
static void IRAM_ATTR onGpioInterrupt(void *arg) { | ||
xQueueSendFromISR(gpio_evt_queue, (SwitchData *)arg, NULL); | ||
} | ||
|
||
static void enableGpioInterrupt(bool enabled) { | ||
for (int i = 0; i < PAIR_SIZE(buttonFunctionPair); ++i) { | ||
if (enabled) { | ||
enableInterrupt((buttonFunctionPair[i]).pin); | ||
} else { | ||
disableInterrupt((buttonFunctionPair[i]).pin); | ||
} | ||
} | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
|
||
Serial.begin(115200); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!Serial) { | ||
delay(10); | ||
} | ||
|
||
//Optional: set Zigbee device name and model | ||
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch"); | ||
|
||
//Optional to allow multiple light to bind to the switch | ||
zbSwitch.allowMultipleBinding(true); | ||
|
||
//Add endpoint to Zigbee Core | ||
log_d("Adding ZigbeeSwitch endpoint to Zigbee Core"); | ||
Zigbee.addEndpoint(&zbSwitch); | ||
|
||
//Open network for 180 seconds after boot | ||
Zigbee.setRebootOpenNetwork(180); | ||
|
||
// Init button switch | ||
for (int i = 0; i < PAIR_SIZE(buttonFunctionPair); i++) { | ||
pinMode(buttonFunctionPair[i].pin, INPUT_PULLUP); | ||
/* create a queue to handle gpio event from isr */ | ||
gpio_evt_queue = xQueueCreate(10, sizeof(SwitchData)); | ||
if (gpio_evt_queue == 0) { | ||
log_e("Queue was not created and must not be used"); | ||
while (1); | ||
} | ||
attachInterruptArg(buttonFunctionPair[i].pin, onGpioInterrupt, (void *)(buttonFunctionPair + i), FALLING); | ||
} | ||
|
||
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode | ||
log_d("Calling Zigbee.begin()"); | ||
Zigbee.begin(ZIGBEE_COORDINATOR); | ||
|
||
Serial.println("Waiting for Light to bound to the switch"); | ||
//Wait for switch to bound to a light: | ||
while (!zbSwitch.isBound()) { | ||
Serial.printf("."); | ||
delay(500); | ||
} | ||
|
||
// Optional: read manufacturer and model name from the bound light | ||
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices(); | ||
//List all bound lights | ||
for (const auto &device : boundLights) { | ||
Serial.printf("Device on endpoint %d, short address: 0x%x\n", device->endpoint, device->short_addr); | ||
Serial.printf( | ||
"IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", device->ieee_addr[0], device->ieee_addr[1], device->ieee_addr[2], device->ieee_addr[3], | ||
device->ieee_addr[4], device->ieee_addr[5], device->ieee_addr[6], device->ieee_addr[7] | ||
); | ||
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr)); | ||
Serial.printf("Light model: %s", zbSwitch.readModel(device->endpoint, device->short_addr)); | ||
} | ||
|
||
Serial.println(); | ||
} | ||
|
||
void loop() { | ||
// Handle button switch in loop() | ||
uint8_t pin = 0; | ||
SwitchData buttonSwitch; | ||
static SwitchState buttonState = SWITCH_IDLE; | ||
bool eventFlag = false; | ||
|
||
/* check if there is any queue received, if yes read out the buttonSwitch */ | ||
if (xQueueReceive(gpio_evt_queue, &buttonSwitch, portMAX_DELAY)) { | ||
pin = buttonSwitch.pin; | ||
enableGpioInterrupt(false); | ||
eventFlag = true; | ||
} | ||
while (eventFlag) { | ||
bool value = digitalRead(pin); | ||
switch (buttonState) { | ||
case SWITCH_IDLE: buttonState = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_IDLE; break; | ||
case SWITCH_PRESS_DETECTED: buttonState = (value == LOW) ? SWITCH_PRESS_DETECTED : SWITCH_RELEASE_DETECTED; break; | ||
case SWITCH_RELEASE_DETECTED: | ||
buttonState = SWITCH_IDLE; | ||
/* callback to button_handler */ | ||
(*onZbButton)(&buttonSwitch); | ||
break; | ||
default: break; | ||
} | ||
if (buttonState == SWITCH_IDLE) { | ||
enableGpioInterrupt(true); | ||
eventFlag = false; | ||
break; | ||
} | ||
vTaskDelay(10 / portTICK_PERIOD_MS); | ||
} | ||
|
||
// print the bound lights every 10 seconds | ||
static uint32_t lastPrint = 0; | ||
if (millis() - lastPrint > 10000) { | ||
lastPrint = millis(); | ||
zbSwitch.printBoundDevices(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Arduino-ESP32 Zigbee Networks Scan Example | ||
|
||
This example shows how to scan Zigbee Networks. | ||
|
||
# Supported Targets | ||
|
||
Currently, this example supports the following targets. | ||
|
||
| Supported Targets | ESP32-C6 | ESP32-H2 | | ||
| ----------------- | -------- | -------- | | ||
|
||
## Example Output | ||
|
||
Setup done | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
Loop running... | ||
|
||
Scan done | ||
2 networks found: | ||
Nr | PAN ID | CH | Permit Joining | Router Capacity | End Device Capacity | Extended PAN ID | ||
1 | 0xe6f0 | 14 | Yes | Yes | Yes | f0:f5:bd:ff:fe:02:3f:24 | ||
2 | 0xa9bb | 24 | No | Yes | Yes | 60:55:f9:00:00:f7:52:d0 | ||
|
||
## Hardware Required | ||
|
||
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with `Zigbee_Thermostat` example) | ||
* A USB cable for power supply and programming | ||
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device (loaded with `Zigbee_Temperature_Sensor` example) | ||
|
||
### Configure the Project | ||
|
||
In this example, the internal temperature sensor task is reading the chip temperature. | ||
Set the Button Switch GPIO by changing the `GPIO_INPUT_IO_TOGGLE_SWITCH` definition. By default, it's the `GPIO_NUM_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 | ||
|
||
***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,113 @@ | ||
// 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 Network Scanning. | ||
* | ||
* The example demonstrates how to use ESP Zigbee stack to scan for Zigbee networks. | ||
* | ||
* Any Zigbee mode can be selected in Tools->Zigbee mode | ||
* with proper Zigbee partition scheme 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/) | ||
*/ | ||
|
||
#if !defined(ZIGBEE_MODE_ED) && !defined(ZIGBEE_MODE_ZCZR) | ||
#error "Zigbee device mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
|
||
#ifdef ZIGBEE_MODE_ZCZR | ||
zigbee_role_t role = ZIGBEE_ROUTER; // or can be ZIGBEE_COORDINATOR, but it wont scan itself | ||
#else | ||
zigbee_role_t role = ZIGBEE_END_DEVICE; | ||
#endif | ||
|
||
void printScannedNetworks(uint16_t networksFound) { | ||
if (networksFound == 0) { | ||
Serial.println("No networks found"); | ||
} else { | ||
zigbee_scan_result_t *scan_result = Zigbee.getScanResult(); | ||
Serial.println("\nScan done"); | ||
Serial.print(networksFound); | ||
Serial.println(" networks found:"); | ||
Serial.println("Nr | PAN ID | CH | Permit Joining | Router Capacity | End Device Capacity | Extended PAN ID"); | ||
for (int i = 0; i < networksFound; ++i) { | ||
// Print all available info for each network found | ||
Serial.printf("%2d", i + 1); | ||
Serial.print(" | "); | ||
Serial.printf("0x%04hx", scan_result[i].short_pan_id); | ||
Serial.print(" | "); | ||
Serial.printf("%2d", scan_result[i].logic_channel); | ||
Serial.print(" | "); | ||
Serial.printf("%-14.14s", scan_result[i].permit_joining ? "Yes" : "No"); | ||
Serial.print(" | "); | ||
Serial.printf("%-15.15s", scan_result[i].router_capacity ? "Yes" : "No"); | ||
Serial.print(" | "); | ||
Serial.printf("%-19.19s", scan_result[i].end_device_capacity ? "Yes" : "No"); | ||
Serial.print(" | "); | ||
Serial.printf( | ||
"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", scan_result[i].extended_pan_id[7], scan_result[i].extended_pan_id[6], scan_result[i].extended_pan_id[5], | ||
scan_result[i].extended_pan_id[4], scan_result[i].extended_pan_id[3], scan_result[i].extended_pan_id[2], scan_result[i].extended_pan_id[1], | ||
scan_result[i].extended_pan_id[0] | ||
); | ||
Serial.println(); | ||
delay(10); | ||
} | ||
Serial.println(""); | ||
// Delete the scan result to free memory for code below. | ||
Zigbee.scanDelete(); | ||
} | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!Serial) { | ||
delay(10); | ||
} | ||
|
||
// Initialize Zigbee stack without any EPs just for scanning | ||
Zigbee.begin(role); | ||
|
||
// Waint until Zigbee stack is ready | ||
while (!Zigbee.isStarted()) { | ||
delay(100); | ||
} | ||
|
||
Serial.println("Setup done"); | ||
// Start Zigbee Network Scan with default parameters (all channels, scan time 5) | ||
Zigbee.scanNetworks(); | ||
} | ||
|
||
void loop() { | ||
// check Zigbee Network Scan process | ||
int16_t ZigbeeScanStatus = Zigbee.scanComplete(); | ||
if (ZigbeeScanStatus < 0) { // it is busy scanning or got an error | ||
if (ZigbeeScanStatus == ZB_SCAN_FAILED) { | ||
Serial.println("WiFi Scan has failed. Starting again."); | ||
Zigbee.scanNetworks(); | ||
} | ||
// other option is status ZB_SCAN_RUNNING - just wait. | ||
} else { // Found Zero or more Wireless Networks | ||
printScannedNetworks(ZigbeeScanStatus); | ||
Zigbee.scanNetworks(); // start over... | ||
} | ||
|
||
// Loop can do something else... | ||
delay(500); | ||
Serial.println("Loop running..."); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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 sensor. | ||
* | ||
* The example demonstrates how to use Zigbee library to create a end device temperature sensor. | ||
* The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator. | ||
* | ||
* 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 "ZigbeeCore.h" | ||
#include "ep/ZigbeeTempSensor.h" | ||
|
||
#define BUTTON_PIN 9 //Boot button for C6/H2 | ||
#define TEMP_SENSOR_ENDPOINT_NUMBER 10 | ||
|
||
ZigbeeTempSensor zbTempSensor = ZigbeeTempSensor(TEMP_SENSOR_ENDPOINT_NUMBER); | ||
|
||
/************************ Temp sensor *****************************/ | ||
static void temp_sensor_value_update(void *arg) { | ||
for (;;) { | ||
// Read temperature sensor value | ||
float tsens_value = temperatureRead(); | ||
log_v("Temperature sensor value: %.2f°C", tsens_value); | ||
// Update temperature value in Temperature sensor EP | ||
zbTempSensor.setTemperature(tsens_value); | ||
delay(1000); | ||
} | ||
} | ||
|
||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
|
||
Serial.begin(115200); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!Serial) { | ||
delay(10); | ||
} | ||
|
||
// Init button switch | ||
pinMode(BUTTON_PIN, INPUT); | ||
|
||
// Optional: set Zigbee device name and model | ||
zbTempSensor.setManufacturerAndModel("Espressif", "ZigbeeTempSensor"); | ||
|
||
// 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); | ||
|
||
// Add endpoint to Zigbee Core | ||
Zigbee.addEndpoint(&zbTempSensor); | ||
|
||
// When all EPs are registered, start Zigbee in End Device mode | ||
Zigbee.begin(); | ||
|
||
// Start Temperature sensor reading task | ||
xTaskCreate(temp_sensor_value_update, "temp_sensor_update", 2048, NULL, 10, NULL); | ||
|
||
// Set reporting interval for temperature measurement in seconds, must be called after Zigbee.begin() | ||
// min_interval and max_interval in seconds, delta (temp change in °C) | ||
// if min = 1 and max = 0, reporting is sent only when temperature changes by delta | ||
// if min = 0 and max = 10, reporting is sent every 10 seconds or temperature changes by delta | ||
// if min = 0, max = 10 and delta = 0, reporting is sent every 10 seconds regardless of temperature change | ||
zbTempSensor.setReporting(1, 0, 1); | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
zbTempSensor.reportTemperature(); | ||
} | ||
delay(100); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee,ZigbeeMode=ed" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// 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 simple Zigbee thermostat. | ||
* | ||
* The example demonstrates how to use Zigbee library to get data from temperature | ||
* sensor end device and act as an thermostat. | ||
* The temperature sensor is a Zigbee end device, which is controlled by a Zigbee coordinator (thermostat). | ||
* | ||
* 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_ZCZR | ||
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode" | ||
#endif | ||
|
||
#include "ZigbeeCore.h" | ||
#include "ep/ZigbeeThermostat.h" | ||
|
||
#define BUTTON_PIN 9 // Boot button for C6/H2 | ||
#define THERMOSTAT_ENDPOINT_NUMBER 5 | ||
|
||
ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER); | ||
|
||
// Save temperature sensor data | ||
float sensor_temp; | ||
float sensor_max_temp; | ||
float sensor_min_temp; | ||
float sensor_tolerance; | ||
|
||
/****************** Temperature sensor handling *******************/ | ||
void recieveSensorTemp(float temperature) { | ||
Serial.printf("Temperature sensor value: %.2f°C\n", temperature); | ||
sensor_temp = temperature; | ||
} | ||
|
||
void recieveSensorConfig(float min_temp, float max_temp, float tolerance) { | ||
Serial.printf("Temperature sensor settings: min %.2f°C, max %.2f°C, tolerance %.2f°C\n", min_temp, max_temp, tolerance); | ||
sensor_min_temp = min_temp; | ||
sensor_max_temp = max_temp; | ||
sensor_tolerance = tolerance; | ||
} | ||
/********************* Arduino functions **************************/ | ||
void setup() { | ||
Serial.begin(115200); | ||
P-R-O-C-H-Y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (!Serial) { | ||
delay(10); | ||
} | ||
|
||
// Init button switch | ||
pinMode(BUTTON_PIN, INPUT); | ||
|
||
// Set callback functions for temperature and configuration receive | ||
zbThermostat.onTempRecieve(recieveSensorTemp); | ||
zbThermostat.onConfigRecieve(recieveSensorConfig); | ||
|
||
//Optional: set Zigbee device name and model | ||
zbThermostat.setManufacturerAndModel("Espressif", "ZigbeeThermostat"); | ||
|
||
//Add endpoint to Zigbee Core | ||
Zigbee.addEndpoint(&zbThermostat); | ||
|
||
//Open network for 180 seconds after boot | ||
Zigbee.setRebootOpenNetwork(180); | ||
|
||
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode | ||
Zigbee.begin(ZIGBEE_COORDINATOR); | ||
|
||
Serial.println("Waiting for Temperature sensor to bound to the switch"); | ||
|
||
//Wait for switch to bound to a light: | ||
while (!zbThermostat.isBound()) { | ||
Serial.printf("."); | ||
delay(500); | ||
} | ||
|
||
// Get temperature sensor configuration | ||
zbThermostat.getSensorSettings(); | ||
Serial.println(); | ||
} | ||
|
||
void loop() { | ||
// Handle button switch in loop() | ||
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed | ||
|
||
// Key debounce handling | ||
while (digitalRead(BUTTON_PIN) == LOW) { | ||
delay(50); | ||
} | ||
|
||
// Set reporting interval for temperature sensor | ||
zbThermostat.setTemperatureReporting(0, 10, 2); | ||
} | ||
|
||
// Print temperature sensor data each 10 seconds | ||
static uint32_t last_print = 0; | ||
if (millis() - last_print > 10000) { | ||
last_print = millis(); | ||
int temp_percent = (int)((sensor_temp - sensor_min_temp) / (sensor_max_temp - sensor_min_temp) * 100); | ||
Serial.printf("Loop temperature info: %.2f°C (%d %%)\n", sensor_temp, temp_percent); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"fqbn": { | ||
"esp32c6": [ | ||
"espressif:esp32:esp32c6:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
], | ||
"esp32h2": [ | ||
"espressif:esp32:esp32h2:PartitionScheme=zigbee_zczr,ZigbeeMode=zczr" | ||
] | ||
}, | ||
"targets": { | ||
"esp32": false, | ||
"esp32c3": false, | ||
"esp32s2": false, | ||
"esp32s3": false | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
####################################### | ||
# Syntax Coloring Map For Zigbee | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
# Base Classes | ||
ZigbeeCore KEYWORD1 | ||
Zigbee KEYWORD1 | ||
ZigbeeEP KEYWORD1 | ||
|
||
# Endpoint Classes | ||
ZigbeeLight KEYWORD1 | ||
ZigbeeSwitch KEYWORD1 | ||
ZigbeeColorDimmableLight KEYWORD1 | ||
ZigbeeColorDimmerSwitch KEYWORD1 | ||
ZigbeeTempSensor KEYWORD1 | ||
ZigbeeThermostat KEYWORD1 | ||
|
||
# Other | ||
zigbee_role_t KEYWORD1 | ||
zbstring_t KEYWORD1 | ||
zb_device_params_t KEYWORD1 | ||
zigbee_scan_result_t KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
# ZigbeeCore | ||
isStarted KEYWORD2 | ||
addEndpoint KEYWORD2 | ||
setRadioConfig KEYWORD2 | ||
setHostConfig KEYWORD2 | ||
getRadioConfig KEYWORD2 | ||
getHostConfig KEYWORD2 | ||
setPrimaryChannelMask KEYWORD2 | ||
setRebootOpenNetwork KEYWORD2 | ||
scanNetworks KEYWORD2 | ||
scanComplete KEYWORD2 | ||
getScanResult KEYWORD2 | ||
scanDelete KEYWORD2 | ||
factoryReset KEYWORD2 | ||
|
||
# Common ZigbeeEP | ||
setVersion KEYWORD2 | ||
setManufacturerAndModel KEYWORD2 | ||
is_bound KEYWORD2 | ||
printBoundDevices KEYWORD2 | ||
allowMultipleBinding KEYWORD2 | ||
|
||
# ZigbeeLight + ZigbeeColorDimmableLight | ||
setOnOff KEYWORD2 | ||
sceneControl KEYWORD2 | ||
setOnOffTime KEYWORD2 | ||
setOffWaitTime KEYWORD2 | ||
setLevel KEYWORD2 | ||
setColor KEYWORD2 | ||
setColorSaturation KEYWORD2 | ||
setColorHue KEYWORD2 | ||
|
||
# ZigbeeSwitch + ZigbeeColorDimmerSwitch | ||
lightToggle KEYWORD2 | ||
lightOn KEYWORD2 | ||
lightOff KEYWORD2 | ||
lightOffWithEffect KEYWORD2 | ||
lightOnWithTimedOff KEYWORD2 | ||
lightOnWithSceneRecall KEYWORD2 | ||
setLightLevel KEYWORD2 | ||
setLightColor KEYWORD2 | ||
setLightColorSaturation KEYWORD2 | ||
setLightColorHue KEYWORD2 | ||
|
||
# ZigbeeTempSensor | ||
setTemperature KEYWORD2 | ||
setMinMaxValue KEYWORD2 | ||
setTolerance KEYWORD2 | ||
setReporting KEYWORD2 | ||
reportTemperature KEYWORD2 | ||
|
||
# ZigbeeThermostat | ||
temperatureRead KEYWORD2 | ||
temperatureMin KEYWORD2 | ||
temperatureMax KEYWORD2 | ||
temperatureTolerance KEYWORD2 | ||
getTemperature KEYWORD2 | ||
setTemperatureReporting KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|
||
ZIGBEE_COORDINATOR LITERAL1 | ||
ZIGBEE_ROUTER LITERAL1 | ||
ZIGBEE_END_DEVICE LITERAL1 | ||
ZIGBEE_DEFAULT_ED_CONFIG LITERAL1 | ||
ZIGBEE_DEFAULT_ROUTER_CONFIG LITERAL1 | ||
ZIGBEE_DEFAULT_COORDINATOR_CONFIG LITERAL1 | ||
ZIGBEE_DEFAULT_RADIO_CONFIG LITERAL1 | ||
ZIGBEE_DEFAULT_HOST_CONFIG LITERAL1 | ||
ZB_ARRAY_LENTH LITERAL1 | ||
XYZ_TO_RGB LITERAL1 | ||
RGB_TO_XYZ LITERAL1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=Zigbee | ||
version=3.0.5 | ||
author=P-R-O-C-H-Y | ||
maintainer=Jan Procházka <jan.prochazka@espressif.com> | ||
sentence=Enables zigbee connection with the ESP32 | ||
paragraph=With this library you can create zigbee end devices, routers, coordinators and connect them to the zigbee network. | ||
category=Communication | ||
url= | ||
architectures=esp32 |
Uh oh!
There was an error while loading. Please reload this page.