-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotionSensor.ino
More file actions
501 lines (461 loc) · 13.8 KB
/
MotionSensor.ino
File metadata and controls
501 lines (461 loc) · 13.8 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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial wireless(4, 5); // RX, TX
long unsigned int shutdowntimer;
bool flag, blinkpowerstate;
String options[6];
class Comms
// This class requires a software serial connection called "wireless" to be declared
{
public:
String devicename;
String incomingmessage;
Comms(String deviceName)
{
devicename = deviceName;
}
// begins the serial connection to the wireless module
void begin(int baudrate)
{
wireless.begin(baudrate);
}
// listens on the serial port, use inside of a loop
void listen()
{
if (wireless.available())
{
record();
respond();
}
}
// broadcasts a message (no ack)
void broadcast(String message)
{
// sends the message
wireless.print(char(10));
wireless.print(devicename);
wireless.print(" ");
wireless.print(message);
wireless.print(char(13));
}
// sends a message (expects ack)
void send(String target, String command)
{
// check if target is itself
if (target == devicename)
{
// bounce it back to inbox and not send it
setmessage(devicename + " " + target + " " + command);
return;
}
// else, send the message
for (int i = 0; i < 3; i++)
{
// sends the message
wireless.print(char(10));
wireless.print(devicename);
wireless.print(" ");
wireless.print(target);
wireless.print(" ");
wireless.print(command);
wireless.print(char(13));
// after that, listen for ack
if (waitforack())
{
return;
}
}
setmessage("! ack timeout");
return;
}
// announces itself and ask other devices to register itself
void announce()
{
// <source> announce // asks nearby devices to respond with their names
wireless.print(char(10) + devicename + " announce" + char(13));
}
void atinit(bool atstate)
{
digitalWrite(6, atstate);
}
void setmessage(String txt)
{
message += char(10) + txt + char(13);
}
void nextmessage()
{
String txt = "";
bool issecondmessage = false;
for (int i = 0; i < message.length(); i++)
{
if (issecondmessage)
{
txt += message.charAt(i);
}
if (message.charAt(i) == 13)
{
issecondmessage = true;
}
}
message = txt;
}
bool newmsgavailable()
{
if (newmessage)
{
newmessage = false;
return true;
}
else
{
return false;
}
}
bool available()
{
return message.length() != 0;
}
// returns the first segment and clears the available state
String read()
{
String txt = "";
for (int i = 0; i < message.length(); i++)
{
switch (int(message.charAt(i)))
{
case 10:
break;
case 13:
return txt;
default:
txt += message.charAt(i);
break;
}
}
}
// returns the argument of requestedItem
// e.g. txt = "hello world", requestedItem = 1, returning string would be "world"
String pharse(String txt, int requestedItem)
{
String response = "";
int currentItem = 0;
bool inquotes = false;
// go through the length of the text, one at a time
for (int i = 0; i < txt.length(); i++)
{
if (txt.charAt(i) == ' ' && !inquotes)
// checks if it is a space and not in a quote
{
// if so, increment item count
currentItem++;
// checks if the last item is the one that's requested
if (currentItem > requestedItem)
{
// if so, return the recorded string
return response;
}
// if not, then clear buffer and start recording the next segment
response = "";
}
else if (txt.charAt(i) == '"')
// check if it is a quotemark
{
// if so, toggle inquotes
inquotes = !inquotes;
}
else
{
// if not, just record it into buffer for future use
response += String(txt.charAt(i));
}
}
// fallback check for if the requested item is the last item.
// in this case, the above mechanism would not trigger
// because there isn't a space at the end of transmission
if (currentItem == requestedItem)
{
return response;
}
return "-1";
}
private:
char incomingchar;
String message;
bool newmessage;
bool waitforack()
{
long unsigned int acktimeout = millis();
while (millis() - acktimeout < 1000)
{
if (wireless.available())
{
if (wireless.read() == char(6))
{
return true;
}
}
}
return false;
}
// records incoming messages
void record()
{
long unsigned int timer = millis();
while (true)
{
// wait until a byte comes in
while (!wireless.available())
{
// check if it's timed out
if (millis() - timer > 1000)
{
// timed out
incomingchar = 0;
incomingmessage = "";
setmessage("! incoming timeout");
return;
}
}
// reset timeout timer
timer = millis();
// reads the incoming byte
incomingchar = wireless.read();
// detect end of transmission
// filters control bytes, 10 starts message, 13 ends message
switch (incomingchar)
{
case 6:
// ack byte
break;
case 10:
// transmission starts, clear buffer
incomingmessage = "";
break;
case 13:
// transmission ends
setmessage(incomingmessage);
newmessage = true;
return;
default:
incomingmessage += incomingchar;
break;
}
}
}
void respond()
{
// check if target of the message is this machine
if (pharse(incomingmessage, 1) == devicename)
{
// ack syntax: 6
// do not use send(), otherwise there will be a broadcast catastrophe of acks in the air,
// and the NSA would come knocking at your door "knack" "knack"
wireless.print(char(6));
}
// check if message is a brodacasted query for available devices
// <source> announce // asks nearby devices to respond with their names
if (pharse(incomingmessage, 1) == "announce")
{
// wait until other devices finish transmitting
while (wireless.available())
{
record();
respond();
}
// <source> <target> online // tells target (everyone, actually) that device is online
send(pharse(incomingmessage, 0), "online");
}
}
};
Comms comms("motion");
void setup()
{
comms.begin(9600);
pinMode(6, INPUT_PULLUP); // sensor pin
pinMode(7, OUTPUT); // blink power pin
Timer1.initialize(5000000);
Timer1.attachInterrupt(blinkpower);
Serial.begin(9600);
comms.broadcast("init");
}
void loop()
{
// listen to commands
comms.listen();
if (comms.available())
{
commandtree(comms.read());
comms.nextmessage();
}
// detect sensor readings
if (digitalRead(6) && !flag)
{
flag = true;
comms.broadcast("triggered");
digitalWrite(13, HIGH);
}
if (!digitalRead(6) && flag)
{
flag = false;
comms.broadcast("untriggered");
digitalWrite(13, LOW);
}
}
void remotemenutree(String target)
// tree location not zero-indexed, 0 reserved for "return"
{
options[0] = F("Firmware Info");
options[1] = F("Shutdown");
switch (remotemenu(2, comms.devicename, options, target))
{
case 1:
comms.broadcast("v0.4");
comms.broadcast(__DATE__);
break;
case 2:
if (shutdowntimer == 0)
{
int shutdowntime = remoteselnum(F("Schedule Shutdown"), 0, 60, -1, target);
if (shutdowntime != -1)
{
shutdowntimer = 60000 * shutdowntime + millis();
comms.broadcast(F("shutdown scheduled"));
}
}
else
{
shutdowntimer = 0;
comms.broadcast(F("shutdown cancelled"));
}
break;
}
}
int remoteselnum(String title, int mini, int maxi, int defaultint, String target)
{
// <source> <target> set <index> <value>
comms.send(target, "set 0 \"" + String(title) + "\"");
comms.send(target, "set 1 " + String(mini));
comms.send(target, "set 2 " + String(maxi));
comms.send(target, "set 3 " + String(defaultint));
// <source> <target> selnum
comms.send(target, "selnum");
// listen for response
long unsigned int timeout = millis() + 30000;
while (timeout > millis())
{
comms.listen();
if (comms.newmsgavailable())
{
// checks if source of message is target and target is devicename
if (comms.pharse(comms.incomingmessage, 0) == target && comms.pharse(comms.incomingmessage, 1) == comms.devicename)
{
// <source> <target> respond <value>
if (comms.pharse(comms.incomingmessage, 2) == "respond")
{
return comms.pharse(comms.incomingmessage, 3).toInt();
}
else
{
return -1;
}
}
}
}
return -1;
}
int remotemenu(int optcount, String title, String opts[], String target)
{
// trigger the menu
// send the options
for (int i = 0; i < optcount; i++)
{
// <source> <target> set <index> "<content>"
comms.send(target, "set " + String(i) + " \"" + opts[i] + "\"");
}
// trigger with <source> <target> menu <optcount> "<title>"
comms.send(target, "menu " + String(optcount) + " \"" + title + "\"");
// listen for response
long unsigned int timeout = millis() + 30000;
while (timeout > millis())
{
comms.listen();
if (comms.newmsgavailable())
{
// checks if source of message is target and of target is devicename
if (comms.pharse(comms.incomingmessage, 0) == target && comms.pharse(comms.incomingmessage, 1) == comms.devicename)
{
// <source> <target> respond <value>
if (comms.pharse(comms.incomingmessage, 2) == "respond")
{
return comms.pharse(comms.incomingmessage, 3).toInt();
}
}
}
}
return -1;
}
void commandtree(String command)
{
// checks target of command
if (comms.pharse(command, 1) == comms.devicename)
// command directed at this device
{
// checks command arguments
if (comms.pharse(command, 2) == "ping")
// <source> <target> ping // respond with "pong"
{
comms.send(comms.pharse(command, 0), "pong");
return;
}
else if (comms.pharse(command, 2) == "pong")
// <source> <target> pong // response of "ping", do nothing
{
return;
}
else if (comms.pharse(command, 2) == "negative")
// <source> <target> negative // do nothing
{
if (comms.pharse(command, 3) == "invalid")
// <source> <target> negative invalid // do nothing
{
return;
}
}
else if (comms.pharse(command, 2) == "ack")
// <source> <target> ack // do nothing
{
return;
}
else if (comms.pharse(command, 2) == "respond")
// <source> <target> respond <value> // do nothing
{
return;
}
else if (comms.pharse(command, 2) == "query")
// <source> <target> query // trigger remotemenutree
{
remotemenutree(comms.pharse(command, 0));
return;
}
else
// no command matches // respond with "negative invalid"
{
comms.send(comms.pharse(command, 0), "negative invalid");
return;
}
}
}
void blinkpower()
{
// change the state of the power pin to keep the chip awake
if (shutdowntimer != 0 && millis() > shutdowntimer)
{
digitalWrite(7, LOW);
comms.broadcast("shutdown initiated");
}
else
{
blinkpowerstate = !blinkpowerstate;
digitalWrite(7, blinkpowerstate);
}
}