-
Notifications
You must be signed in to change notification settings - Fork 3
/
QuadGarageDoor.ino
326 lines (281 loc) · 7.93 KB
/
QuadGarageDoor.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <SoftwareSerial.h>
#include <arduino-timer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <xbee_zha.h>
#include "zha/device_details.h"
#define TEMP_BUS 5
#define XBEE_RST 4
// Define SoftSerial TX/RX pins
#define ssRX 10
#define ssTX 11
#define BASIC_ENDPOINT 1
#define DOOR1_ENDPOINT 2
#define DOOR2_ENDPOINT 3
#define DOOR3_ENDPOINT 4
#define DOOR4_ENDPOINT 5
#define TEMP_ENDPOINT 6
#define MAX_TEMP_ATTEMPTS 10
#define START_LOOPS 100
uint8_t start_fails = 0;
uint8_t init_status_sent = 0;
constexpr uint8_t DOOR_OUT_PINS[] = {12, 13, A0, A1};
constexpr uint8_t DOOR_IN_PINS[] = {9, 8, 7, 6};
void (*resetFunc)(void) = 0;
auto timer = timer_create_default(); // create a timer with default settings
unsigned long loop_time = millis();
unsigned long last_msg_time = loop_time - 1000;
// One wire temp sensors
OneWire oneWire(TEMP_BUS);
DallasTemperature sensors(&oneWire);
// Input Temp
DeviceAddress devThermometer = {0x28, 0x2E, 0x53, 0x45, 0x92, 0x17, 0x02, 0x58};
SoftwareSerial nss(ssRX, ssTX);
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char *sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
int freeMemory()
{
char top;
#ifdef __arm__
return &top - reinterpret_cast<char *>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
return &top - __brkval;
#else // __arm__
return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif // __arm__
}
void setup()
{
pinMode(XBEE_RST, OUTPUT);
// Reset xbee
digitalWrite(XBEE_RST, LOW);
pinMode(XBEE_RST, INPUT);
// Configure Door Pins
for (uint8_t i = 0; i < 4; i++)
{
pinMode(DOOR_OUT_PINS[i], OUTPUT);
digitalWrite(DOOR_OUT_PINS[i], LOW);
pinMode(DOOR_IN_PINS[i], INPUT);
}
Serial.begin(115200);
Serial.println(F("Startup"));
sensors.begin();
nss.begin(9600);
uint8_t temp_attempts = 0;
bool success = 0;
while (!success && temp_attempts < MAX_TEMP_ATTEMPTS)
{
Serial.print(F("Found "));
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(F(" Temp Dev."));
success = sensors.getAddress(devThermometer, 0);
temp_attempts++;
delay(100);
}
if (success)
{
Serial.print(F("Dev Addr: "));
for (int i; i < sizeof(devThermometer); i++)
{
Serial.print(devThermometer[i]);
Serial.print(F(" "));
}
Serial.println();
// We really need this to happen, otherwise get odd results
sensors.setResolution(devThermometer, 9);
}
else
{
Serial.println(F("T Dev Addr Fail"));
}
zha.Start(nss, zhaClstrCmd, zhaWriteAttr, NUM_ENDPOINTS, ENDPOINTS);
// Set up callbacks, shouldn't have to do this here, but bad design...
zha.registerCallbacks(atCmdResp, zbTxStatusResp, otherResp, zdoReceive);
Serial.println(F("CB Conf"));
timer.every(30000, update_sensors);
}
bool read_temp(DeviceAddress thermometer, int16_t *temp)
{
bool success = sensors.requestTemperaturesByAddress(thermometer);
if (!success)
{
Serial.println(F("T addr not found"));
return false;
}
delay(10); // No idea why this is needed
float TempC = sensors.getTempC(thermometer);
if (TempC == DEVICE_DISCONNECTED_C)
{
Serial.println(F("T Disconnect"));
return false;
}
Serial.print(F("Temp: "));
Serial.print(String(TempC, 2));
Serial.println(F("°C"));
*temp = (int16_t)(TempC * 100.0);
return true;
}
void update_temp()
{
// Temp
int16_t res_temp;
bool r_success = read_temp(devThermometer, &res_temp);
if (r_success && zha.dev_status == READY)
{
Endpoint end_point = zha.GetEndpoint(TEMP_ENDPOINT);
Cluster cluster = end_point.GetCluster(TEMP_CLUSTER_ID);
attribute *attr;
uint8_t attr_exists = cluster.GetAttr(&attr, CURRENT_STATE);
attr->SetValue(res_temp);
zha.sendAttributeRpt(cluster.id, attr, end_point.id, 1);
}
}
bool update_sensors(void *)
{
update_temp();
return true;
}
void update_door_state(uint8_t force)
{
for (uint8_t i = 0; i < 4; i++)
{
uint8_t val = digitalRead(DOOR_IN_PINS[i]) ^ 1;
Endpoint end_point = zha.GetEndpoint(i + 2);
Cluster cluster = end_point.GetCluster(ON_OFF_CLUSTER_ID);
attribute *attr;
uint8_t attr_exists = cluster.GetAttr(&attr, CURRENT_STATE);
if ((val != attr->GetIntValue(0x00)) || force)
{
Serial.print(F("EP"));
Serial.print(end_point.id);
Serial.print(F(": "));
Serial.print(attr->GetIntValue(0x00));
Serial.print(F(" Now "));
attr->SetValue(val);
Serial.println(attr->GetIntValue(0x00));
zha.sendAttributeRpt(cluster.id, attr, end_point.id, 1);
}
}
}
void SetAttr(uint8_t ep_id, uint16_t cluster_id, uint16_t attr_id, uint8_t value, uint8_t rqst_seq_id)
{
Endpoint end_point = zha.GetEndpoint(ep_id);
Cluster cluster = end_point.GetCluster(cluster_id);
attribute *attr;
uint8_t attr_exists = cluster.GetAttr(&attr, attr_id);
Serial.print("Clstr: ");
Serial.println(cluster_id, HEX);
if (cluster_id == ON_OFF_CLUSTER_ID)
{
// We don't want to set value here, value is set by the door opening or closing
if (value == 0x00 || 0x01)
{
Serial.print(F("Toggle: "));
Serial.println(end_point.id);
digitalWrite(DOOR_OUT_PINS[end_point.id - 1], HIGH);
zha.sendAttributeCmdRsp(cluster_id, attr, ep_id, 1, value, zha.cmd_seq_id); // Tell sender that we did what we were told to
delay(2000);
digitalWrite(DOOR_OUT_PINS[end_point.id - 1], LOW);
}
}
}
void loop()
{
zha.loop();
if (zha.dev_status == READY)
{
if (!init_status_sent)
{
Serial.println(F("Snd Init States"));
update_door_state(0x01);
init_status_sent = 1;
}
else
{
update_door_state(0x00);
if ((loop_time - last_msg_time) > 1000)
{
Serial.print(F("Mem: "));
Serial.println(freeMemory());
last_msg_time = millis();
}
}
}
else if ((loop_time - last_msg_time) > 1000)
{
Serial.print(F("Not Started "));
Serial.print(start_fails);
Serial.print(F(" of "));
Serial.println(START_LOOPS);
last_msg_time = millis();
if (start_fails == 15)
{
// Sometimes we don't get a response from dev ann, try a transmit and see if we are good
update_door_state(0x01);
}
if (start_fails > START_LOOPS)
{
resetFunc();
}
start_fails++;
}
timer.tick();
loop_time = millis();
}
void zhaWriteAttr(ZBExplicitRxResponse &erx)
{
// Not used
Serial.println(F("Write Cmd"));
}
void zhaClstrCmd(ZBExplicitRxResponse &erx)
{
Serial.println(F("Clstr Cmd"));
if ((erx.getDstEndpoint() == DOOR1_ENDPOINT) ||
(erx.getDstEndpoint() == DOOR2_ENDPOINT) ||
(erx.getDstEndpoint() == DOOR3_ENDPOINT) ||
(erx.getDstEndpoint() == DOOR4_ENDPOINT))
{
Serial.println(F("Door Ep"));
if (erx.getClusterId() == ON_OFF_CLUSTER_ID)
{
Serial.println(F("ON/OFF"));
Endpoint end_point = zha.GetEndpoint(erx.getDstEndpoint());
uint8_t len_data = erx.getDataLength() - 3;
uint8_t new_state = erx.getFrameData()[erx.getDataOffset() + 2];
uint16_t attr_rqst[len_data / 2];
for (uint8_t i = erx.getDataOffset(); i < (erx.getDataLength() + erx.getDataOffset() + 3); i++)
{
Serial.print(erx.getFrameData()[i], HEX);
Serial.print(F(" "));
}
Serial.println();
if (new_state == 0x00)
{
Serial.println(F("Off"));
SetAttr(erx.getDstEndpoint(), erx.getClusterId(), CURRENT_STATE, new_state, erx.getFrameData()[erx.getDataOffset() + 1]);
}
else if (new_state == 0x01)
{
Serial.println(F("On"));
SetAttr(erx.getDstEndpoint(), erx.getClusterId(), CURRENT_STATE, new_state, erx.getFrameData()[erx.getDataOffset() + 1]);
}
else
{
Serial.print(F("Inv State: "));
Serial.println(new_state, HEX);
}
}
}
else if (erx.getDstEndpoint() == TEMP_ENDPOINT)
{
Serial.println(F("Temp Ep"));
}
else
{
Serial.println(F("Inv Ep"));
}
}