Skip to content

Commit ec60ed4

Browse files
committed
Dev Update: Color DImmable light + switch implemented
Implemeted color dimmable light and color dimmer switch HA devices + examples. Removed unnecessary stored attribute cluster Renamed on/off light and switch examples
1 parent 06b0700 commit ec60ed4

30 files changed

+1113
-7
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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 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+
* 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 "Zigbee_core.h"
34+
#include "ep/ep_color_dimmable_light.h"
35+
36+
#include "esp_zigbee_core.h"
37+
#include "freertos/FreeRTOS.h"
38+
#include "freertos/task.h"
39+
#include "ha/esp_zigbee_ha_standard.h"
40+
41+
#define LED_PIN RGB_BUILTIN
42+
#define BUTTON_PIN 9 // C6/H2 Boot button
43+
#define ZIGBEE_LIGHT_ENDPOINT 10 /* esp light bulb device endpoint, used to process light controlling commands */
44+
45+
class MyZigbeeColorLight : public ZigbeeColorDimmableLight {
46+
public:
47+
// Constructor that passes parameters to the base class constructor
48+
MyZigbeeColorLight(uint8_t endpoint) : ZigbeeColorDimmableLight(endpoint) {}
49+
50+
// Override the set_on_off function
51+
void setOnOff(bool value) override {
52+
if (value == false) {
53+
neopixelWrite(LED_PIN, 0, 0, 0); // Turn off light
54+
} else {
55+
updateLight(); // Turn on light on last color and level
56+
}
57+
}
58+
59+
// Override the set_level function
60+
void setLevel(uint8_t level) override {
61+
if (level == 0) {
62+
neopixelWrite(LED_PIN, 0, 0, 0); // Turn off light and dont update ratio
63+
return;
64+
}
65+
_ratio = (float)level / 255;
66+
updateLight();
67+
}
68+
69+
// Override the set_color function
70+
void setColor(uint8_t red, uint8_t green, uint8_t blue) override {
71+
_red = red;
72+
_green = green;
73+
_blue = blue;
74+
updateLight();
75+
}
76+
77+
void updateLight() {
78+
neopixelWrite(LED_PIN, _red * _ratio, _green * _ratio, _blue * _ratio); // Update light
79+
}
80+
private:
81+
// Add your custom attributes and methods here
82+
float _ratio = 1.0;
83+
uint8_t _red = 255;
84+
uint8_t _green = 255;
85+
uint8_t _blue = 255;
86+
87+
};
88+
89+
MyZigbeeColorLight zbColorLight = MyZigbeeColorLight(ZIGBEE_LIGHT_ENDPOINT);
90+
91+
/********************* Arduino functions **************************/
92+
void setup() {
93+
// Init RMT and leave light OFF
94+
neopixelWrite(LED_PIN, 0, 0, 0);
95+
96+
// Init button for factory reset
97+
pinMode(BUTTON_PIN, INPUT);
98+
99+
//Optional: set Zigbee device name and model
100+
zbColorLight.setManufacturerAndModel("Espressif", "ZBColorLightBulb");
101+
102+
//Add endpoint to Zigbee Core
103+
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
104+
Zigbee.addEndpoint(&zbColorLight);
105+
106+
// When all EPs are registered, start Zigbee. By default acts as ZIGBEE_END_DEVICE
107+
log_d("Calling Zigbee.begin()");
108+
Zigbee.begin();
109+
}
110+
111+
void loop() {
112+
// Cheking button for factory reset
113+
if (digitalRead(BUTTON_PIN) == LOW) { // Push button pressed
114+
// Key debounce handling
115+
delay(100);
116+
int startTime = millis();
117+
while (digitalRead(BUTTON_PIN) == LOW) {
118+
delay(50);
119+
if((millis() - startTime) > 3000) {
120+
// If key pressed for more than 3secs, factory reset Zigbee and reboot
121+
Serial.printf("Reseting Zigbee to factory settings, reboot.\n");
122+
Zigbee.factoryReset();
123+
}
124+
}
125+
}
126+
delay(100);
127+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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 switch.
17+
*
18+
* The example demonstrates how to use ESP Zigbee stack to control a light bulb.
19+
* The light bulb is a Zigbee end device, which is controlled by a Zigbee coordinator (Switch).
20+
* Button switch and Zigbee runs in separate tasks.
21+
*
22+
* Proper Zigbee mode must be selected in Tools->Zigbee mode
23+
* and also the correct partition scheme must be selected in Tools->Partition Scheme.
24+
*
25+
* Please check the README.md for instructions and more detailed description.
26+
*
27+
* Created by Jan Procházka (https://github.com/P-R-O-C-H-Y/)
28+
*/
29+
30+
#ifndef ZIGBEE_MODE_ZCZR
31+
#error "Zigbee coordinator mode is not selected in Tools->Zigbee mode"
32+
#endif
33+
34+
#include "Zigbee_core.h"
35+
#include "ep/ep_color_dimmer_switch.h"
36+
37+
#include "esp_zigbee_core.h"
38+
#include "freertos/FreeRTOS.h"
39+
#include "freertos/task.h"
40+
#include "ha/esp_zigbee_ha_standard.h"
41+
42+
#define SWITCH_ENDPOINT_NUMBER 5
43+
44+
/* Switch configuration */
45+
#define GPIO_SWITCH GPIO_NUM_9 //Boot button for C6/H2
46+
47+
/* Zigbee switch */
48+
ZigbeeColorDimmerSwitch zbSwitch = ZigbeeColorDimmerSwitch(SWITCH_ENDPOINT_NUMBER);
49+
50+
/********************* Arduino functions **************************/
51+
void setup() {
52+
53+
Serial.begin(115200);
54+
55+
// Init button switch
56+
pinMode(GPIO_SWITCH, INPUT);
57+
58+
//Optional: set Zigbee device name and model
59+
zbSwitch.setManufacturerAndModel("Espressif", "ZigbeeSwitch");
60+
61+
//Optional to allow multiple light to bind to the switch
62+
zbSwitch.allowMultipleBinding(true);
63+
64+
//Add endpoint to Zigbee Core
65+
Zigbee.addEndpoint(&zbSwitch);
66+
67+
//Open network for 180 seconds after boot
68+
Zigbee.setRebootOpenNetwork(180);
69+
70+
// When all EPs are registered, start Zigbee with ZIGBEE_COORDINATOR mode
71+
Zigbee.begin(ZIGBEE_COORDINATOR);
72+
73+
Serial.println("Waiting for Light to bound to the switch");
74+
//Wait for switch to bound to a light:
75+
while(!zbSwitch.is_bound())
76+
{
77+
Serial.printf(".");
78+
delay(500);
79+
}
80+
Serial.println();
81+
}
82+
83+
void loop() {
84+
// Handle button switch in loop()
85+
if (digitalRead(GPIO_SWITCH) == LOW) { // Push button pressed
86+
87+
// Key debounce handling
88+
while (digitalRead(GPIO_SWITCH) == LOW) {
89+
delay(50);
90+
}
91+
// Toggle light
92+
zbSwitch.lightToggle();
93+
}
94+
// Handle serial input to controll color and level of the light
95+
if (Serial.available()) {
96+
String command = Serial.readString();
97+
98+
if (command == "on") {
99+
zbSwitch.lightOn();
100+
} else if (command == "off") {
101+
zbSwitch.lightOff();
102+
} else if (command == "toggle") {
103+
zbSwitch.lightToggle();
104+
} else if (command == "red") {
105+
zbSwitch.setLightColor(255, 0, 0);
106+
} else if (command == "green") {
107+
zbSwitch.setLightColor(0, 255, 0);
108+
} else if (command == "blue") {
109+
zbSwitch.setLightColor(0, 0, 255);
110+
} else if (command == "white") {
111+
zbSwitch.setLightColor(255, 255, 255);
112+
} else if (command == "color") {
113+
//wait for color value
114+
Serial.println("Enter red value (0-255):");
115+
while (!Serial.available()) {
116+
delay(100);
117+
}
118+
int red = Serial.parseInt();
119+
Serial.println("Enter green value (0-255):");
120+
while (!Serial.available()) {
121+
delay(100);
122+
}
123+
int green = Serial.parseInt();
124+
Serial.println("Enter blue value (0-255):");
125+
while (!Serial.available()) {
126+
delay(100);
127+
}
128+
int blue = Serial.parseInt();
129+
zbSwitch.setLightColor(red, green, blue);
130+
} else if (command == "level") {
131+
//wait for level value
132+
Serial.println("Enter level value (0-255):");
133+
while (!Serial.available()) {
134+
delay(100);
135+
}
136+
int level = Serial.parseInt();
137+
zbSwitch.setLightLevel(level);
138+
} else {
139+
Serial.println("Unknown command");
140+
}
141+
}
142+
143+
// print the bound lights every 30 seconds
144+
static uint32_t last_print = 0;
145+
if (millis() - last_print > 30000) {
146+
last_print = millis();
147+
zbSwitch.printBoundLights();
148+
}
149+
}

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.

0 commit comments

Comments
 (0)