-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutomatic_Bathroom_Sensor_fan_online.ino
177 lines (161 loc) · 4.24 KB
/
Automatic_Bathroom_Sensor_fan_online.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
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
char auth[] = "#########";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "#######";
char pass[] = "########";
/////////////3/////////////
// Setting up the pin and DHT version
#define DHTTYPE DHT11 // DHT Shield uses DHT 11
#define DHTPIN D4 // DHT Shield uses pin D4
DHT dht(DHTPIN, DHTTYPE);
// Set Colors of LEDs
/* Not currently used
//Widget Colors
#define BLYNK_Green "#23C48E"
#define BLYNK_Blue "#04C0F8"
#define BLYNK_Red "#D3435C"
*/
//Set up of the Humiditly LED & override LED
//these are software LEDs in app
WidgetLED humLED(V1);
WidgetLED overrideLED(V2);
//this timer is used for the blynk app/server and to call data from the sensor
BlynkTimer timer;
//this timer is used to shut off the fan after a cer
SimpleTimer overrideShutOffTimer;
/////////////4/////////////
//Sensor varables
float h = 0;
float t = 0;
//Fan varables
int fanState = 0;
bool fanOverride = false;
// countdown variables and timer
int CountdownRemain;
int CountdownTimer;
/////////////5/////////////
// this funtion gets the data from the sensro and send it to the blynk server for you to see on the app
void sendSensor()
{
h = dht.readHumidity();
t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
int hAsInt = int(h); // converts to int removing unessisary decimal points
int tAsInt = int(t);
Blynk.virtualWrite(V5, hAsInt);
Blynk.virtualWrite(V6, tAsInt);
fanControl();
}
/////////////6/////////////
// Fan Override Button
BLYNK_WRITE(V0)
{
int buttonState = param.asInt();
/*Debug
Serial.print("button pressed: ");
Serial.println(buttonState);
*/
if(buttonState == 1){
fanState = 1;
fanOverride = true;
overrideLED.on();
CountdownRemain = 600;
overrideShutOffTimer.enable(CountdownTimer);
/*Debug
Serial.print("button State: ");
Serial.print(buttonState);
Serial.print(" fan State: ");
Serial.print(fanState);
Serial.print(" fan override: ");
Serial.println(fanOverride);
*/
}else{
fanState = 0;
fanOverride = true;
CountdownRemain = 600;
overrideShutOffTimer.enable(CountdownTimer);
overrideLED.on();
/*Debug
Serial.print("button State: ");
Serial.print(buttonState);
Serial.print(" fan State: ");
Serial.print(fanState);
Serial.print(" fan override: ");
Serial.println(fanOverride);
*/
}
fanControl();
}
/////////////7/////////////
void fanControl()
{
if(h > 60 && fanOverride != true){
fanState = 1; //ON
Blynk.virtualWrite(V0, fanState);
humLED.on();
Serial.print("over 60H, ");
}
else if(h < 50 && fanOverride != true){
fanState = 0; //OFF
Blynk.virtualWrite(V0, fanState);
humLED.off();
Serial.print("less than 50H, ");
}
else{
Serial.print("fan override, ");
}
digitalWrite(D1, fanState);
/* Debug
Serial.print(" fan State: ");
Serial.print(fanState);
Serial.print(" fan override: ");
Serial.println(fanOverride);
*/
}
void CountdownTimerFunction() {
Serial.print("countdown function called ");
CountdownRemain--; // remove 1 every second
Serial.println(CountdownRemain);
Blynk.virtualWrite(V3, CountdownRemain);
if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
overrideShutOffTimer.disable(CountdownTimer); // if 0 stop timer
CountdownRemain = 600;
fanOverride = false;
overrideLED.off();
/*Debug
Serial.println("counter if");
*/
} else {
//manLED.off();
Serial.println("counter else");
}
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(D1, OUTPUT); // Set the relay output up
Blynk.begin(auth, ssid, pass);
// Setup a function to be called every 5 seconds
timer.setInterval(5000L, sendSensor);
//Shut off the manual over ride
CountdownTimer = overrideShutOffTimer.setInterval(1000L, CountdownTimerFunction);
overrideShutOffTimer.disable(CountdownTimer);
}
void loop()
{
Blynk.run();
timer.run();
overrideShutOffTimer.run();
}