-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdash_zigbee.cpp
More file actions
164 lines (134 loc) · 3.97 KB
/
Copy pathdash_zigbee.cpp
File metadata and controls
164 lines (134 loc) · 3.97 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "dash_zigbee.h"
bool s_light_state = false;
bool s_zigbee_linked = false;
bool s_zigbee_pairing_failed = false;
#if USE_ZIGBEE
// Zigbee switch endpoint used to control the paired Ikea switch.
static ZigbeeSwitch zbSwitch(SWITCH_ENDPOINT_NUMBER);
static bool s_endpoint_registered = false;
static void onLightStateChange(bool state) {
s_light_state = state;
Serial.printf("[ZB] Light state -> %s\n", state ? "ON" : "OFF");
}
static void printBoundDevicesInfo() {
if (!zbSwitch.bound()) {
Serial.println("[ZB] No bound devices found.");
return;
}
Serial.println("\n[ZB] Bound devices info:");
for (const auto &device : zbSwitch.getBoundDevices()) {
if (!device) continue;
Serial.printf("[ZB] Bound endpoint=%u short=0x%04x\n", device->endpoint, device->short_addr);
}
zbSwitch.getLightState();
}
static bool beginCoordinatorSession(bool allowPairing, uint32_t waitForBindingMs) {
s_light_state = false;
Serial.println("\n[ZB] Activating Zigbee coordinator");
zbSwitch.onLightStateChange(onLightStateChange);
zbSwitch.allowMultipleBinding(true);
zbSwitch.setManufacturerAndModel("DIY", "ESP32-C6-Coordinator");
if (!s_endpoint_registered) {
Zigbee.addEndpoint(&zbSwitch);
s_endpoint_registered = true;
}
Zigbee.setRebootOpenNetwork(0);
if (!Zigbee.begin(ZIGBEE_COORDINATOR)) {
Serial.println("[ZB] Failed to start Zigbee");
ESP.restart();
}
if (allowPairing) {
Serial.printf("[ZB] Opening pairing window for %lu seconds\n", waitForBindingMs / 1000UL);
Zigbee.openNetwork(waitForBindingMs / 1000UL);
}
uint32_t waitStart = millis();
while (!zbSwitch.bound() && (millis() - waitStart) < waitForBindingMs) {
Serial.print(".");
delay(500);
}
s_zigbee_linked = zbSwitch.bound();
s_zigbee_pairing_failed = allowPairing && !s_zigbee_linked;
if (!s_zigbee_linked) {
Serial.println("\n[ZB] No bound device after pairing window.");
return false;
}
vTaskDelay(300 / portTICK_PERIOD_MS);
return true;
}
static void endCoordinatorSession() {
printBoundDevicesInfo();
vTaskDelay(3000 / portTICK_PERIOD_MS);
Zigbee.setRebootOpenNetwork(0);
vTaskDelay(100 / portTICK_PERIOD_MS);
Zigbee.closeNetwork();
}
void activateCoordinatorReadAndClose(bool flipSwitch) {
if (!beginCoordinatorSession(true, 60000)) {
endCoordinatorSession();
return;
}
if (flipSwitch) {
Serial.println("[ZB] Toggle switch");
zbSwitch.lightToggle();
vTaskDelay(500 / portTICK_PERIOD_MS);
}
Serial.println("[ZB] Reading switch state...");
zbSwitch.getLightState();
vTaskDelay(300 / portTICK_PERIOD_MS);
endCoordinatorSession();
}
void closeZigbee() {
Serial.println("[ZB] Preparing to close network...");
Serial.println(s_light_state);
vTaskDelay(3000 / portTICK_PERIOD_MS);
Serial.println("[ZB] Disable Zigbee network...");
Zigbee.setRebootOpenNetwork(0);
Serial.println(F("[ZB] Closing network"));
delay(100);
Zigbee.closeNetwork();
}
void toogleZigbee() {
zbSwitch.lightToggle();
}
void readZigbee() {
Serial.println("[ZB] Reading switch state...");
zbSwitch.getLightState();
delay(200);
zbSwitch.lightToggle();
delay(200);
}
bool syncZigbeePower(bool turnOn) {
if (!beginCoordinatorSession(!s_zigbee_linked, s_zigbee_linked ? 2000 : 60000)) {
endCoordinatorSession();
return false;
}
Serial.printf("[ZB] Setting switch %s\n", turnOn ? "ON" : "OFF");
bool ok = turnOn ? switchOn() : switchOff();
vTaskDelay(500 / portTICK_PERIOD_MS);
if (ok) {
s_light_state = turnOn;
zbSwitch.getLightState();
vTaskDelay(300 / portTICK_PERIOD_MS);
}
endCoordinatorSession();
return ok;
}
bool ensureZigbeePaired() {
if (beginCoordinatorSession(true, 60000)) {
endCoordinatorSession();
return true;
}
endCoordinatorSession();
return false;
}
bool switchOn() {
if (!zbSwitch.bound()) return false;
zbSwitch.lightOn();
return true;
}
bool switchOff() {
if (!zbSwitch.bound()) return false;
zbSwitch.lightOff();
return true;
}
#endif