-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266_request.ino
49 lines (35 loc) · 957 Bytes
/
esp8266_request.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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "<YOUR_SSID>";
const char* password = "<YOUR_PASSWORD>";
HTTPClient http;
void setup() {
Serial.begin(115200);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
String payload = get_request("http://192.168.0.12:5000/data");
Serial.println(payload);
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
const char* train = root["traintime"];
Serial.println(train);
Serial.println("==========");
delay(5000);
}
String get_request(String url) {
http.begin(url);
int httpCode = http.GET();
String payload = "FAILED";
if (httpCode > 0) { //Check the returning code
payload = http.getString(); //Get the request response payload
}
http.end();
return payload;
}