-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP_SHD_MotionSensor.cpp
82 lines (70 loc) · 3.24 KB
/
ESP_SHD_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
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
#include "ESP_SHD_MotionSensor.h"
#include <FunctionalInterrupt.h>
ShdMotionSensor::ShdMotionSensor(uint8_t _pin){
// prepare pin and attach interrupt:
pin = _pin;
pinMode(pin, INPUT);
attachInterrupt(pin, std::bind(&ShdMotionSensor::pinChange, this), CHANGE);
// initialize variables
motionDeteced = false;
motionSensorStatus = false;
snprintf (pubTopic, 50, "%s/Motion", name);
// debug output:
Serial.print("New motion sensor registered. It publishes to ");
Serial.println(pubTopic);
Serial.println();
}
bool ShdMotionSensor::handleMqttRequest(char* _topic, unsigned char* _payload, uint16_t _length){
return false;
}
void ShdMotionSensor::timer5msHandler(){
// test, if mqtt client is connected
if (!mqttConnected()) {
return;
}
if (motionSensorStatus && !motionDeteced) {
#if DEBUG > 2
Serial.print("Motion detected. ");
#endif
if (mqttPublish(pubTopic, "true")) {
#if DEBUG > 2
Serial.print("Published via MQTT. ");
#endif
} else {
#if DEBUG > 2
Serial.print("Could not publish via MQTT. ");
#endif
}
#if DEBUG > 2
Serial.println();
#endif
} else if (!motionSensorStatus && motionDeteced) {
#if DEBUG > 2
Serial.print("No motion detected. ");
#endif
if (mqttPublish(pubTopic, "false")) {
#if DEBUG > 2
Serial.print("Published via MQTT. ");
#endif
} else {
#if DEBUG > 2
Serial.print("Could not publish via MQTT. ");
#endif
}
#if DEBUG > 2
Serial.println();
#endif
}
motionDeteced = motionSensorStatus;
return;
}
void ShdMotionSensor::pinChange(){
motionSensorStatus = digitalRead(pin);
}
void ShdMotionSensor::republish() {
if (motionSensorStatus) {
mqttPublish(pubTopic, "true");
} else {
mqttPublish(pubTopic, "false");
}
}