Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- // WiFi Parameters
- // WiFi Parameters
- const char* ssid = "TP-LINK_B8AFD8";
- const char* password = "ydMX_v+rYH-L@.#k4a*f'2r$Vm=Zv^&s";
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting...");
- }
- }
- void loop() {
- // Check WiFi Status
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http; //Object of class HTTPClient
- http.begin("http://192.168.1.2/results.json");
- int httpCode = http.GET();
- //Check the returning code
- if (httpCode > 0) {
- // Parsing
- const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(7) + 220;
- DynamicJsonBuffer jsonBuffer(capacity);
- const char* json = "{\"semaphores\":[{\"id\":\"1\",\"status\":\"3\",\"coord_latitude\":\"111\",\"coord_longitude\":\"111\",\"time_red\":\"10\",\"time_yellow\":\"4\",\"time_green\":\"20\"},{\"id\":\"2\",\"status\":\"3\",\"coord_latitude\":\"333\",\"coord_longitude\":\"333\",\"time_red\":\"2\",\"time_yellow\":\"1\",\"time_green\":\"5\"}]}";
- JsonObject& root = jsonBuffer.parseObject(json);
- JsonObject& semaphores_0 = root["semaphores"][0];
- const char* semaphores_0_id = semaphores_0["id"]; // "1"
- const char* semaphores_0_status = semaphores_0["status"]; // "3"
- const char* semaphores_0_coord_latitude = semaphores_0["coord_latitude"]; // "111"
- const char* semaphores_0_coord_longitude = semaphores_0["coord_longitude"]; // "111"
- const char* semaphores_0_time_red = semaphores_0["time_red"]; // "10"
- const char* semaphores_0_time_yellow = semaphores_0["time_yellow"]; // "4"
- const char* semaphores_0_time_green = semaphores_0["time_green"]; // "20"
- JsonObject& semaphores_1 = root["semaphores"][1];
- const char* semaphores_1_id = semaphores_1["id"]; // "2"
- const char* semaphores_1_status = semaphores_1["status"]; // "3"
- const char* semaphores_1_coord_latitude = semaphores_1["coord_latitude"]; // "333"
- const char* semaphores_1_coord_longitude = semaphores_1["coord_longitude"]; // "333"
- const char* semaphores_1_time_red = semaphores_1["time_red"]; // "2"
- const char* semaphores_1_time_yellow = semaphores_1["time_yellow"]; // "1"
- const char* semaphores_1_time_green = semaphores_1["time_green"]; // "5"
- Serial.println(semaphores_0_coord_latitude);
- }
- http.end(); //Close connection
- }
- // Delay
- delay(6000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement