-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf_led_recieve.ino
387 lines (283 loc) · 7.16 KB
/
rf_led_recieve.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <VirtualWire.h>
#include "DHT.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display TWI address
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(-1);
#define DHTPIN 7 // what digital pin we're connected to
const int RF_RX_PIN = 11;
int relayFanPin = 10; //attach relay to pin D4
int ledPin = 5; //attach relay to pin D5
int errorLED = 13;
int pirPin = 2;
int relayLampPin = 3;
int relayLockPin = 4;
float count3 = 120;
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
boolean led = 1;
boolean relayFan = 1;
boolean relayLamp = 1;
boolean relayLock = 1;
boolean ledState = true;
uint8_t buf [VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
float lock_close = millis();
boolean lock_flag = false;
void setup() {
led = 1;
relayFan = 1;
relayLamp = 1;
relayLock = 1;
ledState = true;
// put your setup code here, to run once:
dht.begin();
vw_set_ptt_inverted(true);
vw_set_tx_pin(RF_RX_PIN);
vw_setup(2000);
vw_rx_start();
pinMode(relayFanPin, OUTPUT);
pinMode(relayLampPin, OUTPUT);
pinMode(relayLockPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(errorLED, OUTPUT);
digitalWrite(relayFanPin, LOW);
digitalWrite(relayLampPin, LOW);
digitalWrite(relayLockPin, LOW);
digitalWrite(ledPin, LOW);
// relay = false;
//led = false;
Serial.begin(9600);
// initialize and clear display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
// display a line of text
display.setTextSize(1.8);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("RF Automation");
// update display with all of the above graphics
display.display();
//attach interrupt for the various pins
attachInterrupt(digitalPinToInterrupt(pirPin), Alert_PIR, RISING);
}
void Alert_PIR(){
Serial.println("Motion Detected in PIR1");
Serial.println("Sending SMS Alert");
display.setCursor(0,40);
display.print("Motion Detected");
}
void loop() {
display.setCursor(0,0);
display.print("RF Automation");
// put your main code here, to run repeatedly:
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
display.setCursor(0,10);
display.print("Temp: ");
display.print(t);
display.print(" C");
display.setCursor(0,20);
display.print("Humidity: ");
display.print(h);
display.print("%");
display.display();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
if(vw_get_message(buf, &buflen)) {
delay(500);
Serial.println((char *) buf);
payloadDecode ( (char *) buf);
delay(500);
}
if(led == true) {
digitalWrite(ledPin, led);
}else{
ledState = !ledState;
Serial.println(led);
digitalWrite(ledPin, led);
display.setCursor(0,30);
display.print("LED ON");
display.display();
display.clearDisplay();
// display.setCursor(0,30);
// display.print("LED OFF");
// display.display();
// display.clearDisplay();
}
if(relayFan == true){
Serial.println(relayFan);
digitalWrite(relayFanPin, relayFan);
//display.clearDisplay();
}else{
Serial.println(relayFan);
digitalWrite(relayFanPin, relayFan);
display.setCursor(0,30);
display.print("FAN ON");
display.display();
// display.setCursor(0,30);
//display.print("FAN OFF");
// display.display();
}
if(relayLamp == true){
Serial.println(relayLamp);
digitalWrite(relayLampPin, relayLamp);
//display.clearDisplay();
}else{
Serial.println(relayLamp);
digitalWrite(relayLampPin, relayLamp);
display.setCursor(0,30);
display.print("AC LAMP ON");
display.display();
// display.setCursor(0,30);
//display.print("AC LAMP OFF");
//display.display();
//display.clearDisplay();
}
if(relayLock == true){
count3 = 120;
Serial.println(relayLock);
digitalWrite(relayLockPin, relayLock);
//display.setCursor(0,30);
// display.print("DOOR LOCKED");
// display.display();
//display.clearDisplay();
}else{
Serial.println(relayLock);
digitalWrite(relayLockPin, relayLock);
display.setCursor(0,30);
display.print("DOOR UNLOCKED");
display.display();
//display.clearDisplay();
Serial.println(lock_close);
//close lock after some minutes
Serial.println(millis() - lock_close);
if (millis() - lock_close > 120000){
// Serial.println(millis() - lock_close);
lock_flag = true;
lock_close = millis();
}
count3 -= 1;
display.setCursor(0,45);
display.print("DOOR LOCKING IN ");
display.print(count3);
display.print("secs" );
display.display();
}
//
//if(relayFan == true) {
// digitalWrite(relayFanPin, HIGH);
// }
//
//if(relayFan == false) {
// digitalWrite(relayFanPin, LOW);
// }
if (lock_flag){
int count1 = 0;
int count2 = 5;
while(count1 < 5){
display.setCursor(0,30);
display.print("DOOR LOCKING IN ");
display.print(count2 -= 1);
display.display();
display.clearDisplay();
count1 ++;
delay(1000);
}
relayLock = 1;
digitalWrite(relayLockPin, relayLock);
lock_flag = false;
}
display.clearDisplay();
delay(400);
}
void payloadDecode(char msg[]) {
if (strlen(msg) == 0) {
ErrorLED();
return;
}
if ( compareMessage(msg, '@') == false || compareMessage(msg, '$') == false) {
//Serial.println(msg);
ErrorLED();
return;
}
char cTag[5] ="";
int index =0;
int iLoop = IndexOf(msg,'@');
if(iLoop == -1) {
//Serial.println(msg);
ErrorLED();
return;
}
iLoop++;
while (iLoop < strlen(msg)) {
if(msg[iLoop] == '$') {
break;
} else {
cTag[index] = msg[iLoop];
// Serial.println(msg);
index++;
}
iLoop ++;
}
String sTag = String(cTag);
if (sTag.equals("ABC")) {
//Serial.println(msg);
led = !led;
}
if (sTag.equals("FON")) {
//Serial.println(msg);
relayFan = !relayFan;
}
if (sTag.equals("LCK")) {
//Serial.println(msg);
relayLock = !relayLock;
}
if (sTag.equals("LON")) {
//Serial.println(msg);
relayLamp = !relayLamp;
}
}
int IndexOf(char msg[], char tag) {
if (strlen(msg) ==0) {
return false;
}
boolean bFlag = false;
int iIndex = -1;
for( int i=0; i<strlen(msg); i++) {
if(msg[i]== tag)
iIndex = i;
}
return iIndex;
}
boolean compareMessage (char msg[], char tag) {
if (strlen(msg) ==0){
return false;
}
boolean bFlag = false;
//Serial.println(msg);
for( int i=0; i<strlen(msg); i++) {
if(msg[i]== tag)
bFlag = true;
}
return bFlag;
}
void ErrorLED() {
digitalWrite(errorLED, HIGH);
delay(500);
digitalWrite(errorLED, LOW);
}