-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCode
219 lines (187 loc) · 5.56 KB
/
Code
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
/***************************************************************************************************************
* Smart Cradle using NodeMCU ESP-12 Develop Kit V1.0
* Sensor data sent to Blynk app
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk library is licensed under MIT license
* Input Sensor :Sound sensor
* :Water Sensor
* :PIR sensor
* :Temperature and humidity sensor
* Actuators :Buzzer module
* :Servo Motor
* Version 2.0.0 Blynk&TS - Developed by Sachin
* Date : 29-05-2021
*****************************************************************************************************************/
/* ESP 8266 Library*/
#include <ESP8266WiFi.h>
WiFiClient client;
/*blynk library */
#include <BlynkSimpleEsp8266.h>
char auth[] = "xxxxxxxxxxxxxxxx"; //Place your Blynk applications project token code
char ssid[] = "ABCD";// Hotspot / wifi name
char pass[] = "Airtel@123";// Password
#include <Servo.h> // Include the library
Servo servo1; // Create the Servo and name it "servo1"
#define servo1Pin D8 // Define the NodeMCU pin D8 to attach the Servo
/* DHT11*/
#include "DHT.h"
#define DHTPIN D0 // DHT 11 : Temperature & humidity sensor connected to digital pin D0
#define DHTTYPE DHT11 // Sensor type : DHT11
DHT dht(DHTPIN, DHTTYPE); // Send DHT pin and type parameter to DHT Function
float hum = 0; /* at init humidity is 0 */
float temp = 0;/* at init temp is 0*/
/* TIMER Library*/
#include <SimpleTimer.h>
SimpleTimer timer;
#define PirSensorPin D1 // PIR Sensor connected to digital pin D1
#define Watersensor D2 // Water/Wet sensor to Digital pin D2
#define SoundSensorPin A0 // Analog sound sensor to Analog input in A0
#define Buzzer D3 // Buzzer to digital pin D3
int PIR_Sensor = 0;
int Water_Sensor = 0;
int Sound_Level = 0;
/*Define Macros with virtual PIN for Blynk Widgets */
#define All_Auto V5
#define Servo_Manual V6
#define Servo_Angle V7
#define Servo_Delay V8
#define Sound_Threshold V9
/*init condition as 0 */
int All_Auto_State = 0;
int Servo_Manual_State = 0;
int Servo_Angle_input = 0;
int Servo_Delay_input = 0;
int Sound_Thrshold_input = 0;
// Request the latest state from the server
BLYNK_CONNECTED()
{
Blynk.syncVirtual(All_Auto);
Blynk.syncVirtual(Servo_Manual);
Blynk.syncVirtual(Servo_Angle);
Blynk.syncVirtual(Servo_Delay);
Blynk.syncVirtual(Sound_Threshold);
}
// sync the recieved data from application to variables
BLYNK_WRITE(All_Auto)
{
All_Auto_State = param.asInt();
}
BLYNK_WRITE(Servo_Manual)
{
Servo_Manual_State = param.asInt();
}
BLYNK_WRITE(Servo_Angle)
{
Servo_Angle_input = param.asInt();
}
BLYNK_WRITE(Servo_Delay)
{
Servo_Delay_input = param.asInt();
}
BLYNK_WRITE(Sound_Threshold)
{
Sound_Thrshold_input = param.asInt();
}
void setup()
{
Serial.begin(115200);
delay(10);
servo1.attach(servo1Pin);
Blynk.begin(auth, ssid, pass);
pinMode(Buzzer, OUTPUT);
pinMode(servo1Pin, OUTPUT);
pinMode(DHTPIN, INPUT);
pinMode(SoundSensorPin, INPUT);
pinMode(Watersensor, INPUT);
pinMode(PirSensorPin, INPUT);
servo1.write(0);
dht.begin();
timer.setInterval(2000L, getInputData);
timer.setInterval(4000L, sendUptime);
}
void loop()
{
if (All_Auto_State == 1 && Servo_Manual_State == 0)
{
AutomateAll();
}
if (All_Auto_State == 0 && Servo_Manual_State == 1)
{
Run_Servo();
}
if (Water_Sensor == 1)
{
Blynk.notify("Wet Detected !!!");
Serial.println("Wet Detected !!!");
tone(Buzzer,1);
delay(2000);
noTone(Buzzer);
tone(Buzzer,0);
}
timer.run(); // Initiates SimpleTimer
Blynk.run(); // Initiates Blynk server
}
void AutomateAll()
{
Serial.println("Automate all !!!");
if(PIR_Sensor == 1 & Sound_Level >= Sound_Thrshold_input)
{
Run_Servo();
Blynk.notify("sound and movement Detected !!!");
Serial.println("sound and movement Detected Run servo !!!");
}
else if (Water_Sensor == 1)
{
Blynk.notify("Wet Detected !!!");
Serial.println("Wet Detected !!!");
tone(Buzzer,1);
delay(2000);
noTone(Buzzer);
tone(Buzzer,0);
}
}
void Run_Servo()
{
Serial.println("Run servo !!!");
servo1.write(Servo_Angle_input);
delay(Servo_Delay_input);
servo1.write(0);
delay(Servo_Delay_input);
servo1.write(Servo_Angle_input);
}
void getInputData(void)
{
float tempIni = temp;
float humIni = hum;
temp = dht.readTemperature();
hum = dht.readHumidity();
Water_Sensor = !digitalRead(Watersensor); /*Wet sensor is Active high , so it is inverted */
Sound_Level = analogRead(SoundSensorPin);
PIR_Sensor = digitalRead(PirSensorPin);
Serial.print("Water_Sensor: ");
Serial.println(Water_Sensor);
Serial.print("Sound_Level: ");
Serial.println(Sound_Level);
Serial.print("PIR_Sensor: ");
Serial.println(PIR_Sensor);
/* if (isnan(hum) || isnan(temp)) // Check if any reads failed and exit early (to try again).
{
Serial.println("Failed to read from DHT sensor!");
temp = tempIni;
hum = humIni;
return;
} */
}
/***************************************************
* Send DHT data to Blynk
**************************************************/
void sendUptime()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(00, temp); //virtual pin V00
Blynk.virtualWrite(01, hum); // virtual pin V01
// Blynk.virtualWrite(04, PIR_Sensor);
// Blynk.virtualWrite(02, Sound_Level);
//Blynk.virtualWrite(03, Water_Sensor);
}