Skip to content

Commit f321d43

Browse files
committed
Refactor classes methods + implement Identify command
1 parent 9a354ae commit f321d43

16 files changed

+252
-290
lines changed

libraries/Zigbee/examples/Zigbee_Color_Dimmable_Light/Zigbee_Color_Dimmable_Light.ino

+27-46
Original file line numberDiff line numberDiff line change
@@ -36,53 +36,28 @@
3636

3737
#define LED_PIN RGB_BUILTIN
3838
#define BUTTON_PIN 9 // C6/H2 Boot button
39-
#define ZIGBEE_LIGHT_ENDPOINT 10 /* esp light bulb device endpoint, used to process light controlling commands */
39+
#define ZIGBEE_LIGHT_ENDPOINT 10
4040

41-
class MyZigbeeColorLight : public ZigbeeColorDimmableLight {
42-
public:
43-
// Constructor that passes parameters to the base class constructor
44-
MyZigbeeColorLight(uint8_t endpoint) : ZigbeeColorDimmableLight(endpoint) {}
41+
ZigbeeColorDimmableLight zbColorLight = ZigbeeColorDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
4542

46-
// Override the set_on_off function
47-
void setOnOff(bool value) override {
48-
if (value == false) {
49-
rgbLedWrite(LED_PIN, 0, 0, 0); // Turn off light
50-
} else {
51-
updateLight(); // Turn on light on last color and level
52-
}
53-
}
54-
55-
// Override the set_level function
56-
void setLevel(uint8_t level) override {
57-
if (level == 0) {
58-
rgbLedWrite(LED_PIN, 0, 0, 0); // Turn off light and dont update ratio
59-
return;
60-
}
61-
_ratio = (float)level / 255;
62-
updateLight();
63-
}
64-
65-
// Override the set_color function
66-
void setColor(uint8_t red, uint8_t green, uint8_t blue) override {
67-
_red = red;
68-
_green = green;
69-
_blue = blue;
70-
updateLight();
71-
}
72-
73-
void updateLight() {
74-
rgbLedWrite(LED_PIN, _red * _ratio, _green * _ratio, _blue * _ratio); // Update light
75-
}
76-
private:
77-
// Add your custom attributes and methods here
78-
float _ratio = 1.0;
79-
uint8_t _red = 255;
80-
uint8_t _green = 255;
81-
uint8_t _blue = 255;
82-
83-
};
43+
/********************* RGB LED functions **************************/
44+
void setRGBLight(bool state, uint8_t red, uint8_t green, uint8_t blue, uint8_t level) {
45+
float brightness = (float)level / 255;
46+
rgbLedWrite(LED_PIN, red * brightness, green * brightness, blue * brightness);
47+
}
8448

85-
MyZigbeeColorLight zbColorLight = MyZigbeeColorLight(ZIGBEE_LIGHT_ENDPOINT);
49+
// Create a task on identify call to handle the identify function
50+
void identify(uint16_t time) {
51+
static uint8_t blink = 1;
52+
log_d("Identify called for %d seconds", time);
53+
if (time == 0) {
54+
// If identify time is 0, stop blinking and restore light as it was used for identify
55+
zbColorLight.restoreLight();
56+
return;
57+
}
58+
rgbLedWrite(LED_PIN, 255 * blink, 255 * blink, 255 * blink);
59+
blink = !blink;
60+
}
8661

8762
/********************* Arduino functions **************************/
8863
void setup() {
@@ -92,10 +67,16 @@ void setup() {
9267
// Init button for factory reset
9368
pinMode(BUTTON_PIN, INPUT);
9469

95-
//Optional: set Zigbee device name and model
70+
// Set callback function for light change
71+
zbColorLight.onLightChange(setRGBLight);
72+
73+
// Optional: Set callback function for device identify
74+
zbColorLight.onIdentify(identify);
75+
76+
// Optional: Set Zigbee device name and model
9677
zbColorLight.setManufacturerAndModel("Espressif", "ZBColorLightBulb");
9778

98-
//Add endpoint to Zigbee Core
79+
// Add endpoint to Zigbee Core
9980
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
10081
Zigbee.addEndpoint(&zbColorLight);
10182

libraries/Zigbee/examples/Zigbee_Color_Dimmer_Switch/Zigbee_Color_Dimmer_Switch.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "ep/ZigbeeColorDimmerSwitch.h"
4040

4141
/* Switch configuration */
42-
#define SWITCH_PIN 9 //Boot button for C6/H2
42+
#define SWITCH_PIN 9 // ESP32-C6/H2 Boot button
4343
#define SWITCH_ENDPOINT_NUMBER 5
4444

4545
/* Zigbee switch */

libraries/Zigbee/examples/Zigbee_On_Off_Light/Zigbee_On_Off_Light.ino

+10-13
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,15 @@
3434
#include "ep/ZigbeeLight.h"
3535

3636
#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 */
37+
#define BUTTON_PIN 9 // ESP32-C6/H2 Boot button
38+
#define ZIGBEE_LIGHT_ENDPOINT 10
3939

40-
class MyZigbeeLight : public ZigbeeLight {
41-
public:
42-
// Constructor that passes parameters to the base class constructor
43-
MyZigbeeLight(uint8_t endpoint) : ZigbeeLight(endpoint) {}
40+
ZigbeeLight zbLight = ZigbeeLight(ZIGBEE_LIGHT_ENDPOINT);
4441

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);
42+
/********************* RGB LED functions **************************/
43+
void setLED(bool value) {
44+
rgbLedWrite(LED_PIN, 255 * value, 255 * value, 255 * value);
45+
}
5246

5347
/********************* Arduino functions **************************/
5448
void setup() {
@@ -61,6 +55,9 @@ void setup() {
6155
//Optional: set Zigbee device name and model
6256
zbLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
6357

58+
// Set callback function for light change
59+
zbLight.onLightChange(setLED);
60+
6461
//Add endpoint to Zigbee Core
6562
log_d("Adding ZigbeeLight endpoint to Zigbee Core");
6663
Zigbee.addEndpoint(&zbLight);

libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

+12-19
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,7 @@ typedef enum {
6565

6666
static SwitchData buttonFunctionPair[] = {{GPIO_INPUT_IO_TOGGLE_SWITCH, SWITCH_ONOFF_TOGGLE_CONTROL}};
6767

68-
/* Zigbee switch */
69-
class MyZigbeeSwitch : public ZigbeeSwitch {
70-
public:
71-
// Constructor that passes parameters to the base class constructor
72-
MyZigbeeSwitch(uint8_t endpoint) : ZigbeeSwitch(endpoint) {}
73-
74-
// Override the set_on_off function
75-
void readManufacturer(char* manufacturer) override {
76-
//Do what you want with the manufacturer string
77-
Serial.printf("Manufacturer: %s\n", manufacturer);
78-
}
79-
void readModel(char* model) override {
80-
//Do what you want with the model string
81-
Serial.printf("Model: %s\n", model);
82-
}
83-
};
84-
85-
MyZigbeeSwitch zbSwitch = MyZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
68+
ZigbeeSwitch zbSwitch = ZigbeeSwitch(SWITCH_ENDPOINT_NUMBER);
8669

8770
/********************* Zigbee functions **************************/
8871
static void onZbButton(SwitchData *button_func_pair) {
@@ -127,7 +110,6 @@ void setup() {
127110
//Open network for 180 seconds after boot
128111
Zigbee.setRebootOpenNetwork(180);
129112

130-
131113
// Init button switch
132114
for (int i = 0; i < PAIR_SIZE(buttonFunctionPair); i++) {
133115
pinMode(buttonFunctionPair[i].pin, INPUT_PULLUP);
@@ -151,6 +133,17 @@ void setup() {
151133
Serial.printf(".");
152134
delay(500);
153135
}
136+
137+
// Optional: read manufacturer and model name from the bound light
138+
std::list<zb_device_params_t*> boundLights = zbSwitch.getBoundDevices();
139+
//List all bound lights
140+
for (const auto& device : boundLights) {
141+
Serial.printf("Device on endpoint %d, short address: 0x%x\n", device->endpoint, device->short_addr);
142+
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]);
143+
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr));
144+
Serial.printf("Light model: %s", zbSwitch.readModel(device->endpoint, device->short_addr));
145+
}
146+
154147
Serial.println();
155148
}
156149

libraries/Zigbee/examples/Zigbee_Thermostat/Zigbee_Thermostat.ino

+28-41
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,37 @@
3737
#define BUTTON_PIN 9 // Boot button for C6/H2
3838
#define THERMOSTAT_ENDPOINT_NUMBER 5
3939

40-
class MyZigbeeThermostat : public ZigbeeThermostat {
41-
public:
42-
// Constructor that passes parameters to the base class constructor
43-
MyZigbeeThermostat(uint8_t endpoint) : ZigbeeThermostat(endpoint) {}
44-
45-
// Override virtual functions from ZigbeeThermostat to handle temperature sensor data
46-
void temperatureRead(float temp) override {
47-
if (temperature != temp) {
48-
Serial.printf("Temperature sensor value changed to: %.2f°C\n", temp);
49-
temperature = temp;
50-
}
51-
}
52-
53-
void temperatureMin(float temp) override {
54-
Serial.printf("Temperature sensor min value: %.2f°C\n", temp);
55-
min_temperature = temp;
56-
}
57-
58-
void temperatureMax(float temp) override {
59-
Serial.printf("Temperature sensor max value: %.2f°C\n", temp);
60-
max_temperature = temp;
61-
}
62-
63-
void temperatureTolerance(float tolerance) override {
64-
Serial.printf("Temperature sensor tolerance: %.2f°C\n", tolerance);
65-
temp_tolerance = tolerance;
66-
}
67-
68-
float temperature;
69-
float max_temperature;
70-
float min_temperature;
71-
float temp_tolerance;
72-
};
73-
74-
MyZigbeeThermostat zbThermostat = MyZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);
40+
ZigbeeThermostat zbThermostat = ZigbeeThermostat(THERMOSTAT_ENDPOINT_NUMBER);
41+
42+
// Save temperature sensor data
43+
float sensor_temp;
44+
float sensor_max_temp;
45+
float sensor_min_temp;
46+
float sensor_tolerance;
47+
48+
/****************** Temperature sensor handling *******************/
49+
void recieveSensorTemp(float temperature) {
50+
Serial.printf("Temperature sensor value: %.2f°C\n", temperature);
51+
sensor_temp = temperature;
52+
}
7553

54+
void recieveSensorConfig(float min_temp, float max_temp, float tolerance) {
55+
Serial.printf("Temperature sensor settings: min %.2f°C, max %.2f°C, tolerance %.2f°C\n", min_temp, max_temp, tolerance);
56+
sensor_min_temp = min_temp;
57+
sensor_max_temp = max_temp;
58+
sensor_tolerance = tolerance;
59+
}
7660
/********************* Arduino functions **************************/
7761
void setup() {
78-
7962
Serial.begin(115200);
8063

8164
// Init button switch
8265
pinMode(BUTTON_PIN, INPUT);
8366

67+
// Set callback functions for temperature and configuration recieve
68+
zbThermostat.onTempRecieve(recieveSensorTemp);
69+
zbThermostat.onConfigRecieve(recieveSensorConfig);
70+
8471
//Optional: set Zigbee device name and model
8572
zbThermostat.setManufacturerAndModel("Espressif", "ZigbeeThermostat");
8673

@@ -94,16 +81,16 @@ void setup() {
9481
Zigbee.begin(ZIGBEE_COORDINATOR);
9582

9683
Serial.println("Waiting for Temperature sensor to bound to the switch");
84+
9785
//Wait for switch to bound to a light:
9886
while(!zbThermostat.isBound())
9987
{
10088
Serial.printf(".");
10189
delay(500);
10290
}
10391

104-
// Get temperature sensor value to update min and max values
105-
zbThermostat.getTemperature();
106-
92+
// Get temperature sensor configuration
93+
zbThermostat.getSensorSettings();
10794
Serial.println();
10895
}
10996

@@ -124,7 +111,7 @@ void loop() {
124111
static uint32_t last_print = 0;
125112
if (millis() - last_print > 10000) {
126113
last_print = millis();
127-
int temp_percent = (int)((zbThermostat.temperature - zbThermostat.min_temperature) / (zbThermostat.max_temperature - zbThermostat.min_temperature) * 100);
128-
Serial.printf("Temperature: %.2f°C (%d %%)\n", zbThermostat.temperature, temp_percent);
114+
int temp_percent = (int)((sensor_temp - sensor_min_temp) / (sensor_max_temp - sensor_min_temp) * 100);
115+
Serial.printf("Loop temperature info: %.2f°C (%d %%)\n", sensor_temp, temp_percent);
129116
}
130117
}

0 commit comments

Comments
 (0)