Skip to content

Commit f1be773

Browse files
committed
add zigbee examples
1 parent e5a15af commit f1be773

File tree

13 files changed

+650
-0
lines changed

13 files changed

+650
-0
lines changed

.github/workflows/examples.yml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
- "examples/arduino-rmt-blink"
1919
- "examples/arduino-usb-keyboard"
2020
- "examples/arduino-wifiscan"
21+
- "examples/arduino-zigbee-light"
22+
- "examples/arduino-zigbee-switch"
2123
- "examples/espidf-arduino-h2zero-BLE_scan"
2224
- "examples/espidf-arduino-matter-light"
2325
- "examples/espidf-arduino-blink"
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Arduino-ESP32 Zigbee On/Off Light Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) on/off light.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Hardware Required
13+
14+
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with Zigbee_On_Off_switch example)
15+
* A USB cable for power supply and programming
16+
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device and upload the Zigbee_On_Off_Light example
17+
18+
### Configure the Project
19+
20+
Set the LED GPIO by changing the `LED_PIN` definition. By default, the LED_PIN is `RGB_BUILTIN`.
21+
By default, the `neoPixelWrite` function is used to control the LED. You can change it to digitalWrite to control a simple LED.
22+
23+
#### Using Arduino IDE
24+
25+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
26+
27+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
28+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
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+
32+
## Troubleshooting
33+
34+
If the End device flashed with this example is not connecting to the coordinator, erase the flash before flashing it to the board. It is recommended to do this if you did changes to the coordinator.
35+
You can do the following:
36+
37+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`
38+
* In the sketch uncomment function `esp_zb_nvram_erase_at_start(true);` located in `esp_zb_task` function.
39+
40+
By default, the coordinator network is open for 180s after rebooting or flashing new firmware. After that, the network is closed for adding new devices.
41+
You can change it by editing `esp_zb_bdb_open_network(180);` in `esp_zb_app_signal_handler` function.
42+
43+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
44+
45+
* **LED not blinking:** Check the wiring connection and the IO selection.
46+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
47+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
48+
49+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
50+
51+
## Contribute
52+
53+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
54+
55+
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!
56+
57+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
58+
59+
## Resources
60+
61+
* Official ESP32 Forum: [Link](https://esp32.com)
62+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
63+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
64+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
65+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; http://docs.platformio.org/page/projectconf.html
9+
10+
11+
[env:esp32-h2-devkitm-1]
12+
platform = https://github.com/pioarduino/platform-espressif32.git#develop
13+
framework = arduino
14+
board = esp32-h2-devkitm-1
15+
monitor_speed = 115200
16+
board_build.partitions = zigbee.csv
17+
board_build.filesystem = spiffs
18+
build_flags =
19+
-DZIGBEE_MODE_ED
20+
-DCORE_DEBUG_LEVEL=5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2024 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 simple Zigbee light bulb.
17+
*
18+
* The example demonstrates how to use Zigbee library to create a end device light bulb.
19+
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator.
20+
*
21+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
22+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
23+
*
24+
* Please check the README.md for instructions and more detailed description.
25+
*
26+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
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 "ZigbeeCore.h"
34+
#include "ep/ZigbeeLight.h"
35+
36+
#define LED_PIN RGB_BUILTIN
37+
#define BUTTON_PIN 9 // C6/H2 Boot button
38+
#define ZIGBEE_LIGHT_ENDPOINT 10 /* esp light bulb device endpoint, used to process light controlling commands */
39+
40+
class MyZigbeeLight : public ZigbeeLight {
41+
public:
42+
// Constructor that passes parameters to the base class constructor
43+
MyZigbeeLight(uint8_t endpoint) : ZigbeeLight(endpoint) {}
44+
45+
// Override the set_on_off function
46+
void setOnOff(bool value) override {
47+
rgbLedWrite(LED_PIN, 255 * value, 255 * value, 255 * value); // Toggle light
48+
}
49+
};
50+
51+
MyZigbeeLight zbLight = MyZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
52+
53+
/********************* Arduino functions **************************/
54+
void setup() {
55+
// Init RMT and leave light OFF
56+
rgbLedWrite(LED_PIN, 0, 0, 0);
57+
58+
// Init button for factory reset
59+
pinMode(BUTTON_PIN, INPUT);
60+
61+
//Optional: set Zigbee device name and model
62+
zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
63+
64+
//Add endpoint to Zigbee Core
65+
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
66+
Zigbee.addEndpoint(&zbLight);
67+
68+
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
69+
log_d("Calling Zigbee.begin()");
70+
Zigbee.begin();
71+
}
72+
73+
void loop() {
74+
// Cheking button for factory reset
75+
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
76+
// Key debounce handling
77+
delay(100);
78+
int startTime = millis();
79+
while (digitalRead(BUTTON_PIN) == LOW) {
80+
delay(50);
81+
if((millis() - startTime) > 3000) {
82+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
83+
Serial.printf("Reseting Zigbee to factory settings, reboot.\n");
84+
Zigbee.factoryReset();
85+
}
86+
}
87+
}
88+
delay(100);
89+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Arduino-ESP32 Zigbee On/Off Light Switch Example
2+
3+
This example shows how to configure Zigbee Coordinator and use it as a Home Automation (HA) on/off light switch.
4+
5+
# Supported Targets
6+
7+
Currently, this example supports the following targets.
8+
9+
| Supported Targets | ESP32-C6 | ESP32-H2 |
10+
| ----------------- | -------- | -------- |
11+
12+
## Hardware Required
13+
14+
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee end device (loaded with Zigbee_On_Off_Light example).
15+
* A USB cable for power supply and programming.
16+
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee coordinator and upload the Zigbee_On_Off_Switch example.
17+
18+
### Configure the Project
19+
20+
Set the Button Switch GPIO by changing the `GPIO_INPUT_IO_TOGGLE_SWITCH` definition. By default, it's the pin `9` (BOOT button on ESP32-C6 and ESP32-H2).
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 Coordinator Zigbee mode: `Tools -> Zigbee mode: Zigbee ZCZR (coordinator/router)`.
28+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`.
29+
* Select the COM port: `Tools -> Port: xxx where the `xxx` is the detected COM port.
30+
* Optional: Set debug level to info to see logs from Zigbee stack: `Tools -> Core Debug Level: Info`.
31+
32+
## Troubleshooting
33+
34+
If the End device flashed with the example `Zigbee_Light_Bulb` 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.
35+
You can do the following:
36+
37+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`.
38+
* In the `Zigbee_Light_Bulb` example sketch uncomment function `esp_zb_nvram_erase_at_start(true);` located in `esp_zb_task` function.
39+
40+
By default, the coordinator network is open for 180s after rebooting or flashing new firmware. After that, the network is closed for adding new devices.
41+
You can change it by editing `esp_zb_bdb_open_network(180);` in `esp_zb_app_signal_handler` function.
42+
43+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
44+
45+
* **LED not blinking:** Check the wiring connection and the IO selection.
46+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
47+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
48+
49+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
50+
51+
## Contribute
52+
53+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
54+
55+
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!
56+
57+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
58+
59+
## Resources
60+
61+
* Official ESP32 Forum: [Link](https://esp32.com)
62+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
63+
* ESP32-C6 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf)
64+
* ESP32-H2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf)
65+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

0 commit comments

Comments
 (0)