-
Notifications
You must be signed in to change notification settings - Fork 1
/
soil_sense_project.ino
73 lines (56 loc) · 1.28 KB
/
soil_sense_project.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
#include <SoftwareSerial.h>
int soil;
// replace with your channel's thingspeak API key
String apiKey = "PVKAH015ZZN2I70Z";
// connect 2 to TX of ESP
// connect 3 to RX of ESP
SoftwareSerial ser(2,3); // RX, TX
// this runs once
void setup() {
// enable debug serial
Serial.begin(115200);
// enable software serial
ser.begin(115200);
// reset ESP8266
//ser.println("AT+RST");
}
// the loop
void loop() {
soil = analogRead(A0);
esp_8266();
}
void esp_8266()
{
// convert to string
String soil_send = String(soil);
//Serial.print(soil_send);
// 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;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += soil_send;
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");
}
// thingspeak needs 15 sec delay between updates
delay(16000);
}