JsonHttpClient.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. //
  19. // https://arduinojson.org/v6/example/http-client/
  20. #include <ArduinoJson.h>
  21. #include <Ethernet.h>
  22. #include <SPI.h>
  23. void setup() {
  24. // Initialize Serial port
  25. Serial.begin(9600);
  26. while (!Serial) continue;
  27. // Initialize Ethernet library
  28. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  29. if (!Ethernet.begin(mac)) {
  30. Serial.println(F("Failed to configure Ethernet"));
  31. return;
  32. }
  33. delay(1000);
  34. Serial.println(F("Connecting..."));
  35. // Connect to HTTP server
  36. EthernetClient client;
  37. client.setTimeout(10000);
  38. if (!client.connect("arduinojson.org", 80)) {
  39. Serial.println(F("Connection failed"));
  40. return;
  41. }
  42. Serial.println(F("Connected!"));
  43. // Send HTTP request
  44. client.println(F("GET /example.json HTTP/1.0"));
  45. client.println(F("Host: arduinojson.org"));
  46. client.println(F("Connection: close"));
  47. if (client.println() == 0) {
  48. Serial.println(F("Failed to send request"));
  49. return;
  50. }
  51. // Check HTTP status
  52. char status[32] = {0};
  53. client.readBytesUntil('\r', status, sizeof(status));
  54. // It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
  55. if (strcmp(status + 9, "200 OK") != 0) {
  56. Serial.print(F("Unexpected response: "));
  57. Serial.println(status);
  58. return;
  59. }
  60. // Skip HTTP headers
  61. char endOfHeaders[] = "\r\n\r\n";
  62. if (!client.find(endOfHeaders)) {
  63. Serial.println(F("Invalid response"));
  64. return;
  65. }
  66. // Allocate the JSON document
  67. // Use arduinojson.org/v6/assistant to compute the capacity.
  68. const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
  69. DynamicJsonDocument doc(capacity);
  70. // Parse JSON object
  71. DeserializationError error = deserializeJson(doc, client);
  72. if (error) {
  73. Serial.print(F("deserializeJson() failed: "));
  74. Serial.println(error.c_str());
  75. return;
  76. }
  77. // Extract values
  78. Serial.println(F("Response:"));
  79. Serial.println(doc["sensor"].as<char*>());
  80. Serial.println(doc["time"].as<long>());
  81. Serial.println(doc["data"][0].as<float>(), 6);
  82. Serial.println(doc["data"][1].as<float>(), 6);
  83. // Disconnect
  84. client.stop();
  85. }
  86. void loop() {
  87. // not used in this example
  88. }
  89. // See also
  90. // --------
  91. //
  92. // https://arduinojson.org/ contains the documentation for all the functions
  93. // used above. It also includes an FAQ that will help you solve any
  94. // serialization problem.
  95. //
  96. // The book "Mastering ArduinoJson" contains a tutorial on deserialization
  97. // showing how to parse the response from GitHub's API. In the last chapter,
  98. // it shows how to parse the huge documents from OpenWeatherMap
  99. // and Reddit.
  100. // Learn more at https://arduinojson.org/book/
  101. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤