/
Multisensor_PIR_DHT_LDA_Battery.ino
234 lines (201 loc) · 5.84 KB
/
Multisensor_PIR_DHT_LDA_Battery.ino
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
#include <MySensor.h>
#include <readVcc.h>
#include <SPI.h>
#include <DHT.h>
#define NODE_ID 26 // ID of node
#define SLEEP_TIME 1800000 // Sleep time between reports (in milliseconds)
// 1h = 3600000
// 30m = 1800000
// 15m = 900000
#define CHILD_ID_PIR 1 // ID of the sensor PIR
#define CHILD_ID_HUM 2 // ID of the sensor HUM
#define CHILD_ID_TEMP 3 // ID of the sensor TEMP
#define CHILD_ID_LIGHT 4 // ID of the sensor LIGHT
#define PIR_SENSOR_DIGITAL 3 // PIR pin
#define HUMIDITY_SENSOR_DIGITAL_PIN 4 // DHT pin
#define LIGHT_SENSOR_ANALOG_PIN 0 // LDR pin
#define LED_PIN A1 // LED connected PIN
#define STEPUP_PIN 6 // StepUp Transistor
#define STEPUP_PIN2 7 // StepUp Transistor
#define MIN_V 1900 // empty voltage (0%)
#define MAX_V 3200 // full voltage (100%)
MySensor gw;
// Initialize Variables
MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
DHT dht;
float lastTemp;
float lastHum;
boolean metric = true;
int oldBatteryPcnt;
int lastLightLevel;
int sentValue;
int repeatSending = 10;
void setup()
{
stepup(true);
gw.begin(NULL, NODE_ID, false);
gw.sendSketchInfo("PIR,DHT,Light", "1.0");
// Register all sensors to gateway (they will be created as child devices)
gw.present(CHILD_ID_PIR, S_MOTION);
gw.present(CHILD_ID_HUM, S_HUM);
gw.present(CHILD_ID_TEMP, S_TEMP);
gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
// PIR PINS
pinMode(PIR_SENSOR_DIGITAL, INPUT);
//digitalWrite(PIR_SENSOR_DIGITAL, HIGH); //Use this if no external pullup is used for PIR
//DHT PINS
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
//STEPUP PINS
pinMode(STEPUP_PIN, OUTPUT); // sets the pin as output
pinMode(STEPUP_PIN2, OUTPUT); // sets the pin as output
//STARTUP BLINK
led(true,5,200);
stepup(false);
}
void loop()
{
Serial.println("Waking up...");
stepup(true);
sendPir();
sendLight();
sendTemp();
sendHum();
sendBattery();
stepup(false);
Serial.println("Going to sleep...");
Serial.println("");
gw.sleep(PIR_SENSOR_DIGITAL - 2, RISING, SLEEP_TIME);
}
void sendBattery() // Measure battery
{
int batteryPcnt = min(map(readVcc(), MIN_V, MAX_V, 0, 100), 100);
if (batteryPcnt != oldBatteryPcnt) {
gw.sendBatteryLevel(batteryPcnt); // Send battery percentage
oldBatteryPcnt = batteryPcnt;
}
Serial.print("---------- Battery: ");
Serial.println(batteryPcnt);
}
void sendPir() // Get value of PIR
{
int value = digitalRead(PIR_SENSOR_DIGITAL); // Get value of PIR
if (value != sentValue) { // If status of PIR has changed
//gw.send(msgPir.set(value == HIGH ? 1 : 0)); // Send PIR status to gateway
resend((msgPir.set(value == HIGH ? 1 : 0)),repeatSending);
sentValue = value;
}
Serial.print("---------- PIR: ");
Serial.println(value ? "tripped" : "not tripped");
}
void sendTemp() // Get temperature
{
delay(dht.getMinimumSamplingPeriod()); // Use the delay if DHT cant read
float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
} else {
if (temperature != lastTemp) {
//gw.send(msgTemp.set(temperature, 1));
resend((msgTemp.set(temperature, 1)),repeatSending);
lastTemp = temperature;
}
Serial.print("---------- Temp: ");
Serial.println(temperature);
}
}
void sendHum() // Get humidity
{
//delay(dht.getMinimumSamplingPeriod()); // Use the delay if DHT cant read
float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
} else {
if (humidity != lastHum) {
//gw.send(msgHum.set(humidity, 1));
resend((msgHum.set(humidity, 1)),repeatSending);
lastHum = humidity;
}
Serial.print("---------- Humidity: ");
Serial.println(humidity);
}
}
void sendLight() // Get light level
{
int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
if (lightLevel != lastLightLevel) {
//gw.send(msgLight.set(lightLevel));
resend((msgLight.set(lightLevel)),repeatSending);
lastLightLevel = lightLevel;
}
Serial.print("---------- Light: ");
Serial.println(lightLevel);
}
void led(boolean onoff, int blink, int time) // LED Signal
{
pinMode(LED_PIN, OUTPUT); // sets the pin as output
Serial.print("---------- LED: ");
if (blink == 0)
{
if (onoff == true)
{
Serial.println("ON");
digitalWrite(LED_PIN, LOW); // turn on
}
else
{
Serial.println("OFF");
digitalWrite(LED_PIN, HIGH); // turn off
}
}
else
{
if (time == 0) {time = 100;}
Serial.print("Blink ");
Serial.print(blink);
Serial.print(" Delay ");
Serial.println(time);
for (int count = 0; count < blink; count++)
{
digitalWrite(LED_PIN, LOW); // turn on
delay(time);
digitalWrite(LED_PIN, HIGH); // turn off
delay(time);
}
}
}
void resend(MyMessage &msg, int repeats)
{
int repeat = 1;
int repeatdelay = 0;
boolean sendOK = false;
while ((sendOK == false) and (repeat < repeats)) {
if (gw.send(msg)) {
sendOK = true;
} else {
sendOK = false;
Serial.print("Send ERROR ");
Serial.println(repeat);
repeatdelay += 250;
led(true,1,5);
} repeat++; delay(repeatdelay);
}
}
void stepup(boolean onoff)
{
Serial.print("---------- StepUp: ");
if (onoff == true)
{
Serial.println("ON");
digitalWrite(STEPUP_PIN, HIGH); // turn on
digitalWrite(STEPUP_PIN2, LOW); // turn on
}
else
{
Serial.println("OFF");
digitalWrite(STEPUP_PIN, LOW); // turn off
digitalWrite(STEPUP_PIN2, HIGH); // turn off
}
}