Skip to content

Commit 94cc558

Browse files
committed
Initial commit - light bulb + switch working
1 parent 50ef6f4 commit 94cc558

32 files changed

+1243
-4
lines changed

libraries/ESP32/examples/Zigbee/Zigbee_Light_Bulb/Zigbee_Light_Bulb.ino

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
/* Default End Device config */
3939
#define ESP_ZB_ZED_CONFIG() \
4040
{ \
41-
.esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, .install_code_policy = INSTALLCODE_POLICY_ENABLE, \
41+
.esp_zb_role = ESP_ZB_DEVICE_TYPE_ED, \
42+
.install_code_policy = INSTALLCODE_POLICY_ENABLE, \
4243
.nwk_cfg = { \
4344
.zed_cfg = \
4445
{ \
@@ -111,8 +112,12 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
111112
static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, const void *message) {
112113
esp_err_t ret = ESP_OK;
113114
switch (callback_id) {
114-
case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID: ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message); break;
115-
default: log_w("Receive Zigbee action(0x%x) callback", callback_id); break;
115+
case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID:
116+
ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message);
117+
break;
118+
default:
119+
log_w("Receive Zigbee action(0x%x) callback", callback_id);
120+
break;
116121
}
117122
return ret;
118123
}
@@ -127,7 +132,7 @@ static void esp_zb_task(void *pvParameters) {
127132
esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
128133

129134
//Erase NVRAM before creating connection to new Coordinator
130-
esp_zb_nvram_erase_at_start(true); //Comment out this line to erase NVRAM data if you are conneting to new Coordinator
135+
esp_zb_nvram_erase_at_start(true); //Comment out this line to erase NVRAM data if you are conneting to new Coordinator
131136

132137
ESP_ERROR_CHECK(esp_zb_start(false));
133138
esp_zb_main_loop_iteration();

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32

Whitespace-only changes.

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32c3

Whitespace-only changes.

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32c6

Whitespace-only changes.

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32h2

Whitespace-only changes.

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32s2

Whitespace-only changes.

libraries/Zigbee/examples/Zigbee_Light_Bulb/.skip.esp32s3

Whitespace-only changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Arduino-ESP32 Zigbee Light Bulb Example
2+
3+
This example shows how to configure the Zigbee end device and use it as a Home Automation (HA) on/off light bulb.
4+
5+
**This example is based on ESP-Zigbee-SDK example esp_zigbee_HA_sample/HA_on_off_light.**
6+
7+
# Supported Targets
8+
9+
Currently, this example supports the following targets.
10+
11+
| Supported Targets | ESP32-C6 | ESP32-H2 |
12+
| ----------------- | -------- | -------- |
13+
14+
## Hardware Required
15+
16+
* One development board (ESP32-H2 or ESP32-C6) acting as Zigbee coordinator (loaded with Zigbee_Light_switch example)
17+
* A USB cable for power supply and programming
18+
* Choose another board (ESP32-H2 or ESP32-C6) as Zigbee end device (loaded with Zigbee_Light_bulb example)
19+
20+
### Configure the Project
21+
22+
Set the LED GPIO by changing the `LED_PIN` definition. By default, the LED_PIN is `RGB_BUILTIN`.
23+
By default, the `neoPixelWrite` function is used to control the LED. You can change it to digitalWrite to control a simple LED.
24+
25+
#### Using Arduino IDE
26+
27+
To get more information about the Espressif boards see [Espressif Development Kits](https://www.espressif.com/en/products/devkits).
28+
29+
* Before Compile/Verify, select the correct board: `Tools -> Board`.
30+
* Select the End device Zigbee mode: `Tools -> Zigbee mode: Zigbee ED (end device)`
31+
* Select Partition Scheme for Zigbee: `Tools -> Partition Scheme: Zigbee 4MB with spiffs`
32+
* Select the COM port: `Tools -> Port: xxx` where the `xxx` is the detected COM port.
33+
34+
## Troubleshooting
35+
36+
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.
37+
You can do the following:
38+
39+
* In the Arduino IDE go to the Tools menu and set `Erase All Flash Before Sketch Upload` to `Enabled`
40+
* In the sketch uncomment function `esp_zb_nvram_erase_at_start(true);` located in `esp_zb_task` function.
41+
42+
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.
43+
You can change it by editing `esp_zb_bdb_open_network(180);` in `esp_zb_app_signal_handler` function.
44+
45+
***Important: Make sure you are using a good quality USB cable and that you have a reliable power source***
46+
47+
* **LED not blinking:** Check the wiring connection and the IO selection.
48+
* **Programming Fail:** If the programming/flash procedure fails, try reducing the serial connection speed.
49+
* **COM port not detected:** Check the USB cable and the USB to Serial driver installation.
50+
51+
If the error persists, you can ask for help at the official [ESP32 forum](https://esp32.com) or see [Contribute](#contribute).
52+
53+
## Contribute
54+
55+
To know how to contribute to this project, see [How to contribute.](https://github.com/espressif/arduino-esp32/blob/master/CONTRIBUTING.rst)
56+
57+
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!
58+
59+
Before creating a new issue, be sure to try Troubleshooting and check if the same issue was already created by someone else.
60+
61+
## Resources
62+
63+
The ESP Zigbee SDK provides more examples:
64+
* ESP Zigbee SDK Docs: [Link](https://docs.espressif.com/projects/esp-zigbee-sdk)
65+
* ESP Zigbee SDK Repo: [Link](https://github.com/espressif/esp-zigbee-sdk)
66+
67+
* Official ESP32 Forum: [Link](https://esp32.com)
68+
* Arduino-ESP32 Official Repository: [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
69+
* ESP32 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf)
70+
* ESP32-S2 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s2_datasheet_en.pdf)
71+
* ESP32-C3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf)
72+
* ESP32-S3 Datasheet: [Link to datasheet](https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf)
73+
* Official ESP-IDF documentation: [ESP-IDF](https://idf.espressif.com)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2023 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 ESP Zigbee stack 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+
27+
#ifndef ZIGBEE_MODE_ED
28+
#error "Zigbee end device mode is not selected in Tools->Zigbee mode"
29+
#endif
30+
31+
#include "Zigbee_core.h"
32+
#include "ep/ep_on_off_light.h"
33+
34+
#include "esp_zigbee_core.h"
35+
#include "freertos/FreeRTOS.h"
36+
#include "freertos/task.h"
37+
#include "ha/esp_zigbee_ha_standard.h"
38+
39+
#define LED_PIN RGB_BUILTIN
40+
#define ZIGBEE_LIGHT_ENDPOINT 10 /* esp light bulb device endpoint, used to process light controlling commands */
41+
42+
/* Handle the light attribute */
43+
// User callback for Zigbee actions to handle turning on/off the light called by Zigbee stack in zb_attribute_handler()
44+
45+
static void light_cb(const esp_zb_zcl_set_attr_value_message_t *message) {
46+
bool light_state = 0;
47+
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) {
48+
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) {
49+
light_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : light_state;
50+
log_i("Light sets to %s", light_state ? "On" : "Off");
51+
neopixelWrite(LED_PIN, 255 * light_state, 255 * light_state, 255 * light_state); // Toggle light
52+
}
53+
}
54+
return;
55+
}
56+
57+
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT, light_cb);
58+
59+
/********************* Arduino functions **************************/
60+
void setup() {
61+
// Init RMT and leave light OFF
62+
neopixelWrite(LED_PIN, 0, 0, 0);
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+
//empty, zigbee running in task
75+
}

libraries/Zigbee/examples/Zigbee_Light_Switch/.skip.esp32

Whitespace-only changes.

0 commit comments

Comments
 (0)