JsonServer.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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) return;
  42. Serial.println(F("New client"));
  43. // Read the request (we ignore the content in this example)
  44. while (client.available()) client.read();
  45. // Allocate a temporary JsonDocument
  46. // Use arduinojson.org/v6/assistant to compute the capacity.
  47. StaticJsonDocument<500> doc;
  48. // Create the "analog" array
  49. JsonArray analogValues = doc.createNestedArray("analog");
  50. for (int pin = 0; pin < 6; pin++) {
  51. // Read the analog input
  52. int value = analogRead(pin);
  53. // Add the value at the end of the array
  54. analogValues.add(value);
  55. }
  56. // Create the "digital" array
  57. JsonArray digitalValues = doc.createNestedArray("digital");
  58. for (int pin = 0; pin < 14; pin++) {
  59. // Read the digital input
  60. int value = digitalRead(pin);
  61. // Add the value at the end of the array
  62. digitalValues.add(value);
  63. }
  64. Serial.print(F("Sending: "));
  65. serializeJson(doc, Serial);
  66. Serial.println();
  67. // Write response headers
  68. client.println(F("HTTP/1.0 200 OK"));
  69. client.println(F("Content-Type: application/json"));
  70. client.println(F("Connection: close"));
  71. client.print(F("Content-Length: "));
  72. client.println(measureJsonPretty(doc));
  73. client.println();
  74. // Write JSON document
  75. serializeJsonPretty(doc, client);
  76. // Disconnect
  77. client.stop();
  78. }
  79. // See also
  80. // --------
  81. //
  82. // https://arduinojson.org/ contains the documentation for all the functions
  83. // used above. It also includes an FAQ that will help you solve any
  84. // serialization problem.
  85. //
  86. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  87. // It begins with a simple example, then adds more features like serializing
  88. // directly to a file or an HTTP client.
  89. // Learn more at https://arduinojson.org/book/
  90. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤