-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmoveir.cpp
97 lines (84 loc) · 2.64 KB
/
moveir.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Movement sensors (IR and X-band radio)
#include "moveir.h"
#include "mqtt-support.h"
// publish PNP information to MQTT
void pnp_moveir() {
pnp_mqtt(TOPIC_IR_SENSE, "IR sensor", "Sensors Movement IR", "O:Number", "0", "100");
pnp_mqtt(TOPIC_RADIO_SENSE, "Radio sensor", "Sensors Movement Radio", "O:Number", "0", "100");
pnp_mqtt(TOPIC_ADDITIONAL, "Additional IR sensor", "Sensors Movement IR", "O:Number", "0", "100");
} // void pnp_moveir()
// subscribe to MQTT topics
void subscribe_moveir() {
} // void subscribe_moveir()
// Vars
int FlagIrSense = 0;
int FlagRadioSense = 0;
int FlagAddSense = 0;
int lastIrSense = 0;
int lastRadioSense = 0;
int lastAddSense = 0;
// Last states (to not report same value too often)
int lastRadioSenseState = -1;
int lastIrSenseState = -1;
int lastAddIrSenseState = -1;
// Setup hardware (pins, etc)
void setup_moveir() {
pinMode(IR_SENSE, INPUT);
pinMode(RADIO_SENSE, INPUT);
pinMode(ADD_SENSE, INPUT);
} // void setup_moveir()
// must be called from loop()
void loop_moveir() {
unsigned long startDelay = millis();
// Check sensors for 9ms
while ((millis() - startDelay) < 9) {
if (digitalRead(IR_SENSE) != lastIrSense) {
FlagIrSense++;
lastIrSense = digitalRead(IR_SENSE);
}
if (digitalRead(RADIO_SENSE) != lastRadioSense) {
FlagRadioSense++;
lastRadioSense = digitalRead(RADIO_SENSE);
}
if (digitalRead(ADD_SENSE) != lastAddSense) {
FlagAddSense++;
lastAddSense = digitalRead(ADD_SENSE);
}
}
} // void loop_moveir()
// periodic refreshing values to MQTT
void refresh_moveir(boolean flagForce) {
int tmp;
// Publish statuses of IR/RADIO sensors
tmp = (FlagIrSense > 100) ? 100 : FlagIrSense;
if (flagForce || (lastIrSenseState != tmp)) {
#ifdef DEBUG
Serial.println("Reporting changed IR status");
#endif
lastIrSenseState = tmp;
publish_mqttI(TOPIC_IR_SENSE, tmp);
}
tmp = (FlagRadioSense > 100) ? 100 : FlagRadioSense;
if (flagForce || (lastRadioSenseState != tmp)) {
#ifdef DEBUG
Serial.println("Reporting changed RADIO status");
#endif
lastRadioSenseState = tmp;
publish_mqttI(TOPIC_RADIO_SENSE, tmp);
}
tmp = (FlagAddSense > 100) ? 100 : FlagAddSense;
if (flagForce || (lastAddIrSenseState != tmp)) {
#ifdef DEBUG
Serial.println("Reporting changed ADDIR status");
#endif
lastAddIrSenseState = tmp;
publish_mqttI(TOPIC_ADDITIONAL, tmp);
}
FlagRadioSense = 0;
FlagIrSense = 0;
FlagAddSense = 0;
} // void refresh_moveir(boolean flagForce)
// hook for incoming MQTT messages
boolean mqtt_moveir(char *topicCut, char *payload) {
return false;
} // boolean mqtt_moveir(char *topicCut, char *payload)