JsonHttpClient.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2017
  3. // MIT License
  4. //
  5. // This example shows how to parse a JSON document in an HTTP response.
  6. // It uses the Ethernet library, but can be easily adapted for Wifi.
  7. //
  8. // It performs a GET resquest on arduinojson.org/example.json
  9. // Here is the expected response:
  10. // {
  11. // "sensor": "gps",
  12. // "time": 1351824120,
  13. // "data": [
  14. // 48.756080,
  15. // 2.302038
  16. // ]
  17. // }
  18. #include <ArduinoJson.h>
  19. #include <Ethernet.h>
  20. #include <SPI.h>
  21. void setup() {
  22. // Initialize Serial port
  23. Serial.begin(9600);
  24. while (!Serial) continue;
  25. // Initialize Ethernet library
  26. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  27. if (!Ethernet.begin(mac)) {
  28. Serial.println(F("Failed to configure Ethernet"));
  29. return;
  30. }
  31. delay(1000);
  32. Serial.println(F("Connecting..."));
  33. // Connect to HTTP server
  34. EthernetClient client;
  35. client.setTimeout(10000);
  36. if (!client.connect("arduinojson.org", 80)) {
  37. Serial.println(F("Connection failed"));
  38. return;
  39. }
  40. Serial.println(F("Connected!"));
  41. // Send HTTP request
  42. client.println(F("GET /example.json HTTP/1.0"));
  43. client.println(F("Host: arduinojson.org"));
  44. client.println(F("Connection: close"));
  45. if (client.println() == 0) {
  46. Serial.println(F("Failed to send request"));
  47. return;
  48. }
  49. // Check HTTP status
  50. char status[32] = {0};
  51. client.readBytesUntil('\r', status, sizeof(status));
  52. if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
  53. Serial.print(F("Unexpected response: "));
  54. Serial.println(status);
  55. return;
  56. }
  57. // Skip HTTP headers
  58. char endOfHeaders[] = "\r\n\r\n";
  59. if (!client.find(endOfHeaders)) {
  60. Serial.println(F("Invalid response"));
  61. return;
  62. }
  63. // Allocate JsonBuffer
  64. // Use arduinojson.org/assistant to compute the capacity.
  65. const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
  66. DynamicJsonBuffer jsonBuffer(capacity);
  67. // Parse JSON object
  68. JsonObject& root = jsonBuffer.parseObject(client);
  69. if (!root.success()) {
  70. Serial.println(F("Parsing failed!"));
  71. return;
  72. }
  73. // Extract values
  74. Serial.println(F("Response:"));
  75. Serial.println(root["sensor"].as<char*>());
  76. Serial.println(root["time"].as<char*>());
  77. Serial.println(root["data"][0].as<char*>());
  78. Serial.println(root["data"][1].as<char*>());
  79. // Disconnect
  80. client.stop();
  81. }
  82. void loop() {
  83. // not used in this example
  84. }
  85. // See also
  86. // --------
  87. //
  88. // The website arduinojson.org contains the documentation for all the functions
  89. // used above. It also includes an FAQ that will help you solve any
  90. // serialization problem.
  91. // Please check it out at: https://arduinojson.org/
  92. //
  93. // The book "Mastering ArduinoJson" contains a tutorial on deserialization
  94. // showing how to parse the response from Yahoo Weather. In the last chapter,
  95. // it shows how to parse the huge documents from OpenWeatherMap
  96. // and Weather Underground.
  97. // Please check it out at: https://leanpub.com/arduinojson/