forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZigbee_Dimmable_Light.ino
132 lines (115 loc) · 3.77 KB
/
Zigbee_Dimmable_Light.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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 Dimmable light bulb.
*
* The example demonstrates how to use Zigbee library to create an end device with
* 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 "Zigbee.h"
/* Zigbee dimmable light configuration */
#define ZIGBEE_LIGHT_ENDPOINT 10
uint8_t led = RGB_BUILTIN;
uint8_t button = BOOT_PIN;
ZigbeeDimmableLight zbDimmableLight = ZigbeeDimmableLight(ZIGBEE_LIGHT_ENDPOINT);
/********************* RGB LED functions **************************/
void setLight(bool state, uint8_t level)
{
if (!state)
{
rgbLedWrite(led, 0, 0, 0);
return;
}
rgbLedWrite(led, level, level, level);
}
// 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
zbDimmableLight.restoreLight();
return;
}
rgbLedWrite(led, 255 * blink, 255 * blink, 255 * blink);
blink = !blink;
}
/********************* Arduino functions **************************/
void setup()
{
Serial.begin(115200);
// Init RMT and leave light OFF
rgbLedWrite(led, 0, 0, 0);
// Init button for factory reset
pinMode(button, INPUT_PULLUP);
// Set callback function for light change
zbDimmableLight.onLightChange(setLight);
// Optional: Set callback function for device identify
zbDimmableLight.onIdentify(identify);
// Optional: Set Zigbee device name and model
zbDimmableLight.setManufacturerAndModel("Espressif", "ZBLightBulb");
// Add endpoint to Zigbee Core
Serial.println("Adding ZigbeeLight endpoint to Zigbee Core");
Zigbee.addEndpoint(&zbDimmableLight);
// When all EPs are registered, start Zigbee in End Device mode
if (!Zigbee.begin())
{
Serial.println("Zigbee failed to start!");
Serial.println("Rebooting...");
ESP.restart();
}
Serial.println("Connecting to network");
while (!Zigbee.connected())
{
Serial.print(".");
delay(100);
}
Serial.println();
}
void loop()
{
// Checking button for factory reset
if (digitalRead(button) == LOW)
{ // Push button pressed
// Key debounce handling
delay(100);
int startTime = millis();
while (digitalRead(button) == LOW)
{
delay(50);
if ((millis() - startTime) > 3000)
{
// If key pressed for more than 3secs, factory reset Zigbee and reboot
Serial.println("Resetting Zigbee to factory and rebooting in 1s.");
delay(1000);
Zigbee.factoryReset();
}
}
// Increase blightness by 50 every time the button is pressed
zbDimmableLight.setLightLevel(zbDimmableLight.getLightLevel() + 50);
}
delay(100);
}