-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionSensor.cpp
54 lines (42 loc) · 997 Bytes
/
MotionSensor.cpp
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
#include "MotionSensor.h"
#include <HttpClient.h>
MotionSensor::MotionSensor(int sensor, int id, int redLED, int greenLED, String path) {
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
MotionSensor::id = id;
_sensor = sensor;
_status = 0;
_path = path;
_notified = false;
_red = redLED;
_green = greenLED;
}
void MotionSensor::pulse() {
if (hasChanged()) {
if (_status == HIGH) {
notify();
} else {
_notified = false;
_timeSet = millis();
}
}
if (!_notified && (millis() - _timeSet > 1000*waitTime)) {
notify();
}
}
void MotionSensor::notify() {
_notified = true;
if (_lastNotification == _status) {
return;
}
_lastNotification = _status;
HttpClient client;
client.get(_path + "/" + id + "/" + !_status);
digitalWrite(_green, !_status);
digitalWrite(_red, _status);
}
bool MotionSensor::hasChanged() {
int lastStatus = _status;
_status = digitalRead(_sensor);
return (_status != lastStatus);
}