-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_sensors_merged.ino
196 lines (159 loc) · 5.59 KB
/
all_sensors_merged.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
#define BLYNK_TEMPLATE_ID "define your template id"
#define BLYNK_TEMPLATE_NAME "define template name"
#define BLYNK_AUTH_TOKEN "define auth token"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>
#include <Adafruit_Sensor.h>
const char* ssid = "ssid"; // Define your wifi ssid/name
const char* password = "password"; //Define your wifi password
const int tdsPin = A0; // Analog pin for TDS sensor
const int tempPin = D2; // digital pin for temperature sensor
const int ultraSonicTrig = D0; // Trigger pin for ultrasonic sensor
const int ultraSonicEcho = D1; // Echo pin for ultrasonic sensor
const int fanPin = D7; // Digital pin for fan
const int npumpPin = D3; //Digital pin for nutrients pump(TDS)
const int wpumpPin = D5; //Digital pin for water pump
const int airpumpPin = D6; //Digital pin for air pump
#define DHTTYPE DHT11
DHT dht(tempPin, DHTTYPE);
long duration;
const float thresholdTDS = 400; // Change this based on desired TDS level for pump activation
const int tempFanOn = 30; // Change this based on desired temperature for fan activation
const int tempFanOff = 20; // Change this based on desired temperature for fan deactivation
const int maxDistance = 35; // Change this based on desired distance threshold
unsigned long lastLoopTime = millis();
unsigned long currentMillis = millis();
const unsigned long inter = 5 * 60 * 60 * 1000; // 5 hours in milliseconds
const long interval = 60 * 60 * 1000; // 1 hours in milliseconds
const unsigned long npumpInterval = 300000;
unsigned long previousMillis = 0;
unsigned long preMillis = 0;
unsigned long prevMillis = 0;
bool airpumpState = LOW; // relay is initially off
BlynkTimer timer;
WiFiClient espClient;
// Water pump control based on TDS
void checkTDS() {
unsigned long currMillis = millis();
if (currMillis - prevMillis >= npumpInterval) {
prevMillis = currMillis;
int tdsValue = analogRead(tdsPin);
float tdsvoltage = tdsValue * (3.3 / 1023.0);
float tds = (133.42 * tdsvoltage * tdsvoltage * tdsvoltage - 255.86 * tdsvoltage * tdsvoltage + 857.39 * tdsvoltage) * 0.5;
Blynk.virtualWrite(V0, tds);
Serial.print("TDS Value: ");
Serial.print(tds);
Serial.println(" ppm");
if (tds < thresholdTDS) {
digitalWrite(npumpPin, HIGH); // Turn on nutrients dosing pump for low TDS
delay(5000);
digitalWrite(npumpPin, LOW);
} else {
digitalWrite(npumpPin, LOW); // Turn off nutrients dosing pump for high TDS
}
}
}
// Fan control based on temperature
void checkTemperature() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V2, temperature);
Serial.print("% Temp: ");
Serial.print(temperature);
Serial.println("°C");
if (temperature > tempFanOn) {
digitalWrite(fanPin, HIGH); // Turn on fan
}
else if (temperature < tempFanOff) {
digitalWrite(fanPin, LOW); // Turn off fan
}
else {
if (millis() - lastLoopTime >= 4 * 60 * 60 * 1000) { // Check every 4 hours
digitalWrite(fanPin, !digitalRead(fanPin)); // Toggle fan
lastLoopTime = millis();
}
}
}
// Notification if distance exceeds threshold
void checkDistance() {
long duration, distance;
digitalWrite(ultraSonicTrig, LOW);
delayMicroseconds(2);
digitalWrite(ultraSonicTrig, HIGH);
delayMicroseconds(10);
digitalWrite(ultraSonicTrig, LOW);
duration = pulseIn(ultraSonicEcho, HIGH);
distance = duration * 0.034 / 2;
Blynk.virtualWrite(V3, distance);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > maxDistance) {
Blynk.logEvent("distance","Water level low! Distance: " + String(distance) + "cm");
}
delay(5000);
}
//Turn on submersible pump with the interval of 5 hrs
void wpump() {
if (currentMillis - previousMillis >= inter) {
previousMillis = currentMillis;
Serial.println("Turning on pump");
digitalWrite(wpumpPin, LOW); // Replace with LOW if using a relay connected to Active-Low output
delay(20000); // Pump run time (20 seconds)
Serial.println("Turning off pump");
digitalWrite(wpumpPin, HIGH); // Replace with HIGH if using a relay connected to Active-Low output
}
delay(500);
}
//Turn on air pump with the interval of 1 hour
void airpump() {
unsigned long curMillis = millis();
if (curMillis - preMillis >= interval) {
preMillis = curMillis;
airpumpState = !airpumpState;
digitalWrite(airpumpPin, airpumpState);
if (airpumpState) {
Serial.println("Air pump is ON");
} else {
Serial.println("Air pump is OFF");
}
}
}
void setup() {
Serial.begin(9600);
dht.begin();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Connect to Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
pinMode(fanPin, OUTPUT);
pinMode(npumpPin, OUTPUT);
pinMode(wpumpPin, OUTPUT);
pinMode(airpumpPin, OUTPUT);
pinMode(ultraSonicTrig, OUTPUT);
pinMode(ultraSonicEcho, INPUT);
pinMode(tdsPin, INPUT);
digitalWrite(npumpPin, LOW);
digitalWrite(wpumpPin, LOW);
digitalWrite(airpumpPin, airpumpState);
}
void loop() {
Blynk.run();
timer.run();
checkTDS();
checkTemperature();
checkDistance();
wpump();
airpump();
}