JsonServer.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, 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/v6/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) continue;
  26. // Initialize Ethernet libary
  27. if (!Ethernet.begin(mac)) {
  28. Serial.println(F("Failed to initialize Ethernet library"));
  29. return;
  30. }
  31. // Start to listen
  32. server.begin();
  33. Serial.println(F("Server is ready."));
  34. Serial.print(F("Please connect to http://"));
  35. Serial.println(Ethernet.localIP());
  36. }
  37. void loop() {
  38. // Wait for an incomming connection
  39. EthernetClient client = server.available();
  40. // Do we have a client?
  41. if (!client)
  42. return;
  43. Serial.println(F("New client"));
  44. // Read the request (we ignore the content in this example)
  45. while (client.available()) client.read();
  46. // Allocate a temporary JsonDocument
  47. // Use https://arduinojson.org/v6/assistant to compute the capacity.
  48. StaticJsonDocument<500> doc;
  49. // Create the "analog" array
  50. JsonArray analogValues = doc.createNestedArray("analog");
  51. for (int pin = 0; pin < 6; pin++) {
  52. // Read the analog input
  53. int value = analogRead(pin);
  54. // Add the value at the end of the array
  55. analogValues.add(value);
  56. }
  57. // Create the "digital" array
  58. JsonArray digitalValues = doc.createNestedArray("digital");
  59. for (int pin = 0; pin < 14; pin++) {
  60. // Read the digital input
  61. int value = digitalRead(pin);
  62. // Add the value at the end of the array
  63. digitalValues.add(value);
  64. }
  65. Serial.print(F("Sending: "));
  66. serializeJson(doc, Serial);
  67. Serial.println();
  68. // Write response headers
  69. client.println(F("HTTP/1.0 200 OK"));
  70. client.println(F("Content-Type: application/json"));
  71. client.println(F("Connection: close"));
  72. client.print(F("Content-Length: "));
  73. client.println(measureJsonPretty(doc));
  74. client.println();
  75. // Write JSON document
  76. serializeJsonPretty(doc, client);
  77. // Disconnect
  78. client.stop();
  79. }
  80. // Performance issue?
  81. // ------------------
  82. //
  83. // EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
  84. // See: https://arduinojson.org/v6/how-to/improve-speed/
  85. // See also
  86. // --------
  87. //
  88. // https://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. //
  92. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  93. // It begins with a simple example, then adds more features like serializing
  94. // directly to a file or an HTTP client.
  95. // Learn more at https://arduinojson.org/book/
  96. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤