JsonServer.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. //
  5. // This example shows how to implement an HTTP server that sends a JSON document
  6. // in the response.
  7. // It uses the Ethernet library but can be easily adapted for Wifi.
  8. //
  9. // The JSON document contains the values of the analog and digital pins.
  10. // It looks like that:
  11. // {
  12. // "analog": [0, 76, 123, 158, 192, 205],
  13. // "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
  14. // }
  15. //
  16. // https://arduinojson.org/v7/example/http-server/
  17. #include <ArduinoJson.h>
  18. #include <Ethernet.h>
  19. #include <SPI.h>
  20. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  21. EthernetServer server(80);
  22. void setup() {
  23. // Initialize serial port
  24. Serial.begin(9600);
  25. while (!Serial)
  26. continue;
  27. // Initialize Ethernet libary
  28. if (!Ethernet.begin(mac)) {
  29. Serial.println(F("Failed to initialize Ethernet library"));
  30. return;
  31. }
  32. // Start to listen
  33. server.begin();
  34. Serial.println(F("Server is ready."));
  35. Serial.print(F("Please connect to http://"));
  36. Serial.println(Ethernet.localIP());
  37. }
  38. void loop() {
  39. // Wait for an incomming connection
  40. EthernetClient client = server.available();
  41. // Do we have a client?
  42. if (!client)
  43. return;
  44. Serial.println(F("New client"));
  45. // Read the request (we ignore the content in this example)
  46. while (client.available())
  47. client.read();
  48. // Allocate a temporary JsonDocument
  49. JsonDocument doc;
  50. // Create the "analog" array
  51. JsonArray analogValues = doc["analog"].to<JsonArray>();
  52. for (int pin = 0; pin < 6; pin++) {
  53. // Read the analog input
  54. int value = analogRead(pin);
  55. // Add the value at the end of the array
  56. analogValues.add(value);
  57. }
  58. // Create the "digital" array
  59. JsonArray digitalValues = doc["digital"].to<JsonArray>();
  60. for (int pin = 0; pin < 14; pin++) {
  61. // Read the digital input
  62. int value = digitalRead(pin);
  63. // Add the value at the end of the array
  64. digitalValues.add(value);
  65. }
  66. Serial.print(F("Sending: "));
  67. serializeJson(doc, Serial);
  68. Serial.println();
  69. // Write response headers
  70. client.println(F("HTTP/1.0 200 OK"));
  71. client.println(F("Content-Type: application/json"));
  72. client.println(F("Connection: close"));
  73. client.print(F("Content-Length: "));
  74. client.println(measureJsonPretty(doc));
  75. client.println();
  76. // Write JSON document
  77. serializeJsonPretty(doc, client);
  78. // Disconnect
  79. client.stop();
  80. }
  81. // Performance issue?
  82. // ------------------
  83. //
  84. // EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
  85. // See: https://arduinojson.org/v7/how-to/improve-speed/
  86. // See also
  87. // --------
  88. //
  89. // https://arduinojson.org/ contains the documentation for all the functions
  90. // used above. It also includes an FAQ that will help you solve any
  91. // serialization problem.
  92. //
  93. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  94. // It begins with a simple example, then adds more features like serializing
  95. // directly to a file or an HTTP client.
  96. // Learn more at https://arduinojson.org/book/
  97. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤