-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart-pot.ino
377 lines (332 loc) · 7.58 KB
/
Smart-pot.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
#include <SoftwareSerial.h>
//channel's thingspeak API key
String apiKey = "HHNBSOP44SPJ5IBB";
// connect 2 to TX of Serial USB
// connect 3 to RX of serial USB
SoftwareSerial ser(2,3);
unsigned long time0; //present time
unsigned long time1; //field1 time
unsigned long time2; //field2 time
unsigned long time3; //field3 time
unsigned long time4; //water pump time
unsigned long time5; //speaker time
#define Temp A2
#define Pump 13
#define Moisture A3
const int read = A5; //Sensor A1 pin to Arduino pin A1
#define DO 4
#define Speaker 5
double temp,data;
char str[]="Mois:";
char con[]="WL:";
char con1[]="WL: 0mm - Empty!";
char con2[]="WL: 0mm to 5mm";
char con3[]="WL: 5mm to 10mm";
char con4[]="WL: 10mm to 15mm";
char con5[]="WL: 15mm to 20mm";
char con6[]="WL: 20mm to 25mm";
char con7[]="WL: 25mm to 30mm";
char con8[]="WL: 30mm to 35mm";
char con9[]="WL: 35mm to 40mm";
int LCD1602_RS=12;
int LCD1602_RW=11;
int LCD1602_EN=10;
int DB[] = { 6, 7, 8, 9};
// this runs once
void setup() {
// enable debug serial
Serial.begin(115200);
// enable software serial
ser.begin(115200);
// reset ESP8266
ser.println("AT+RST");
pinMode(Pump,OUTPUT); //PUMP
pinMode(Speaker,OUTPUT);
digitalWrite(Pump,LOW);
digitalWrite(Speaker,LOW);
int i = 0;
for (i=6; i <= 12; i++)
{
pinMode(i,OUTPUT);
}
delay(100);
LCD_Command_Write(0x28);//2row 5x7
delay(50);
LCD_Command_Write(0x06);
delay(50);
LCD_Command_Write(0x0c);
delay(50);
LCD_Command_Write(0x80);
delay(50);
LCD_Command_Write(0x01);
delay(50);
}
// the loop
void loop() {
float val; //
float dat; //centigrade
float data; //water level _transformed
float value; // water level
float humid; // moisture
val=analogRead(A2);
dat=val*0.488; //centigrade
Serial.print(dat);
Serial.println(" centigrade");
Serial.print("Moisture=");
humid = 1024 - analogRead(Moisture);
Serial.print(humid);
Serial.print("|DO=");
Serial.println(digitalRead(DO));
Serial.print("\n");
value = analogRead(read); //Read data from analog pin and store it to value variable
data = (value/650)*4;
Serial.println(data);
if(data <=1) data = data / 2;
else if(data <=1.5) data = data *0.67;
else if(data <=1.9) data = data *0.79;
else if(data <=2.4) data = data *0.88;
else if(data <=2.7) data = data *0.96;
else if(data <=2.9) data = data *1.03;
else if(data <=3.1) data = data *1.08;
else if(data <=3.4) data = data * 1.21;
Serial.print("Water level: ");
Serial.print(data);
Serial.println(" cm");
delay(1000);
controlSpeaker(data);
pumpWater(humid);
esp_8266_filed1(dat);
esp_8266_filed2(humid);
esp_8266_filed3(data);
}
void esp_8266_filed1(float a) //centi moisture water_level
{
time0 = millis();
if(time0 - time1 >= 15000){
time1 = time0;
}
else{
return;
}
// convert to string
char buf[32];
String strVolt = dtostrf( a, 2, 1, buf);
Serial.print(a);
Serial.println(" Centigrade");
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
//strVrepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strVolt);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
}
void esp_8266_filed2(float a) //centi moisture water_level
{
time0 = millis();
if(time0 - time2 >= 15000){
time2 = time0;
}
else{
return;
}
// convert to string
char buf[32];
String strVolt = dtostrf( a, 2, 1, buf);
Serial.print(a);
Serial.println(" Moisture");
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
//strVrepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field2=";
getStr += String(strVolt);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
}
//hingspeak needs 15 sec delay between updates
void esp_8266_filed3(float a) //centi moisture water_level
{
time0 = millis();
if(time0 - time3 >= 15000){
time3 = time0;
}
else{
return;
}
// convert to string
char buf[32];
String strVolt = dtostrf( a, 2, 1, buf);
Serial.print(a);
Serial.println(" mm");
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
//strVrepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field3=";
getStr += String(strVolt);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
}
void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 9; i++)
{
digitalWrite(i,temp & 0x80);
temp <<= 1;
}
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
void LCD_SET_XY( int x, int y )
{
int address;
if (y ==0) address = 0x80 + x;
else address = 0xC0 + x;
LCD_Command_Write(address);
}
void LCD_Write_Char( int x,int y,int dat)
{
LCD_SET_XY( x, y );
LCD_Data_Write(dat);
}
void LCD_Write_String(int X,int Y,char *s)
{
LCD_SET_XY( X, Y ); //设置地址
while (*s) //写字符串
{
LCD_Data_Write(*s);
s ++;
}
}
void pumpWater(float a)
{
time0 = millis();
if(time0 - time4 >= 10000){
time4 = time0;
}
else{
return;
}
if(int(a) < 251) //moisture
{
Serial.println("No water in soil!");
digitalWrite(13,HIGH);
delay(2500);
digitalWrite(13,LOW);
}
}
void controlSpeaker( float wLevel)
{
time0 = millis();
if(time0 - time5 >= 10000){
time5 = time0;
}
else{
return;
}
if(wLevel <= 0.5){
Serial.println("Add water!");
digitalWrite(5,HIGH);
delay(4000);
digitalWrite(5,LOW);
}
}