JsonHttpClient.ino 3.2 KB

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