SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4.  
  5.  
  6. #include <ESP8266WiFi.h>
  7. #include <ESP8266HTTPClient.h>
  8. #include <ArduinoJson.h>
  9.  
  10. // WiFi Parameters
  11. // WiFi Parameters
  12. const char* ssid = "TP-LINK_B8AFD8";
  13. const char* password = "ydMX_v+rYH-L@.#k4a*f'2r$Vm=Zv^&s";
  14.  
  15. void setup() {
  16.   Serial.begin(115200);
  17.   WiFi.begin(ssid, password);
  18.  
  19.   while (WiFi.status() != WL_CONNECTED) {
  20.     delay(1000);
  21.     Serial.println("Connecting...");
  22.   }
  23. }
  24.  
  25. void loop() {
  26.   // Check WiFi Status
  27.   if (WiFi.status() == WL_CONNECTED) {
  28.     HTTPClient http;  //Object of class HTTPClient
  29.     http.begin("http://192.168.1.2/results.json");
  30.     int httpCode = http.GET();
  31.     //Check the returning code                                                                  
  32.     if (httpCode > 0) {
  33.       // Parsing
  34.       const size_t capacity = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(7) + 220;
  35.       DynamicJsonBuffer jsonBuffer(capacity);
  36.  
  37.       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\"}]}";
  38.  
  39.       JsonObject& root = jsonBuffer.parseObject(json);
  40.  
  41.       JsonObject& semaphores_0 = root["semaphores"][0];
  42.       const char* semaphores_0_id = semaphores_0["id"]; // "1"
  43.       const char* semaphores_0_status = semaphores_0["status"]; // "3"
  44.       const char* semaphores_0_coord_latitude = semaphores_0["coord_latitude"]; // "111"
  45.       const char* semaphores_0_coord_longitude = semaphores_0["coord_longitude"]; // "111"
  46.       const char* semaphores_0_time_red = semaphores_0["time_red"]; // "10"
  47.       const char* semaphores_0_time_yellow = semaphores_0["time_yellow"]; // "4"
  48.       const char* semaphores_0_time_green = semaphores_0["time_green"]; // "20"
  49.  
  50.       JsonObject& semaphores_1 = root["semaphores"][1];
  51.       const char* semaphores_1_id = semaphores_1["id"]; // "2"
  52.       const char* semaphores_1_status = semaphores_1["status"]; // "3"
  53.       const char* semaphores_1_coord_latitude = semaphores_1["coord_latitude"]; // "333"
  54.       const char* semaphores_1_coord_longitude = semaphores_1["coord_longitude"]; // "333"
  55.       const char* semaphores_1_time_red = semaphores_1["time_red"]; // "2"
  56.       const char* semaphores_1_time_yellow = semaphores_1["time_yellow"]; // "1"
  57.       const char* semaphores_1_time_green = semaphores_1["time_green"]; // "5"
  58.       Serial.println(semaphores_0_coord_latitude);
  59.     }
  60.     http.end();   //Close connection
  61.   }
  62.   // Delay
  63.   delay(6000);
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top