-
Notifications
You must be signed in to change notification settings - Fork 0
/
soil_humidity_meter.ino
166 lines (151 loc) · 4.66 KB
/
soil_humidity_meter.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
#include<LiquidCrystal.h>
#include <SoftwareSerial.h>
int buzzerPin = 8; // Buzzer pin
int R = 11; // Red pin of RGB led
int G = 12; // Green pin of RGB led
int B = 13; // Blue pin of RGB led
int rX = 9;
int tX = 10;
int nemDegeri; //Current readed humidity
int nemSensorPin = A0; // humidity sensor pin
#define VOLTAGE 1
String wifiName = "SOILHM";
String wifiPassword = "7e8r9GH6D2FF32T";
String deviceId = "7e8r9GH6D2FF32T";
String apiIp = "13.38.214.178";
String ipGetterApiIp = "3.220.57.224";
String deviceIp;
LiquidCrystal lcd(7,6,5,4,3,2); //initiliaze lcd screen
void loadColors();
void lightLed(int r, int g, int b);
void makeSound(int delayTime);
void stopSound(int delayTime);
String getIp();
SoftwareSerial esp(rX, tX);
void setup()
{
Serial.begin(9600);
Serial.println("Started");
esp.begin(115200);
esp.println("AT");
Serial.println("AT Yolland?");
while(!esp.find("OK")){
esp.println("AT");
Serial.println("ESP8266 Bulunamad?.");
}
Serial.println("OK Komutu Al?nd?");
esp.println("AT+CWMODE=1");
while(!esp.find("OK")){
esp.println("AT+CWMODE=1");
Serial.println("Ayar Yap?l?yor....");
}
Serial.println("Client olarak ayarland?");
Serial.println("Aga Baglaniliyor...");
esp.println("AT+CWJAP=\""+wifiName+"\",\""+wifiPassword+"\"");
while(!esp.find("OK")); //A?a ba?lanana kadar bekliyoruz.
Serial.println("Aga Baglandi.");
delay(1000);
//set up pins
pinMode(nemSensorPin,INPUT);
pinMode(buzzerPin,OUTPUT);
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
lcd.begin(16,2); //start lcd screen
lcd.print("Loading...");
loadColors();
deviceIp = getIp();
}
void loop() {
esp.println("AT+CIPSTART=\"TCP\",\""+apiIp+"\",80");
if(esp.find("Error")){
Serial.println("AT+CIPSTART Error");
}
String veri = "POST https://shmb.eu-west-3.elasticbeanstalk.com/measurements?deviceId="+deviceId;
veri+="&humidity="+String(nemDegeri);
veri += "&ip="+deviceIp;
veri += "\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(veri.length()+2);
delay(2000);
if(esp.find(">")){ //ESP8266 haz?r oldu?unda i�indeki komutlar �al???yor.
esp.print(veri); //Veriyi g�nderiyoruz.
Serial.println(veri);
Serial.println("Veri gonderildi.");
delay(1000);
}
Serial.println("Baglant? Kapatildi.");
esp.println("AT+CIPCLOSE");
nemDegeri = analogRead(nemSensorPin);
nemDegeri = map(nemDegeri,0,1023,0,100); //map humidity to range of 0 100
nemDegeri = 100 - nemDegeri; //map it to per cent
lcd.clear(); //clear lcd screen
char buffer [16]; //create a buffer to write in a lcd screen
sprintf (buffer, " %c %d",'%',nemDegeri); //set up humidity percent
lcd.print("Toprak Nem Yuzde"); //print header
lcd.setCursor(0,1); //go to new line
lcd.print(buffer); //print humidity per cent
if(nemDegeri>60){ //if percent above 60 it means plant not needs any water
lightLed(0,255,255); //red color
makeSound(100);
}
else if(nemDegeri>=50 && nemDegeri<=60){ // %50-%60 is good for plants
lightLed(255,0,255); //green color
makeSound(1000); //make a calmer sound
}
else{
lightLed(255,255,0); //blue color
stopSound(200); //and not sound buzzer
}
}
String getIp(){
esp.println("AT+CIPSTART=\"TCP\",\""+ipGetterApiIp+"\",80");
if(esp.find("Error")){
Serial.println("AT+CIPSTART Error");
}
String veri = "GET http://api.ipify.org/";
veri += "\r\n\r\n";
esp.print("AT+CIPSEND=");
esp.println(veri.length()+2);
delay(2000);
if(esp.find(">")){
esp.print(veri);
Serial.println(veri);
Serial.println("Veri gonderildi.");
delay(1000);
Serial.println(esp.read());
String ip = esp.readString();
Serial.println("Baglant? Kapatildi.");
esp.println("AT+CIPCLOSE");
return ip;
}
Serial.println("Baglant? Kapatildi.");
esp.println("AT+CIPCLOSE");
return "";
}
void stopSound(int delayTime){
digitalWrite(buzzerPin,!VOLTAGE);
delay(delayTime);
}
void lightLed(int r, int g, int b){
analogWrite(R,r);
analogWrite(G,g);
analogWrite(B,b);
}
void makeSound(int delayTime){
digitalWrite(buzzerPin,VOLTAGE);
delay(delayTime);
digitalWrite(buzzerPin,!VOLTAGE);
delay(delayTime);
}
void loadColors(){
for(int r=0; r<=3; r++){
for(int g=0; g<=3; g++){
for(int b=0; b<=3; b++){
lightLed(r*85,g*85,b*85);
delay(30);
}
}
}
lightLed(0,0,0);
}