| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- // ArduinoJson - arduinojson.org
- // Copyright Benoit Blanchon 2014-2018
- // MIT License
- //
- // This example shows how to implement an HTTP server that sends a JSON document
- // in the response.
- // It uses the Ethernet library but can be easily adapted for Wifi.
- //
- // The JSON document contains the values of the analog and digital pins.
- // It looks like that:
- // {
- // "analog": [0, 76, 123, 158, 192, 205],
- // "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
- // }
- #include <ArduinoJson.h>
- #include <Ethernet.h>
- #include <SPI.h>
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- EthernetServer server(80);
- void setup() {
- // Initialize serial port
- Serial.begin(9600);
- while (!Serial) continue;
- // Initialize Ethernet libary
- if (!Ethernet.begin(mac)) {
- Serial.println(F("Failed to initialize Ethernet library"));
- return;
- }
- // Start to listen
- server.begin();
- Serial.println(F("Server is ready."));
- Serial.print(F("Please connect to http://"));
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // Wait for an incomming connection
- EthernetClient client = server.available();
- // Do we have a client?
- if (!client) return;
- Serial.println(F("New client"));
- // Read the request (we ignore the content in this example)
- while (client.available()) client.read();
- // Allocate a temporary JsonDocument
- // Use arduinojson.org/v6/assistant to compute the capacity.
- StaticJsonDocument<500> doc;
- // Create the "analog" array
- JsonArray analogValues = doc.createNestedArray("analog");
- for (int pin = 0; pin < 6; pin++) {
- // Read the analog input
- int value = analogRead(pin);
- // Add the value at the end of the array
- analogValues.add(value);
- }
- // Create the "digital" array
- JsonArray digitalValues = doc.createNestedArray("digital");
- for (int pin = 0; pin < 14; pin++) {
- // Read the digital input
- int value = digitalRead(pin);
- // Add the value at the end of the array
- digitalValues.add(value);
- }
- Serial.print(F("Sending: "));
- serializeJson(doc, Serial);
- Serial.println();
- // Write response headers
- client.println(F("HTTP/1.0 200 OK"));
- client.println(F("Content-Type: application/json"));
- client.println(F("Connection: close"));
- client.print(F("Content-Length: "));
- client.println(measureJsonPretty(doc));
- client.println();
- // Write JSON document
- serializeJsonPretty(doc, client);
- // Disconnect
- client.stop();
- }
- // Visit https://arduinojson.org/v6/example/http-server/ for more.
|