-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_plant_Monitoring_System.ino
More file actions
194 lines (160 loc) · 4.46 KB
/
Smart_plant_Monitoring_System.ino
File metadata and controls
194 lines (160 loc) · 4.46 KB
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
/* Connections:
Soil Moisture Sensor: A0
PIR Motion Sensor: D6
Relay Module: D7
Push Button: D5
DHT Sensor: D4
*/
#define BLYNK_TEMPLATE_ID "TMPL6b-B2x9Vj"
#define BLYNK_TEMPLATE_NAME "Smart plant monitoring system"
// Include libraries
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
// Initialize LCD and DHT sensor
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Wi-Fi and Blynk credentials
char auth[] = "bog1VV5PcXgp7dJNaiEh49_MzjOguWgS";
char ssid[] = "Galaxy M519904";
char pass[] = "ashuashu";
// Define pins for components
#define SOIL_PIN A0
#define PIR_PIN D6
#define RELAY_PIN D7
#define BUTTON_PIN D5
#define DHT_PIN D4
DHT dht(DHT_PIN, DHT11);
BlynkTimer timer;
void checkPhysicalButton();
int pirToggleValue = 0;
int relayState = LOW;
int buttonState = HIGH;
// Virtual pin assignments
#define VPIN_BUTTON V12
#define VPIN_TEMP V0
#define VPIN_HUMIDITY V1
#define VPIN_SOIL V3
#define VPIN_PIR_LED V5
// Initializing DHT in setup
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalWrite(RELAY_PIN, relayState);
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
// Display initializing message
lcd.setCursor(0, 0);
lcd.print(" Initializing ");
for (int i = 5; i <= 10; i++) {
lcd.setCursor(i, 1);
lcd.print(".");
delay(250); // Adjusted delay for smoother loading
}
lcd.clear();
lcd.setCursor(11, 1);
lcd.print("W:OFF");
// Set up Blynk timers
timer.setInterval(2000L, updateSoilMoisture); // Soil sensor update every 2 seconds
timer.setInterval(2000L, updateDHTSensor); // DHT sensor update every 2 seconds
timer.setInterval(500L, checkButtonState); // Button check every 500ms
}
// Function to read temperature and humidity from DHT11
void updateDHTSensor() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Print to serial for debugging
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("DHT Error "); // Clear previous values
delay(250); // Shorter delay to retry quickly
return;
}
Blynk.virtualWrite(VPIN_TEMP, temperature);
Blynk.virtualWrite(VPIN_HUMIDITY, humidity);
lcd.setCursor(0, 0);
lcd.print("T: ");
lcd.print(temperature);
lcd.setCursor(8, 0);
lcd.print(" H: ");
lcd.print(humidity);
}
// Function to read soil moisture
void updateSoilMoisture() {
int soilValue = analogRead(SOIL_PIN);
soilValue = map(soilValue, 0, 1024, 0, 100);
soilValue = (soilValue - 100) * -1; // Adjust for inverted values
Blynk.virtualWrite(VPIN_SOIL, soilValue);
lcd.setCursor(0, 1);
lcd.print("S: ");
lcd.print(soilValue);
lcd.print("%");
}
// Function to check PIR sensor and control virtual LED
void updatePIRSensor() {
bool pirValue = digitalRead(PIR_PIN);
if (pirValue) {
Blynk.logEvent("PIRmotion", "WARNING! Motion Detected!");
Blynk.virtualWrite(VPIN_PIR_LED, 255); // Turn on LED in Blynk app
lcd.setCursor(5, 1);
lcd.print("M: ON ");
} else {
Blynk.virtualWrite(VPIN_PIR_LED, 0); // Turn off LED in Blynk app
lcd.setCursor(5, 1);
lcd.print("M: OFF");
}
}
// Blynk write for PIR toggle control
BLYNK_WRITE(V6) {
pirToggleValue = param.asInt();
}
// Blynk write for button relay control
BLYNK_WRITE(VPIN_BUTTON) {
relayState = param.asInt();
digitalWrite(RELAY_PIN, relayState);
updateRelayDisplay();
}
// Check physical button and update relay state
void checkButtonState() {
if (digitalRead(BUTTON_PIN) == LOW) {
if (buttonState != LOW) {
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState);
Blynk.virtualWrite(VPIN_BUTTON, relayState);
updateRelayDisplay();
}
buttonState = LOW;
} else {
buttonState = HIGH;
}
}
// Display relay state on LCD
void updateRelayDisplay() {
lcd.setCursor(11, 1);
if (relayState == HIGH) {
lcd.print("W: ON ");
} else {
lcd.print("W: OFF");
}
}
void loop() {
Blynk.run();
timer.run();
// Check PIR if enabled
if (pirToggleValue == 1) {
updatePIRSensor();
} else {
lcd.setCursor(5, 1);
lcd.print("M: OFF");
}
}