-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartHome-logicII2022.ino
682 lines (621 loc) · 17.1 KB
/
SmartHome-logicII2022.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include <Servo.h>
#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <dht.h>
#include <ezButton.h>
//define bluetooth module
int devicebluetooth = 52;
int setpoint1 = 40;
//*************************************
//initialising limit switch
ezButton limitSwitch(48);
#define mailLED 49
//****************************************
//initializing touch sensor
int led = 3;
int sen = 8;
int val = 0;
//*************************************
//define motion detection sensor
#define trigPin 12
#define echoPin 13
#define pindetection 6
int ledstate = 0;
int theft_acknowledge = 0;
//******************************
//initializing smoke detector
//#define MQ2pin (0)
#define smokeoff_button 10
const int Aoutpin = 0;
float sensorValue; //variable to store sensor value
const int buzzer = 7;
int fireLED = 53;
const int pinread = 5;
int fire_acknowledge = 0;
//******************************
//initializing servo for door
Servo door;
int pos = 0;
//*****************************
//initializing servo for actualdoor
Servo door2;
int pos2 = 0;
//******************************
//initializing IR_garage sensor
const int pinIRd = 11;
const int pinIRa = A1;
int IRvalueA = 0;
int IRvalueD = 0;
int open_flag = 0;
int unlocked = 0;
//******************************
//initialising temperature sensor
int device = 4;
int setpoint = 26;
int AC_flag=0;
#define AC 28
#define dataPin 4
dht DHT;
//*****************************
//initialising lock switch
int lockswitch = 52;
//******************************
//initializing swimming pool
int swimmingpoollow = 42;
int swimmingpoolhigh = 40;
const int autohigh = 3;
const int autolow = 2;
int drowning_acknowledge = 0;
int IRvalueAhigh = 0;
int IRvalueDhigh = 0;
int IRvalueAlow = 0;
int IRvalueDlow = 0;
//******************************
//initialising Keypad and LCD
int lock = 0;
const int rs = 27, en = 26, d4 = 25, d5 = 24, d6 = 23, d7 = 22; //pins connected to LCD
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {37, 36, 35, 34}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {33, 32, 31}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); //initilize Keypad
char key ; //variable to get the input from user
int order = 0 ; //variable to change the screen on LCD (Display or set T or set H or set Pos)
bool flag = 0 ;
int i = 0 ;
bool check = 0 ;
int count = 0;
int Test_array[4] ; //array to store each char from the user
int Test = 0 ; // convert array to a number
int saved_password[4] = {1, 2, 3, 4};
int current_password[4] ;
int new_password[4] ;
int door_pin = 41 ;
//******************************
void setup() {
// put your setup code here, to run once:
//Limit switch
Serial.begin(9600);
limitSwitch.setDebounceTime(50);// set debounce time to 50 milliseconds
pinMode(mailLED, OUTPUT);
//*****************************************************
//touch sensor setup
Serial.begin(9600);
pinMode(sen, INPUT);
pinMode(led, OUTPUT);
digitalWrite(sen, LOW);
digitalWrite(led, LOW);
//**********************************
//servo setup
door.attach(9);
//*********************************
//servo home door setup
door2.attach(41);
//**********************************
//IR_garage setup
Serial.begin(9600);
pinMode(pinIRd, INPUT);
pinMode(pinIRa, INPUT);
//***********************************
//temperature sensor setup
Serial.begin(9600);
pinMode(device, OUTPUT);
pinMode(AC, OUTPUT);
//Serial.print("Give input of Setpoint");
//***********************************
//Keypad and LCD setup
lcd.begin(16 , 2); // initiaize lcd
//*********************************
//smoke detector setup
pinMode(buzzer , OUTPUT);
pinMode(fireLED , OUTPUT);
pinMode(smokeoff_button, INPUT);
//bluetooth module
pinMode(devicebluetooth, OUTPUT);
//motion detection setup
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pindetection, OUTPUT);
//******************************
//initializing lockswitch
pinMode(lockswitch, INPUT);
//*****************************
//initializing swimmingpool
pinMode(swimmingpoolhigh, INPUT);
pinMode(swimmingpoollow, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
home_mode();
distancedetection();
if (lock == 1) { //unlocked case
touch_sensor();
temperature_sensor();
smokedetector();
bluetoothmodule();
servo_garage();
limit_switch();
swimming_pool();
}
else //locked case
{
digitalWrite(AC, LOW);
digitalWrite(pindetection,LOW);
}
}
//********************************************************************************************************************
void touch_sensor()
{
while (digitalRead(sen) == HIGH)
{
while ((digitalRead(sen) == HIGH) && (val < 100))
{
analogWrite(led, val);
val++;
delay(15);
}
delay(1000);
while ((digitalRead(sen) == HIGH) && (val >= 0))
{
analogWrite(led, val);
val--;
delay(15);
}
delay(1000);
}
}
//*********************************************************************
void swimming_pool() {
IRvalueAhigh = analogRead(autohigh);
IRvalueDhigh = digitalRead(swimmingpoolhigh);
IRvalueAlow = analogRead(autolow);
IRvalueDlow = digitalRead(swimmingpoollow);
while ((IRvalueDlow == LOW) && (IRvalueDhigh == HIGH) && (!digitalRead(smokeoff_button)) && (!drowning_acknowledge))
{
digitalWrite(buzzer, !digitalRead(buzzer));
delay(500);
Serial.print("drowning level");
Serial.println("");
}
if (digitalRead(smokeoff_button))
{
drowning_acknowledge = 1;
}
if (drowning_acknowledge == 1) {
digitalWrite(buzzer, LOW);
}
drowning_acknowledge = 0;
}
void servo_home()
{
for (pos2 = 90; pos2 <= 270; pos2 += 1) { // goes from 0 degrees to 100 degrees
// in steps of 1 degree
door2.write(pos2); // tell servo to go to position in variable 'pos'
delay(10);
}// waits 10 ms for the servo to reach the position
delay(1000);
for (pos2 = 270; pos2 >= 90; pos2 -= 1)
{
// goes from 100 degrees to 0 degrees
door2.write(pos2); // tell servo to go to position in variable 'pos'
delay(10); // waits 10 ms for the servo to reach the position
}
}
//***********************************************************************
void servo_garage()
{
//Serial.print("Analog Reading=");
//Serial.print(IRvalueA);
//Serial.print("\t Digital Reading=");
//Serial.println(IRvalueD);
if ((IRvalueD == HIGH) && (open_flag == 0))
{
for (pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 100 degrees
// in steps of 1 degree
door.write(pos); // tell servo to go to position in variable 'pos'
delay(10);
}// waits 10 ms for the servo to reach the position
open_flag = 1;
}
if ((IRvalueD == LOW) && (open_flag == 1))
{
for (pos = 100; pos >= 0; pos -= 1)
{
// goes from 100 degrees to 0 degrees
door.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 10 ms for the servo to reach the position
}
open_flag = 0;
}
delay(50);
IRvalueA = analogRead(pinIRa);
IRvalueD = digitalRead(pinIRd);
}
//****************************************************************************
void temperature_sensor()
{
int readData = DHT.read11(dataPin);
float t = DHT.temperature;
float h = DHT.humidity;
////Serial.print("Temperature = ");
////Serial.print(t);
////Serial.print(" *C ");
////Serial.print(" Humidity = ");
////Serial.print(h);
////Serial.println(" % ");
if ((t >= 23) && (sensorValue < 1050) && (lock))
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature ");
lcd.print(t);
lcd.setCursor(0, 1);
lcd.print("AC ON");
digitalWrite(AC, HIGH);
}
else if((t<23)&&(!AC_flag))
digitalWrite(AC, LOW);
Serial.print(t);
Serial.print(",");
Serial.print(h);
Serial.print(",");
}
//********************************************************************************************************************//
void smokedetector()
{
sensorValue = analogRead(Aoutpin);
////Serial.print("Sensor Value: ");
////Serial.print(sensorValue);
Serial.println(sensorValue);
while ((sensorValue >= 1050) && (!digitalRead(smokeoff_button)) && (!fire_acknowledge))
{
lcd.clear();
////Serial.print(" | Smoke detected!");
digitalWrite(buzzer, !digitalRead(buzzer));
digitalWrite(fireLED, !digitalRead(fireLED));
lcd.setCursor(0, 0);
lcd.print("FIRE!!!!!!");
delay(500);
}
if (digitalRead(smokeoff_button))
{
fire_acknowledge = 1;
}
if (fire_acknowledge)
{
digitalWrite(buzzer, LOW);
digitalWrite(fireLED, LOW);
}
if (sensorValue < 1050)
fire_acknowledge = 0;
////Serial.println("");
}
//********************************************************************************************************************//
void distancedetection()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
//Serial.print(distance);
if (distance >= 5 || distance <= 0)
{
ledstate = 0;
////Serial.println("Out of range");
//digitalWrite(pindetection, LOW);
}
else {
if (ledstate == 0) {
ledstate = 1;
digitalWrite(pindetection, HIGH);
//Serial.print(distance);
//Serial.println(" cm");
}
else
{ ledstate = 0;
digitalWrite(pindetection, LOW);
//Serial.print(distance);
//Serial.println(" cm");
}
}
while (!lock && (distance < 5) && (!digitalRead(smokeoff_button)) && !(theft_acknowledge))
{
lcd.clear();
//Serial.print(" BURGLAR ALARM!");
digitalWrite(buzzer, !digitalRead(buzzer));
digitalWrite(fireLED, !digitalRead(fireLED));
lcd.setCursor(0, 0);
lcd.print("BURGLAR ALERT!");
digitalWrite(pindetection, LOW); //turnoff led for enterance derection (works for unlocked mode only)
delay(500);
}
if (digitalRead(smokeoff_button)) //same button for fire alarm :)
{
theft_acknowledge = 1;
}
if (theft_acknowledge)
{
digitalWrite(buzzer, LOW);
digitalWrite(fireLED, LOW);
}
if (distance >= 5)
theft_acknowledge = 0;
////Serial.println(smokeoff_button);
}
//*************************************************
void bluetoothmodule() {
// To check if data is available on Serial port
if (Serial.available() > 0) {
// To read a data in integer form from HC05 via serial communication protocol
setpoint = Serial.parseInt();
//Serial.print("Set Point Changed: ");
//Serial.println(setpoint1);
}
int temperature = analogRead(A1);
temperature = map(temperature, 0, 1023, 0, 500);
//Serial.print(temperature);
//Serial.print(",");
//Serial.print("Temperature = ");
//Serial.println(temperature);
if (temperature >= setpoint1) {
digitalWrite (device, HIGH);
//Serial.println("Device is on");
}
else if (temperature < setpoint1) {
digitalWrite (device, LOW);
//Serial.println("Device is off");
}
delay(500);
if (Serial.available() > 0)
{
char data = Serial.read(); // reading the data received from the bluetooth module
switch (data)
{
case '1':
{
digitalWrite(led, HIGH);
break;
}
case '2':
{
digitalWrite(led, LOW);
break;
} case '3':
{
digitalWrite(pindetection, HIGH);
break;
}
case '4':
{
digitalWrite(pindetection, LOW);
break;
}
case '5':
{
digitalWrite(AC, HIGH);
AC_flag=1;
break;
}
case '6':
{
digitalWrite(AC, LOW);
AC_flag=0;
break;
}
default : break;
}
}
delay(50);
}
//*************************************************************************************
void Get_Order () {
if ( key == '*' && flag == 0 ) { //each time user press * the order number increases to change screen
order = order + 1 ; // Display or set T or set H or set Pos)
flag = 1 ;
lcd.clear();
i = 0;
check = 0 ;
}
else if ( key != '*' ) {
flag = 0 ;
}
if ( order == 2 ) {
order = 0 ;
}
}
//********************************************************************************************************************//
void Enter_Password() {
if (i == 0) {
lcd.setCursor(0, 0);
lcd.print("Enter Password : " );
lcd.setCursor(0, 1);
lcd.print("- - - -" );
}
if (key == '0' || key == '1' || key == '2' || key == '3'
|| key == '4' || key == '5' || key == '6'
|| key == '7' || key == '8' || key == '9')
{
lcd.setCursor(2 * i, 1);
lcd.print(key);
delay(50);
lcd.setCursor(2 * i, 1);
lcd.print("*");
current_password[i] = (int)key - 48 ;
i = i + 1 ;
}
if (i == 4) {
if ((current_password[0] == saved_password[0]) && (current_password[1] == saved_password[1]) &&
(current_password[2] == saved_password[2]) && (current_password[3] == saved_password[3]) ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome");
lock = 1;
servo_home();
lcd.clear();
i = 0 ;
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect Password");
delay(1000);
lcd.clear();
i = 0 ;
}
}
}
//****************************************************
void Change_Password() {
if (check == 0) {
if (i == 0) {
lcd.setCursor(0, 0);
//lcd.print("*Change Password*" );
//delay(1000);
lcd.setCursor(0, 0);
lcd.print("Enter OLD Password :" );
lcd.setCursor(0, 1);
lcd.print("- - - -" );
}
if (key == '0' || key == '1' || key == '2' || key == '3'
|| key == '4' || key == '5' || key == '6'
|| key == '7' || key == '8' || key == '9') {
lcd.setCursor(2 * i, 1);
lcd.print(key);
delay(300);
lcd.setCursor(2 * i, 1);
lcd.print("*");
current_password[i] = (int)key - 48 ;
i = i + 1 ;
}
if (i == 4) {
if ((current_password[0] == saved_password[0]) && (current_password[1] == saved_password[1]) &&
(current_password[2] == saved_password[2]) && (current_password[3] == saved_password[3]) ) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter New Password");
lcd.setCursor(0, 1);
lcd.print("- - - -" );
check = 1;
i = 0;
}
else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Incorrect Password");
delay(1000);
lcd.clear();
i = 0 ;
}
}
}
else if (check == 1) {
if (key == '0' || key == '1' || key == '2' || key == '3'
|| key == '4' || key == '5' || key == '6'
|| key == '7' || key == '8' || key == '9') {
lcd.setCursor(2 * i, 3);
lcd.print(key);
delay(300);
lcd.setCursor(2 * i, 3);
lcd.print("*");
new_password[i] = (int)key - 48 ;
i = i + 1 ;
}
if (i == 4) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password Changed");
saved_password[0] = new_password[0];
saved_password[1] = new_password[1];
saved_password[2] = new_password[2];
saved_password[3] = new_password[3];
delay(1000);
lcd.clear();
i = 0 ;
check = 0 ;
order = 0 ;
}
}
}
//********************************************************************************************************************//
void home_mode()
{
key = keypad.getKey(); // function to get a key from the keypad
Get_Order () ; //function to get an order from the user (Display or set T or set H or set Pos)
if ( order == 0 ) { // if order=0 display date on LCD
Enter_Password(); //function to display data
}
else if ( order == 1 ) {
Change_Password();
}
if (digitalRead(lockswitch) && lock == 1)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HOME LOCKED");
delay(3000);
lock = 0;
}
}
//***********************************************************************************************************************
void limit_switch()
{
int flag = 0;
int count = 4;
limitSwitch.loop(); // MUST call the loop() function first
if (limitSwitch.isPressed())
{
//Serial.println("The limit switch: UNTOUCHED -> TOUCHED");
flag = 1;
}
if (limitSwitch.isReleased())
{
//Serial.println("The limit switch: TOUCHED -> UNTOUCHED");
}
/*int state = limitSwitch.getState();
if(state == HIGH)
//Serial.println("The limit switch: UNTOUCHED");
else
//Serial.println("The limit switch: TOUCHED");
*/
while ((flag == 1) && count >= 0)
{
digitalWrite(mailLED, !digitalRead(mailLED));
delay(500);
count--;
}
flag = 0;
digitalWrite(mailLED, LOW);
}
//***********************************************************************************************************************