forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP12E_ThinkSpeak.ino
154 lines (136 loc) · 4.14 KB
/
ESP12E_ThinkSpeak.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
/*
NodeMCU (ESP12E)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-10-07 13:00
https://www.facebook.com/francefu
*/
#include <ESP8266WiFi.h> //ESP12E
const char* ssid = "xxxxx"; //your network SSID
const char* password = "xxxxx"; //your network password
void setup()
{
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
delay(1000);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if ((StartTime+10000) < millis()) break;
}
if (WiFi.status() == WL_CONNECTED)
{
pinMode(2, OUTPUT);
for (int i=0;i<5;i++)
{
digitalWrite(2,LOW);
delay(100);
digitalWrite(2,HIGH);
delay(100);
}
Serial.println();
Serial.println("STAIP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
else
Serial.println("Unable to connect!");
}
void loop()
{
String request ="";
//Update a Channel Feed
int temperature = random(100);
int humidity = random(100);
request = "/update?api_key=UOJGVW3F98OFF5MB&field1="+String(temperature)+"&field2="+String(humidity);
tcp("api.thingspeak.com",request,80,0);
//Get a Channel Field
request = "/channels/463224/feeds.json?results=1";
String response = tcp("api.thingspeak.com",request,80,1);
//Serial.println(response);
String data_csv = transJSONtoCSV(response);
Serial.println(data_csv);
String field1 = getField(data_csv,"field1");
Serial.println("field1="+field1);
String field2 = getField(data_csv,"field2");
Serial.println("field2="+field2);
String field3 = getField(data_csv,"field3");
Serial.println("field3="+field3);
String field4 = getField(data_csv,"field4");
Serial.println("field4="+field4);
String field5 = getField(data_csv,"field5");
Serial.println("field5="+field5);
String field6 = getField(data_csv,"field6");
Serial.println("field6="+field6);
String field7 = getField(data_csv,"field7");
Serial.println("field7="+field7);
String field8 = getField(data_csv,"field8");
Serial.println("field8="+field8);
Serial.println();
delay(15000);
}
String tcp(String domain,String request,int port,byte wait)
{
WiFiClient client_tcp;
if (client_tcp.connect(domain, port))
{
Serial.println("GET " + request);
client_tcp.println("GET " + request + " HTTP/1.1");
client_tcp.println("Host: " + domain);
client_tcp.println("Connection: close");
client_tcp.println();
String getResponse="",Feedback="";
boolean state = false;
int waitTime = 3000; // timeout 3 seconds
long startTime = millis();
while ((startTime + waitTime) > millis())
{
while (client_tcp.available())
{
char c = client_tcp.read();
if (state==true) Feedback += String(c);
if (c == '\n')
{
if (getResponse.length()==0) state=true;
getResponse = "";
}
else if (c != '\r')
getResponse += String(c);
if (wait==1)
startTime = millis();
}
if (wait==0)
if ((state==true)&&(Feedback.length()!= 0)) break;
}
client_tcp.stop();
return Feedback;
}
else
return "Connection failed";
}
String transJSONtoCSV(String data)
{
int s = data.indexOf("feeds");
int e = data.indexOf("}]}");
if ((s!=-1)&&(e!=-1)) {
String data_sub = data.substring(s+9,e);
data_sub.replace("\":","\",");
data_sub.replace("\"","");
data_sub.replace("},{","\n");
return data_sub;
}
else
return "";
}
String getField(String data, String field) {
if (data.lastIndexOf(field+",")!=-1) {
String data_sub = data.substring(data.lastIndexOf(field+",")+7);
return data_sub.substring(0,data_sub.indexOf(","));
}
else
return "";
}