JsonServer.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 JSON document
  6. // in the responses.
  7. // It uses the Ethernet library but can be easily adapted for Wifi.
  8. //
  9. // It sends the value of the analog and digital pins.
  10. // The JSON document looks like the following:
  11. // {
  12. // "analog": [ 0, 1, 2, 3, 4, 5 ],
  13. // "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
  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 JsonBuffer
  44. // Use arduinojson.org/assistant to compute the capacity.
  45. StaticJsonBuffer<500> jsonBuffer;
  46. // Create the root object
  47. JsonObject& root = jsonBuffer.createObject();
  48. // Create the "analog" array
  49. JsonArray& analogValues = root.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 = root.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. root.printTo(Serial);
  66. Serial.println();
  67. // Write response headers
  68. client.println("HTTP/1.0 200 OK");
  69. client.println("Content-Type: application/json");
  70. client.println("Connection: close");
  71. client.println();
  72. // Write JSON document
  73. root.prettyPrintTo(client);
  74. // Disconnect
  75. client.stop();
  76. }
  77. // See also
  78. // --------
  79. //
  80. // https://arduinojson.org/ contains the documentation for all the functions
  81. // used above. It also includes an FAQ that will help you solve any
  82. // serialization problem.
  83. //
  84. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  85. // It begins with a simple example, then adds more features like serializing
  86. // directly to a file or an HTTP client.
  87. // Learn more at https://arduinojson.org/book/
  88. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤