JsonServer.ino 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  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. #include <ArduinoJson.h>
  16. #include <Ethernet.h>
  17. #include <SPI.h>
  18. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  19. EthernetServer server(80);
  20. void setup() {
  21. // Initialize serial port
  22. Serial.begin(9600);
  23. while (!Serial) continue;
  24. // Initialize Ethernet libary
  25. if (!Ethernet.begin(mac)) {
  26. Serial.println(F("Failed to initialize Ethernet library"));
  27. return;
  28. }
  29. // Start to listen
  30. server.begin();
  31. Serial.println(F("Server is ready."));
  32. Serial.print(F("Please connect to http://"));
  33. Serial.println(Ethernet.localIP());
  34. }
  35. void loop() {
  36. // Wait for an incomming connection
  37. EthernetClient client = server.available();
  38. // Do we have a client?
  39. if (!client) return;
  40. Serial.println(F("New client"));
  41. // Read the request (we ignore the content in this example)
  42. while (client.available()) client.read();
  43. // Allocate a temporary JsonDocument
  44. // Use arduinojson.org/v6/assistant to compute the capacity.
  45. StaticJsonDocument<500> doc;
  46. // Create the "analog" array
  47. JsonArray analogValues = doc.createNestedArray("analog");
  48. for (int pin = 0; pin < 6; pin++) {
  49. // Read the analog input
  50. int value = analogRead(pin);
  51. // Add the value at the end of the array
  52. analogValues.add(value);
  53. }
  54. // Create the "digital" array
  55. JsonArray digitalValues = doc.createNestedArray("digital");
  56. for (int pin = 0; pin < 14; pin++) {
  57. // Read the digital input
  58. int value = digitalRead(pin);
  59. // Add the value at the end of the array
  60. digitalValues.add(value);
  61. }
  62. Serial.print(F("Sending: "));
  63. serializeJson(doc, Serial);
  64. Serial.println();
  65. // Write response headers
  66. client.println(F("HTTP/1.0 200 OK"));
  67. client.println(F("Content-Type: application/json"));
  68. client.println(F("Connection: close"));
  69. client.print(F("Content-Length: "));
  70. client.println(measureJsonPretty(doc));
  71. client.println();
  72. // Write JSON document
  73. serializeJsonPretty(doc, client);
  74. // Disconnect
  75. client.stop();
  76. }
  77. // Visit https://arduinojson.org/v6/example/http-server/ for more.