-
Notifications
You must be signed in to change notification settings - Fork 0
/
garageDoorNotifier.ino
executable file
·463 lines (415 loc) · 12.5 KB
/
garageDoorNotifier.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//---------------------------------------------------------------
// includes
//---------------------------------------------------------------
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "WifiCredentials.h"
//---------------------------------------------------------------
// macros
//---------------------------------------------------------------
// Comment this out to disable prints and save space
// TODO this was moved from above the includes. that doesnt make sense to me.does it matter?
//#define BLYNK_PRINT Serial
#define DBG_ENABLED 1
#if DBG_ENABLED
#define DBG_PRINT(fn, msg) debug_print(fn, msg)
#else
#define DBG_PRINT(fn, msg) 0; // do nothing
#endif
//---------------------------------------------------------------
// Types
//---------------------------------------------------------------
typedef unsigned int UInt32;
typedef unsigned short UInt16;
typedef unsigned char UInt8;
typedef int Int32;
typedef short Int16;
typedef char Int8;
// LED state machine
// TODO change name to pin/garage door state machine
enum eLEDSTATUS
{
eUNINIT,
eON,
eWAIT_ON,
eOFF
};
enum eDOORSTATUS
{
eDOOR_UNKNOWN,
eDOOR_OPEN,
eDOOR_CLOSED
};
//---------------------------------------------------------------
// constants
//---------------------------------------------------------------
String mVersionNumber = "-------VERSION 0.5-------";
const Int32 LED_PIN = 5; // ESP8266 onboard, green LED
const Int32 TOGGLE_DURATION = 1000; // full second
const Int32 US_PIN = 15; // ultrasonic sensor Pulse Width pin
const Int32 SENREAD_INTERVAL = 5000 ; // five seconds in ms.
const Int32 SENREAD_ERR = 100; // 2% error in case other processes take too long
const Int32 PULSE_TIMEOUT = 100000; //100ms timeout // TODO decrease?
const Int32 SEN_THRESH_ERR = 580; // 10cm (580us /29/2 = 10cm)
const Int32 EEPROM_ADDR = 0; // address where to save threshold
//---------------------------------------------------------------
// member variables
//---------------------------------------------------------------
// used for different data to the email/callback
Int32 mButtonPressCnt = 0;
// used for the current LED status (toggling the pin)
eLEDSTATUS mStatus = eUNINIT;
// used for counting how long the pin has been pressed
UInt32 mTargetTime = 0;
// used for counting how long we should run until checking the Ultrasonic sensor
UInt32 mReadSenCnt = 0;
// used for the Ultrasonic sensor read value
UInt32 mSenVal = 0;
// threshold to trigger when the door is open (0 is unset)
UInt32 mSenThreshold = 0;
// state of the garage door
eDOORSTATUS mDoorStatus = eDOOR_UNKNOWN;
// cheater way for hysteresis
bool doorStatusHyst = false;
// terminal for status updates
WidgetTerminal mTerminal(V10);
//---------------------------------------------------------------
// setup
//
// setup the wifi, pins, and assign interupts
//---------------------------------------------------------------
void setup()
{
#if DBG_ENABLED
Serial.begin(9600);
#endif
// print the version number
DBG_PRINT("", ""); // intentional blank line
DBG_PRINT("", "--------------------------");
DBG_PRINT("", mVersionNumber );
DBG_PRINT("", "--------------------------");
// connect to wifi
DBG_PRINT("setup", " connect to wifi ");
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
DBG_PRINT("setup", "connection success");
// read in the persisted threshold from EEPROM
mSenThreshold = readEE(EEPROM_ADDR);
DBG_PRINT("setup", String("EEPROM threshold = " + String(microsecondsToCentimeters(mSenThreshold)) + "cm" ) );
mTerminal.println( mVersionNumber );
mTerminal.println("--------------------------");
mTerminal.println("Door threshold = " + String(microsecondsToCentimeters(mSenThreshold)) + " cm");
mTerminal.flush();
// start the sensor as output
pinMode(US_PIN, OUTPUT);
digitalWrite(US_PIN, LOW);
// setup the LED/garage door pin
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// attach interrrupt to the button
}
//---------------------------------------------------------------
// loop
// keep the blynk running
// update the pin state machine
// read the sensor
//---------------------------------------------------------------
void loop()
{
Blynk.run();
pinOutStateMachine();
#if 0
if ((millis() % SENREAD_INTERVAL) < SENREAD_ERR) // once every 5 secs + 100 ms threshold
{
readSen();
}
else
{
// keep waiting
}
#else
#define THREE_MINS 0x59ff4c
#define TWENTY_SECS 0x9FFF6
#define TWO_SECS 0xFFFF
if (mReadSenCnt >= TWENTY_SECS )
{
mReadSenCnt = 0;
readSen();
}
else
{
mReadSenCnt++;
}
#endif
}
//---------------------------------------------------------------
// BLYNK_WRITE(VirtualPin #) - openGarageDoor
//
// Virtual Pin (button) on the blynk app triggers this event
// This initializes the output pin to toggle
// (press the garage door button)
//---------------------------------------------------------------
BLYNK_WRITE(V1)
{
String paramStr = param.asStr();
String msg = "Got a value: " + paramStr;
DBG_PRINT("BLYNK_WRITE - V1", msg);
if (param.asInt() == 0)
{
DBG_PRINT("BLYNK_WRITE - V1", "toggle the output");
mStatus = eON;
}
else
{
// do nothing, because active LOW
}
}
//---------------------------------------------------------------
// BLYNK_WRITE(VirtualPin #) - setThreshold
//
// Virtual Pin (button) on the blynk app triggers this event
// This sets the opened door threshold
//---------------------------------------------------------------
BLYNK_WRITE(V2)
{
String paramStr = param.asStr();
String msg = "Got a value: " + paramStr;
DBG_PRINT("BLYNK_WRITE - V2", msg);
if (param.asInt() == 0)
{
mSenThreshold = mSenVal + SEN_THRESH_ERR;
writeEE(EEPROM_ADDR, mSenThreshold);
String thresh = "set the sensor output as the open Threshold as < " +
String(microsecondsToCentimeters(mSenThreshold)) + " cm " ;
DBG_PRINT("BLYNK_WRITE - V2", thresh);
mTerminal.println(":: Set threshold to " + String(microsecondsToCentimeters(mSenThreshold)) + " cm ") ;
mTerminal.flush();
}
else
{
// do nothing, because active LOW
}
}
//---------------------------------------------------------------
// BLYNK_WRITE(VirtualPin #) - getInfo
//
// Virtual Pin (button) on the blynk app triggers this event
// This pushes a status to the app
//---------------------------------------------------------------
BLYNK_WRITE(V3)
{
String paramStr = param.asStr();
String msg = "Got a value: " + paramStr;
DBG_PRINT("BLYNK_WRITE - V3", msg);
if (param.asInt() == 0)
{
String info = "Sen: "
+ String(microsecondsToCentimeters(mSenVal))
+ "/"
+ String(microsecondsToCentimeters(mSenThreshold))
+ " cm. Door is " +
getString(mDoorStatus);
DBG_PRINT("BLYNK_WRITE - V3", info);
mTerminal.println(":: " + info);
mTerminal.flush();
}
else
{
// do nothing, because active LOW
}
}
//---------------------------------------------------------------
// pinOutStateMachine
//
// simple state machine to toggle the output pin without
// interrupting the code too much
//---------------------------------------------------------------
void pinOutStateMachine()
{
switch (mStatus)
{
case eON:
{
mTargetTime = millis() + TOGGLE_DURATION;
digitalWrite(LED_PIN, HIGH);
mTerminal.println(">> Pressing Garage Door Button ");
mTerminal.flush();
mStatus = eWAIT_ON;
break;
}
case eWAIT_ON:
{
if ( millis() > mTargetTime)
{
mStatus = eOFF;
}
else
{
// keep waiting
}
break;
}
case eOFF:
{
digitalWrite(LED_PIN, LOW);
mTerminal.println(">> Release Garage Door Button");
mTerminal.flush();
mStatus = eUNINIT;
break;
}
case eUNINIT:
// intentionally do nothing
default:
// do nothing
break;
}
}
//---------------------------------------------------------------
// readSen
//
// read the ultraSonicSensor in cm
//---------------------------------------------------------------
void readSen()
{
// reset it // TODO why is this needed?
mSenVal = 0;
// low for 2us, high for 5us, back to low
pinMode(US_PIN, OUTPUT);
digitalWrite(US_PIN, LOW);
delayMicroseconds(2);
digitalWrite(US_PIN, HIGH);
delayMicroseconds(4); // its supposed to be 5, but the scoope syas 5 --> 6.6 us, and 4--> 5.2us
digitalWrite(US_PIN, LOW);
// the same pin is used for feedback
pinMode(US_PIN, INPUT);
mSenVal = pulseIn(US_PIN, HIGH, PULSE_TIMEOUT);
if (mSenVal == 0)
{
String str = getUpTime() + ") timeout " + String(microsecondsToCentimeters(mSenVal)) + " cm" ;
DBG_PRINT("readSen", str);
mTerminal.println(">> " + str );
}
else
{
String str = getUpTime() + ") sensor = " + String(microsecondsToCentimeters(mSenVal)) + " cm";
DBG_PRINT("readSen" , str );
mTerminal.println(">> " + str );
eDOORSTATUS doorStatus = eDOOR_UNKNOWN;
// check if the door is closed
if (mSenVal < mSenThreshold )
{
doorStatus = eDOOR_OPEN;
}
else
{
// nothing to see here. continue
doorStatus = eDOOR_CLOSED;
}
if (doorStatus != mDoorStatus)
{
String msg = String( getUpTime() + ") !!! " + getString(mDoorStatus) + " ---> " + getString(doorStatus) + " !!!" );
DBG_PRINT("readSen", msg );
if (doorStatusHyst == true)
{
// send event to the app only if the status changed
DBG_PRINT("readSen", "hysteresis is true, send the notification");
mTerminal.println(">> " + str + ". Send notification" );
mDoorStatus = doorStatus;
Blynk.virtualWrite(V5, getString(mDoorStatus) );
Blynk.notify(msg);
}
else
{
DBG_PRINT("readSen", "hysteresis is false, do one more check");
mTerminal.println(">> " + str + ". hysteresis check" );
doorStatusHyst = true;
}
}
else
{
//status is the same. reset the hysteresis if it happened to be triggered
doorStatusHyst = false;
}
}
mTerminal.flush();
}
//---------------------------------------------------------------
// getUpTime
// return a string of the hh:mm:ss of the uptime
//---------------------------------------------------------------
String getUpTime()
{
UInt32 ms = millis();
UInt32 secs = ( ms/ 1000 ) % 60;
UInt32 mins = ( ms/(1000*60) ) % 60;
UInt32 hours =( ms/ (1000*60*60) ) %60;
String upTime = String(hours) + ":" + String(mins) + ":" + String(secs) ;
return upTime;
}
//---------------------------------------------------------------
// microsecondsToCentimeters
// read the ultraSonicSensor in cm
//---------------------------------------------------------------
unsigned long microsecondsToCentimeters(unsigned long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
//---------------------------------------------------------------
// readEE
// read a UInt32 from the EEPROM at address addr
//---------------------------------------------------------------
UInt32 readEE(Int32 addr)
{
UInt32 val = 0;
EEPROM.begin(512);
EEPROM.get(addr, val);
EEPROM.end();
return val;
}
//---------------------------------------------------------------
// writeEE
// write the val UInt32 to the EEPROM at address addr
//---------------------------------------------------------------
void writeEE(Int32 addr, UInt32 val)
{
EEPROM.begin(512);
EEPROM.put(addr, val);
EEPROM.commit();
EEPROM.end();
}
//---------------------------------------------------------------
// getString
// get the door status string
//---------------------------------------------------------------
String getString(const eDOORSTATUS stat)
{
String retStr = "?";
switch(stat)
{
case eDOOR_UNKNOWN:
retStr = "UNKNOWN";
break;
case eDOOR_OPEN:
retStr = "OPEN";
break;
case eDOOR_CLOSED:
retStr = "CLOSED";
break;
default:
retStr = "??????";
break;
}
return retStr;
}
//---------------------------------------------------------------
// debug_print
//
// easy way to enable/disable debug prints.
// change the macro at the top of the file
//---------------------------------------------------------------
void debug_print(String fn, String msg)
{
Serial.println(fn + ": " + msg);
}