Guest User

Untitled

a guest
Mar 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. {"channel":{"id":421932,"name":"remote_car","description":"Nothing up to this point !","latitude":"0.0","longitude":"0.0","field1":"MOTOR ONE","field2":"MOTOR TWO","field3":"MOTOR THREE","field4":"MOTOR FOUR","created_at":"2018-02-07T17:32:09Z","updated_at":"2018-03-17T07:08:49Z","last_entry_id":7},"feeds":[{"created_at":"2018-03-17T06:53:48Z","entry_id":4,"field1":"0"},{"created_at":"2018-03-17T06:59:22Z","entry_id":5,"field1":"0"},{"created_at":"2018-03-17T07:03:59Z","entry_id":6,"field1":"2"},{"created_at":"2018-03-17T07:08:49Z","entry_id":7,"field1":"1"}]}
  2.  
  3. Parse Failed...
  4.  
  5. #include "WiFiEsp.h"
  6. #include <ArduinoJson.h>
  7. // Emulate Serial1 on pins 6/7 if not present
  8. #ifndef HAVE_HWSERIAL1
  9. #include "SoftwareSerial.h"
  10. SoftwareSerial Serial1(2,3); // RX, TX
  11. #endif
  12. char ssid[] = "M T G"; // your network SSID (name)
  13. char pass[] = "androidGUII"; // your network password
  14. int status = WL_IDLE_STATUS; // the Wifi radio's status
  15. // Initialize the Ethernet client object
  16. WiFiEspClient client;
  17. static const char* host = "api.thingspeak.com";
  18. static const char* apiKey = "H359MYV0P0KFG2VS";
  19. const unsigned long HTTP_TIMEOUT = 10000;
  20.  
  21. void setup()
  22. {
  23. // initialize serial for debugging
  24. Serial.begin(115200);
  25. // initialize serial for ESP module
  26. Serial1.begin(9600);
  27. // initialize ESP module
  28. WiFi.init(&Serial1);
  29.  
  30. // check for the presence of the shield
  31. if (WiFi.status() == WL_NO_SHIELD) {
  32. Serial.println("WiFi shield not present");
  33. // don't continue
  34. while (true);
  35. }
  36.  
  37. // attempt to connect to WiFi network
  38. while ( status != WL_CONNECTED) {
  39. Serial.print("Attempting to connect to WPA SSID: ");
  40. Serial.println(ssid);
  41. // Connect to WPA/WPA2 network
  42. status = WiFi.begin(ssid, pass);
  43. }
  44. // you're connected now, so print out the data
  45. Serial.println("You're connected to the network");
  46. // printWifiStatus();
  47. Serial.println();
  48. }
  49.  
  50. void loop()
  51. {
  52. const int httpPort = 80;
  53. if (!client.connect(host, httpPort)) {
  54. Serial.println("connection failed");
  55. return;
  56. }
  57.  
  58. client.print("GET /channels/421932/fields/1.json?api_key=");
  59. client.print(apiKey);
  60. client.println("&results=4 HTTP/1.1");
  61. client.print("Host:");
  62. client.println(host);
  63. client.println();
  64. while (client.available() == 0);
  65. while (!skipResponseHeaders()); // wait till something comes in; you may want to add a timeout here
  66. while (client.available()) { // Now receive the data
  67. String line = client.readStringUntil('n');
  68. if ( line.indexOf('{',0) >= 0 ) { // Ignore data that is not likely to be JSON formatted, so must contain a '{'
  69. Serial.println(line); // Show the text received
  70. parseData(line);
  71. }
  72. }
  73. client.stop();
  74. delay(10000);
  75. }
  76.  
  77. void parseData(String data){
  78. const size_t bufferSize = JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(12) + 520;
  79. DynamicJsonBuffer jsonBuffer(bufferSize);
  80. const char* json = data.c_str();
  81. JsonObject& root = jsonBuffer.parseObject(json);
  82. if(!root.success()){
  83. Serial.println("Parse Failed...");
  84. }
  85. JsonObject& channel = root["channel"];
  86. long channel_id = channel["id"]; // 421932
  87. const char* channel_name = channel["name"]; // "remote_car"
  88. const char* channel_description = channel["description"]; // "Nothing up to this point !"
  89. Serial.println(channel_name);
  90. Serial.println(channel_description);
  91. }
  92.  
  93. bool skipResponseHeaders() {
  94. char endOfHeaders[] = "rnrn"; // HTTP headers end with an empty line
  95. client.setTimeout(HTTP_TIMEOUT);
  96. bool ok = client.find(endOfHeaders);
  97. if (!ok) { Serial.println("No response or invalid response!"); }
  98. return ok;
  99. }
  100.  
  101. #include <Arduino.h>
  102. #include <avr/pgmspace.h>
  103. #include <ArduinoJson.h>
  104.  
  105. #define stringToParse
  106. "{"channel":{"id":421932,"name":"remote_car","description":"Nothing up to this point !","latitude":"0.0","longitude":"0.0","field1":"MOTOR ONE","field2":"MOTOR TWO","field3":"MOTOR THREE","field4":"MOTOR FOUR","created_at":"2018-02-07T17:32:09Z","updated_at":"2018-03-17T07:08:49Z","last_entry_id":7},"feeds":[{"created_at":"2018-03-17T06:53:48Z","entry_id":4,"field1":"0"},{"created_at":"2018-03-17T06:59:22Z","entry_id":5,"field1":"0"},{"created_at":"2018-03-17T07:03:59Z","entry_id":6,"field1":"2"},{"created_at":"2018-03-17T07:08:49Z","entry_id":7,"field1":"1"}]}"
  107.  
  108. StaticJsonBuffer<785> jsonBuffer;
  109. void parseData(const __FlashStringHelper* json){
  110. //const size_t bufferSize = JSON_ARRAY_SIZE(4) + JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(12) + 520;
  111. //DynamicJsonBuffer jsonBuffer(bufferSize);
  112. //const char* json = data.c_str();
  113. JsonObject& root = jsonBuffer.parseObject(json);
  114. if(!root.success()){
  115. Serial.println("Parse Failed...");
  116. return;
  117. }
  118. JsonObject& channel = root["channel"];
  119. long channel_id = channel["id"]; // 421932
  120. const char* channel_name = channel["name"]; // "remote_car"
  121. const char* channel_description = channel["description"]; // "Nothing up to this point !"
  122. Serial.print("Channel name: "); Serial.println(channel_name);
  123. Serial.print("Channel description: "); Serial.println(channel_description);
  124. Serial.print("Channel id: ");
  125. Serial.println(channel_id);
  126. }
  127.  
  128. void setup() {
  129. Serial.begin(9600);
  130. //We use the F() macro to create a flash string helper
  131. //this way, that huge JSON string is held in FLASH and not in precious RAM..
  132. parseData(F(stringToParse));
  133. }
  134.  
  135. void loop() { }
Add Comment
Please, Sign In to add comment