-
Notifications
You must be signed in to change notification settings - Fork 0
/
greenhouse_2.0.ino
417 lines (356 loc) · 10.4 KB
/
greenhouse_2.0.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
/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_NAME "Greenhouse"
#define BLYNK_AUTH_TOKEN "ajeuZJMB4uGp8V4rfbhJ3s71_wKGD4fA"
#define BLYNK_TEMPLATE_ID "TMPL6Xr608Zyd"
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ravindu"; //WiFi Name
char pass[] = "12121212"; //WiFi Password
//Set the maximum wet and maximum dry value measured by the sensor
int wetSoilVal = 1104 ; //min value when soil is wet
int drySoilVal = 2635 ; //max value when soil is dry
//Set ideal moisture range percentage(%) in soil
int moistPerLow = 20 ; //min moisture %
int moistPerHigh = 80 ; //max moisture %
int hulow = 85;
int huhigh = 90 ;
int templow = 20 ;
int temphigh = 22 ;
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <AceButton.h>
using namespace ace_button;
// Define connections to sensor
#define Temperature_SensorPino 14 //D14
#define Soil_Moisture_SensorPin 34 //D34
#define Temperature_SensorPin 27 //D14
#define Motor_RelayPin 1 //D25
#define Fan_RelayPin 32
#define Heater_RelayPin 15
#define Vent_RelayPin 19
#define Humidifier_RelayPin 13
#define wifiLed 2 //D2
#define ModeSwitchPin 33 //D33
#define ModeLed 3 //D15
#define DHTTYPE DHT22
#define DHTTYPEo DHT11
//Change the virtual pins according the rooms
#define VPIN_Moisture V1
#define VPIN_TEMPERATURE V2
#define VPIN_HUMIDITY V3
#define VPIN_MODE_SWITCH V4
#define VPIN_FAN V5
#define VPIN_VENT V6
#define VPIN_HUMIDIFIER V7
#define VPIN_HEATER V8
#define VPIN_MOTOR V9
#define VPIN_TO V10
#define VPIN_HO V11
int sensorVal;
int moisturePercentage;
bool toggleRelay = HIGH; //Define to remember the toggle state
bool toggleRelay1 = HIGH;
bool toggleRelay2 = HIGH;
bool toggleRelay3 = HIGH;
bool prevMode = true;
int temperature1 = 0;
int humidity1 = 0;
int temperature1o = 0;
int humidity1o = 0;
String currMode = "A";
char auth[] = BLYNK_AUTH_TOKEN;
ButtonConfig config2;
AceButton button2(&config2);
void handleEvent2(AceButton*, uint8_t, uint8_t);
BlynkTimer timer;
DHT dht(Temperature_SensorPin, DHTTYPE);
DHT dhto(Temperature_SensorPino, DHTTYPEo);
void checkBlynkStatus() { // called every 3 seconds by SimpleTimer
bool isconnected = Blynk.connected();
if (isconnected == false) {
Serial.print("Blynk Not Connected ");
digitalWrite(wifiLed, LOW);
}
if (isconnected == true) {
digitalWrite(wifiLed, HIGH);
//Serial.println("Blynk Connected");
}
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_Moisture);
Blynk.syncVirtual(VPIN_FAN);
Blynk.syncVirtual(VPIN_TEMPERATURE);
Blynk.syncVirtual(VPIN_HUMIDITY);
Blynk.syncVirtual(VPIN_VENT);
Blynk.syncVirtual(VPIN_MOTOR);
Blynk.syncVirtual(VPIN_HEATER);
Blynk.syncVirtual(VPIN_HUMIDIFIER);
Blynk.syncVirtual(VPIN_TO);
Blynk.syncVirtual(VPIN_HO);
//Blynk.syncVirtual(VPIN_MODE_SWITCH);
Blynk.virtualWrite(VPIN_MODE_SWITCH, prevMode);
}
BLYNK_WRITE(VPIN_MOTOR) {
if(!prevMode){
toggleRelay = param.asInt();
digitalWrite(Motor_RelayPin, toggleRelay);
}
else{
Blynk.virtualWrite(VPIN_MOTOR, toggleRelay);
}
}
BLYNK_WRITE(VPIN_FAN) {
if(!prevMode){
toggleRelay2 = param.asInt();
digitalWrite(Fan_RelayPin, toggleRelay2);
}
else{
Blynk.virtualWrite(VPIN_FAN, toggleRelay2);
}
}
BLYNK_WRITE(VPIN_HUMIDIFIER) {
if(!prevMode){
toggleRelay3 = param.asInt();
digitalWrite(Humidifier_RelayPin, toggleRelay3);
}
else{
Blynk.virtualWrite(VPIN_HUMIDIFIER, toggleRelay3);
}
}
BLYNK_WRITE(VPIN_HEATER) {
if(!prevMode){
toggleRelay1 = param.asInt();
digitalWrite(Heater_RelayPin , toggleRelay1);
}
else{
Blynk.virtualWrite(VPIN_HEATER, toggleRelay1);
}
}
BLYNK_WRITE(VPIN_MODE_SWITCH) {
if(prevMode != param.asInt()){
prevMode = param.asInt();
currMode = prevMode ? "A" : "M";
digitalWrite(ModeLed, prevMode);
if(!prevMode ==HIGH && toggleRelay == LOW){
digitalWrite(Motor_RelayPin, HIGH);
toggleRelay = HIGH;
Blynk.virtualWrite(VPIN_MOTOR, toggleRelay);
}
if(!prevMode==HIGH && toggleRelay2 == LOW){
digitalWrite(Fan_RelayPin, HIGH);
toggleRelay2 = HIGH;
Blynk.virtualWrite(VPIN_FAN, toggleRelay2);
}
if(!prevMode==HIGH && toggleRelay1 == LOW){
digitalWrite(Heater_RelayPin, HIGH);
toggleRelay1 = HIGH;
Blynk.virtualWrite(VPIN_HEATER, toggleRelay1);
}
if(!prevMode==HIGH && toggleRelay3 == LOW){
digitalWrite(Humidifier_RelayPin, HIGH);
toggleRelay3 = HIGH;
Blynk.virtualWrite(VPIN_HUMIDIFIER, toggleRelay3);
}
}
}
void getMoisture(){
sensorVal = analogRead(Soil_Moisture_SensorPin);
if (sensorVal > (wetSoilVal - 100) && sensorVal < (drySoilVal + 100) ){
moisturePercentage = map(sensorVal ,drySoilVal, wetSoilVal, 0, 100);
// Print result to serial monitor
Serial.print("Moisture Percentage: ");
Serial.print(moisturePercentage);
Serial.println(" %");
}
else{
Serial.println(sensorVal);
}
delay(100);
}
void getWeather(){
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
else {
humidity1 = int(h);
temperature1 = int(t);
// Serial.println(temperature1);
// Serial.println(humidity1);
}
}
void getWeathero(){
float ho = dhto.readHumidity();
float to = dhto.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(ho) || isnan(to)) {
Serial.println("Failed to read from DHT sensoro!");
return;
}
else {
humidity1o = int(ho);
temperature1o = int(to);
// Serial.println(temperature1);
// Serial.println(humidity1);
}
}
void sendSensor(){
getMoisture(); // get Moisture reading
getWeather(); // get reading from DHT11
getWeathero();
Blynk.virtualWrite(VPIN_Moisture, moisturePercentage);
Blynk.virtualWrite(VPIN_TEMPERATURE, temperature1);
Blynk.virtualWrite(VPIN_HUMIDITY, humidity1);
Blynk.virtualWrite(VPIN_HO,humidity1o );
Blynk.virtualWrite(VPIN_TO, temperature1o);
}
void controlMoist(){
if(prevMode){
if (moisturePercentage < (moistPerLow)){
if (toggleRelay == HIGH){
digitalWrite(Motor_RelayPin, LOW);
toggleRelay = LOW;
Blynk.virtualWrite(VPIN_MOTOR, toggleRelay);
delay(1000);
}
}
if (moisturePercentage > (moistPerHigh)){
if (toggleRelay == LOW){
digitalWrite(Motor_RelayPin, HIGH);
toggleRelay = HIGH;
Blynk.virtualWrite(VPIN_MOTOR, toggleRelay);
delay(1000);
}
}
}
else{
}
}
void controlTemp(){
if(prevMode){
if (temperature1 < (templow)){
if (toggleRelay1 == HIGH){
digitalWrite(Heater_RelayPin, LOW);
toggleRelay1 = LOW;
Blynk.virtualWrite(VPIN_HEATER, toggleRelay1);
delay(1000);
}
}
if(temperature1 > (templow)){
if (toggleRelay1 == LOW){
digitalWrite(Heater_RelayPin, HIGH);
toggleRelay1 = HIGH;
Blynk.virtualWrite(VPIN_HEATER, toggleRelay1);
delay(1000);
}
}
if (temperature1 > (temphigh)){
if (toggleRelay2 == HIGH){
digitalWrite(Fan_RelayPin, LOW);
toggleRelay = LOW;
Blynk.virtualWrite(VPIN_FAN, toggleRelay2);
delay(1000);
}
}
if(temperature1 < (temphigh)){
if (toggleRelay2 == LOW){
digitalWrite(Fan_RelayPin, HIGH);
toggleRelay2 = HIGH;
Blynk.virtualWrite(VPIN_FAN, toggleRelay2);
delay(1000);
}
}
}
else{
}
}
void controlHumidity(){
if(prevMode){
if (humidity1 < (hulow)){
if (toggleRelay3 == HIGH){
digitalWrite(Humidifier_RelayPin, LOW);
toggleRelay3 = LOW;
Blynk.virtualWrite(VPIN_HUMIDIFIER, toggleRelay3);
delay(1000);
}
}
if (humidity1 > (huhigh)){
if (toggleRelay3 == LOW){
digitalWrite(Humidifier_RelayPin, HIGH);
toggleRelay3 = HIGH;
Blynk.virtualWrite(VPIN_HUMIDIFIER, toggleRelay3);
delay(1000);
}
}
}
else{
}
}
void setup() {
// Set up serial monitor
Serial.begin(115200);
// Set pinmodes for GPIOs
pinMode(Motor_RelayPin, OUTPUT);
pinMode(wifiLed, OUTPUT);
pinMode(ModeLed, OUTPUT);
pinMode(Fan_RelayPin , OUTPUT);
pinMode(Heater_RelayPin , OUTPUT);
pinMode(Vent_RelayPin , OUTPUT);
pinMode(Humidifier_RelayPin , OUTPUT);
pinMode(ModeSwitchPin, INPUT_PULLUP);
digitalWrite(wifiLed, LOW);
digitalWrite(ModeLed, LOW);
dht.begin(); // Enabling DHT sensor
dhto.begin();
config2.setEventHandler(button2Handler);
button2.init(ModeSwitchPin);
WiFi.begin(ssid, pass);
timer.setInterval(2000L, checkBlynkStatus); // check if Blynk server is connected every 2 seconds
timer.setInterval(3000L, sendSensor); // display and send sensor reading every 3 seconds
Blynk.config(auth);
//delay(1000);
digitalWrite(ModeLed, prevMode);
}
void loop() {
Blynk.run();
timer.run(); // Initiates SimpleTimer
button2.check();
controlMoist();
controlTemp();
controlHumidity();
}
void button2Handler(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT2");
switch (eventType) {
case AceButton::kEventReleased:
//Serial.println("kEventReleased");
if(prevMode ==HIGH && toggleRelay == LOW){
digitalWrite(Motor_RelayPin, HIGH);
toggleRelay = HIGH;
Blynk.virtualWrite(VPIN_MOTOR, toggleRelay);
}
if(prevMode == HIGH && toggleRelay1 == LOW){
digitalWrite(Heater_RelayPin, HIGH);
toggleRelay1 = HIGH;
Blynk.virtualWrite(VPIN_HEATER, toggleRelay1);
}
if(prevMode==HIGH && toggleRelay2 == LOW){
digitalWrite(Fan_RelayPin, HIGH);
toggleRelay2 = HIGH;
Blynk.virtualWrite(VPIN_FAN, toggleRelay2);
}
if(prevMode==HIGH && toggleRelay3 == LOW){
digitalWrite(Humidifier_RelayPin, HIGH);
toggleRelay3 = HIGH;
Blynk.virtualWrite(VPIN_HUMIDIFIER, toggleRelay3);
}
prevMode = !prevMode;
currMode = prevMode ? "A" : "M";
digitalWrite(ModeLed, prevMode);
Blynk.virtualWrite(VPIN_MODE_SWITCH, prevMode);
break;
}
}