-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplantWateringSystem.ino
More file actions
323 lines (282 loc) · 9.69 KB
/
plantWateringSystem.ino
File metadata and controls
323 lines (282 loc) · 9.69 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
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
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <String.h>
#define DEBUG true
int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int val;
float calibrationFactor=4.5;
volatile byte pulseCount;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
int waterOrNot;
int plantNumber;
void setup()
{
Serial.begin(9600);
Serial3.begin(115200); //Serial port for the ESP8266
//Initializing the LED pins as OUTPUT ports
pinMode(ledPin1 , OUTPUT);
pinMode(ledPin2 , OUTPUT);
pinMode(ledPin3 , OUTPUT);
//Initiallizing the pins for moisture sensors as INPUT ports
pinMode(A13 , INPUT);
pinMode(A14 , INPUT);
pinMode(A15 , INPUT);
//Initializing the pins for relays as OUTPUT ports
pinMode(22,OUTPUT); //pump
pinMode(11,OUTPUT); //valve
digitalWrite(11,LOW);
pinMode(12,OUTPUT); //valve
digitalWrite(12,LOW);
pinMode(13,OUTPUT); //valve
digitalWrite(13,LOW);
//Initializing the pins for waterflow sensors as INPUT portss
pinMode(18, INPUT);
digitalWrite(18, HIGH);
pinMode(19, INPUT);
digitalWrite(19, HIGH);
pinMode(20, INPUT);
digitalWrite(20, HIGH);
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CWJAP=\"Moto G (4) 8350\",\"shravi27\"\r\n",3000,DEBUG); //connect to the specified wifi network
delay(10000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on TCP server on port 80
Serial.println("Server ready");
}
void loop() {
// put your main code here, to run repeatedly:
String val="";
while(Serial3.available()>0) //read the value on ESP8266
{
char c= Serial3.read();
val+=c;
delay(10);
}
int findNumber=val.indexOf("="); //find = in the string, if found that means relevant data has been read
if(findNumber!=-1) //perform operations if relevant data is read
{
int connectionId=0;
//assign the plant number and check if user has to water or just see details according to incoming string
if(val.substring(findNumber+1,findNumber+2)=="0") //plant 1
plantNumber=1;
else if(val.substring(findNumber+1,findNumber+2)=="5") //plant 2
plantNumber=2;
else if(val.substring(findNumber+1,findNumber+2)=="9") //plant 3
plantNumber=3;
if(val.substring(findNumber+2,findNumber+3)=="Y") //water
waterOrNot=1;
else if(val.substring(findNumber+2,findNumber+3)=="N") //details
waterOrNot=0;
Serial.println(plantNumber);
Serial.println(waterOrNot);
String content;
boolean flag=true;
if(flag)
{
int valvePin=plantNumber+10;
int soilMoisturePin=plantNumber+12;
int waterFlowPin=plantNumber+17;
int temperaturePin=2*(plantNumber+11);
//Initialization for waterflow sensor
pulseCount=0;
flowRate=0.0;
flowMilliLitres=0;
totalMilliLitres=0;
oldTime=0;
int moistureVal; //variable storing moisture content
double temperatureVal; //variable storing soil temperature
attachInterrupt(digitalPinToInterrupt(temperaturePin), pulseCounter, FALLING); //Interrupt for waterflow sensor
moistureVal=soilMoisture(soilMoisturePin); //extracting moisture content
temperatureVal=temperatureSensor(temperaturePin); //extracting temperature values
if(moistureVal>=800 && waterOrNot==1) //checking if plant needs water
{
digitalWrite(valvePin,HIGH); //opening valve
delay(1000);
digitalWrite(22,HIGH); //opening pump
int val1=moistureVal;
Serial.println(val1);
while(val1>800) //watering till value reaches normal state
{
val1=soilMoisture(soilMoisturePin);
waterFlowSensor(waterFlowPin); //keeping track of water content
}
digitalWrite(22,LOW); //closing the pump
digitalWrite(valvePin,LOW); //closing the valve
}
moistureVal=map(moistureVal,300,1023,100,0);
content += moistureVal;
content += "% moisture content ";
content += temperatureVal;
content += "°C soil temperature ";
if(waterOrNot==1) //if plant is watered, tell how much water was given
{
content+="Amount of water given (in mL) ";
content+=totalMilliLitres;
}
}
sendHTTPResponse(connectionId,content); //send the response
// make close command
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";
sendCommand(closeCommand,1000,DEBUG); // close connection
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module so that the data gets reset
sendCommand("AT+CWMODE=3\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on TCP server again on port 80
Serial.println("Server ready");
}
}
int soilMoisture(int analogPin)
{
val = analogRead(analogPin); //read value from moisture sensor
Serial.println(val);
//check what condition the soil is in right now and accordingly light up corresponding alert LED
if (val<300) //overflow condition
{
digitalWrite(ledPin1, HIGH);
Serial.println("Overflow");
}
if (val>300 && val<=800) //normal condition
{
digitalWrite(ledPin2, HIGH);
Serial.println("Normal Condition");
}
if (val>=800) //dry condition
{
digitalWrite(ledPin3, HIGH);
Serial.println("Dry Condition");
}
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
return val;
}
double temperatureSensor(int analogPin)
{
OneWire oneWire(analogPin);
DallasTemperature sensors(&oneWire);
sensors.begin();
sensors.requestTemperatures();
delay(1000);
return sensors.getTempCByIndex(0);
}
void waterFlowSensor(int pin)
{
if((millis() - oldTime) > 1000)
{
detachInterrupt(digitalPinToInterrupt(pin));
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
unsigned int frac;
Serial.print("Flow rate: ");
frac = (flowRate - int(flowRate)) * 10;
Serial.print(" Current Liquid Flowing: "); // Output separator
Serial.print(flowMilliLitres);
Serial.print("mL/Sec");
pulseCount = 0;
attachInterrupt(digitalPinToInterrupt(pin), pulseCounter, FALLING);
}
}
void pulseCounter()
{
pulseCount++;
}
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
int dataSize = command.length();
char data[dataSize];
command.toCharArray(data,dataSize);
Serial3.write(data,dataSize); // send the read character to the esp8266
if(debug)
{
Serial.println("\r\n====== HTTP Response From Arduino ======");
Serial.write(data,dataSize);
Serial.println("\r\n========================================");
}
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial3.available())
{
// The esp has data so display its output to the serial window
char c = Serial3.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
/*
* Name: sendHTTPResponse
* Description: Function that sends HTTP 200, HTML UTF-8 response
*/
void sendHTTPResponse(int connectionId, String content)
{
// build HTTP response
String httpResponse;
String httpHeader;
// HTTP Header
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length: ";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader +="Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
sendCIPData(connectionId,httpResponse);
}
/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(int connectionId, String data)
{
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=data.length();
cipSend +="\r\n";
sendCommand(cipSend,1000,DEBUG);
sendData(data,1000,DEBUG);
}
/*
* Name: sendCommand
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendCommand(String command, const int timeout, boolean debug){
String response = "";
Serial3.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial3.available())
{
// The esp has data so display its output to the serial window
char c = Serial3.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}