Advertisement
Guest User

json problems

a guest
Apr 12th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. StaticJsonBuffer<200> jsonBuffer;
  3. /*
  4. * Simple HTTP get webclient test
  5. */
  6.  
  7. #include <ESP8266WiFi.h>
  8. char json[] = {0};
  9. char x[] = {0};
  10. JsonArray& root = jsonBuffer.parseArray(json);
  11. const char* ssid = "lehigh-guest";
  12. const char* password = "";
  13. #define MAX_JSON_SIZE 256
  14. const char* host = "api.thingspeak.com";
  15.  
  16. String field1;
  17. String field2;
  18. void setup() {
  19. Serial.begin(115200);
  20. delay(100);
  21. StaticJsonBuffer<200> jsonBuffer;
  22. // We start by connecting to a WiFi network
  23. char json[] = {0};
  24.  
  25.  
  26. WiFi.begin(ssid, password);
  27.  
  28. while (WiFi.status() != WL_CONNECTED) {
  29. delay(500);
  30. }
  31. }
  32.  
  33. int value = 0;
  34.  
  35. void loop() {
  36. char json[256] = {0u};
  37. delay(5000);
  38.  
  39.  
  40. ++value;
  41.  
  42.  
  43. // Use WiFiClient class to create TCP connections
  44. WiFiClient client;
  45. const int httpPort = 80;
  46. if (!client.connect(host, httpPort)) {
  47. return;
  48. }
  49.  
  50. // We now create a URI for the request
  51. String url = "/channels/103752/feeds/last";
  52.  
  53.  
  54. // This will send the request to the server
  55. client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  56. "Host: " + host + "\r\n" +
  57. "Connection: close\r\n\r\n");
  58. delay(500);
  59.  
  60. // Read all the lines of the reply from server and print them to Serial
  61. while(client.available()){
  62. String line = client.readStringUntil('\r\n\r\n');
  63. line.toCharArray (x,256);
  64. Serial.print(line);
  65.  
  66. char json = x[256];
  67. JsonArray& root = jsonBuffer.parseArray(json);
  68. if (!root.success()){
  69. Serial.println("parseObject() failed");
  70. return;
  71. }
  72. String field1 = root["field1"];
  73. String field2 = root["field2"];
  74. Serial.println(field1);
  75. Serial.println(field2);
  76.  
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement