-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
171 lines (134 loc) · 4.5 KB
/
main.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
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
#include <Arduino.h>
#include <Homie.h>
#include <LightDependentResistor.h>
// Adjust these two based on your HW
#define PHOTOCELL_TYPE LightDependentResistor::GL5528
#define PHOTOCELL_RESISTOR 15000 /*ohm*/
//#define PHOTOCELL_RESISTOR 21700 /*ohm*/
#define LED_PIN LED_BUILTIN
#define PIR_PIN D5
#define PHOTOCELL_PIN A0
#define PHOTOCELL_TOLERANCE 15 /* % */
#define PHOTOCELL_READING_INTERVAL 100 /* ms */
int brightness = PWMRANGE;
bool led_on = false;
unsigned long photocell_report_interval = 60000; //ms
unsigned long photocell_report_interval_start = 0;
unsigned long photocell_reading_interval_start = 0;
float photocell_last_value = 0;
bool motion_detected = false;
LightDependentResistor photocell(PHOTOCELL_PIN, PHOTOCELL_RESISTOR, PHOTOCELL_TYPE );
HomieNode ledNode("led", "switch");
HomieNode pirNode("pir", "sensor");
HomieNode photocellNode("photocell", "sensor");
void updateLed() {
if (brightness == 0) {
led_on = false;
}
if (led_on) {
analogWrite(LED_PIN, PWMRANGE-brightness);
} else {
analogWrite(LED_PIN, PWMRANGE);
}
ledNode.setProperty("brightness").send(String(brightness));
ledNode.setProperty("status").send(led_on ? "ON" : "OFF");
}
bool ledBrightnessHandler(const HomieRange& range, const String& value) {
brightness = value.toInt();
if (brightness > PWMRANGE) { brightness = PWMRANGE; }
if (brightness < 0) { brightness = 0; }
updateLed();
return true;
}
bool ledStatusHandler(const HomieRange& range, const String& value) {
if ((value != "ON") && (value != "OFF")) return false;
if (value == "ON") {
led_on = true;
} else {
led_on = false;
}
updateLed();
return true;
}
bool photocellIntervalHandler(const HomieRange& range, const String& value) {
int v = value.toInt();
if (v != 0) {
photocell_report_interval = v * 1000;
photocellNode.setProperty("interval").send(String(v));
}
return true;
}
void photocellLoopHandler() {
unsigned long now = millis();
if ((now - photocell_reading_interval_start) >= PHOTOCELL_READING_INTERVAL) {
float lux = photocell.getCurrentLux();
int raw = analogRead(PHOTOCELL_PIN);
float luxdiff = (lux - photocell_last_value);
float pdiff = 100 * (luxdiff / photocell_last_value);
bool quick_update = false;
if ((abs(luxdiff) > 1.0) and (abs(pdiff) > PHOTOCELL_TOLERANCE)) {
quick_update = true;
}
/*
Serial.print("Lux: ");
Serial.print(lux);
Serial.print(" (");
Serial.print(pdiff);
Serial.println(" %)");
*/
if (quick_update) {
photocellNode.setProperty("lux").send(String(photocell_last_value));
}
if (((now - photocell_report_interval_start) >= photocell_report_interval) or quick_update) {
photocellNode.setProperty("lux").send(String(lux));
photocellNode.setProperty("raw").send(String(raw));
photocell_last_value = lux;
photocell_report_interval_start = now;
}
photocell_reading_interval_start = now;
}
}
void pirLoopHandler() {
if (motion_detected != (digitalRead(PIR_PIN))) {
motion_detected = digitalRead(PIR_PIN);
pirNode.setProperty("motion").send(
motion_detected ? "YES" : "NO"
);
}
}
void loopHandler() {
photocellLoopHandler();
pirLoopHandler();
}
void setupHandler() {
ledNode.setProperty("brightness").send(String(brightness));
ledNode.setProperty("status").send("OFF");
pirNode.setProperty("motion").send("NO");
photocell_report_interval_start = millis();
photocell_reading_interval_start = millis();
photocell_last_value = photocell.getCurrentLux();
photocellNode.setProperty("interval").send(String(photocell_report_interval/1000));
photocellNode.setProperty("lux").send(String(photocell_last_value));
photocellNode.setProperty("raw").send(String(analogRead(PHOTOCELL_PIN)));
}
void setup() {
Serial.begin(115200);
Serial << endl << endl << "[PIR_sensor]" << endl;
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
photocell.setPhotocellPositionOnGround(false);
Homie.setLedPin(LED_PIN, LOW);
Homie_setFirmware("pir_sensor", "0.1.0");
Homie.setSetupFunction(setupHandler);
Homie.setLoopFunction(loopHandler);
ledNode.advertise("brightness").settable(ledBrightnessHandler);
ledNode.advertise("status").settable(ledStatusHandler);
pirNode.advertise("motion");
//photocellNode.advertise("lux");
photocellNode.advertise("raw");
photocellNode.advertise("interval").settable(photocellIntervalHandler);
Homie.setup();
}
void loop() {
Homie.loop();
}