Files
ArduinoJson/examples/JsonServer/JsonServer.ino

109 lines
2.9 KiB
Arduino
Raw Normal View History

2017-12-11 15:19:28 +01:00
// ArduinoJson - arduinojson.org
2018-01-05 09:20:01 +01:00
// Copyright Benoit Blanchon 2014-2018
2017-12-11 15:19:28 +01:00
// MIT License
//
// This example shows how to implement an HTTP server that sends JSON document
// in the responses.
2017-12-15 17:52:47 +01:00
// It uses the Ethernet library but can be easily adapted for Wifi.
2017-12-11 15:19:28 +01:00
//
// It sends the value of the analog and digital pins.
// The JSON document looks like the following:
// {
// "analog": [ 0, 1, 2, 3, 4, 5 ],
// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
// }
2015-01-10 15:25:27 +01:00
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
2015-01-10 15:25:27 +01:00
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetServer server(80);
2017-12-11 15:19:28 +01:00
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;
2015-01-10 15:25:27 +01:00
}
2017-12-11 15:19:28 +01:00
// Start to listen
server.begin();
Serial.println(F("Server is ready."));
Serial.print(F("Please connect to http://"));
Serial.println(Ethernet.localIP());
2015-01-10 15:25:27 +01:00
}
2017-12-11 15:19:28 +01:00
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 JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
2017-12-11 15:19:28 +01:00
StaticJsonBuffer<500> jsonBuffer;
// Create the root object
2015-01-10 15:25:27 +01:00
JsonObject& root = jsonBuffer.createObject();
2017-12-11 15:19:28 +01:00
// Create the "analog" array
2015-01-10 15:25:27 +01:00
JsonArray& analogValues = root.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
2017-12-11 15:19:28 +01:00
// Read the analog input
2015-01-10 15:25:27 +01:00
int value = analogRead(pin);
2017-12-11 15:19:28 +01:00
// Add the value at the end of the array
2015-01-10 15:25:27 +01:00
analogValues.add(value);
}
2017-12-11 15:19:28 +01:00
// Create the "digital" array
2015-01-10 15:25:27 +01:00
JsonArray& digitalValues = root.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) {
2017-12-11 15:19:28 +01:00
// Read the digital input
2015-01-10 15:25:27 +01:00
int value = digitalRead(pin);
2017-12-11 15:19:28 +01:00
// Add the value at the end of the array
2015-01-10 15:25:27 +01:00
digitalValues.add(value);
}
2017-12-11 15:19:28 +01:00
Serial.print(F("Sending: "));
root.printTo(Serial);
Serial.println();
2015-01-10 15:25:27 +01:00
2017-12-11 15:19:28 +01:00
// Write response headers
client.println("HTTP/1.0 200 OK");
2015-01-10 15:25:27 +01:00
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
2017-12-11 15:19:28 +01:00
// Write JSON document
root.prettyPrintTo(client);
2015-01-10 15:25:27 +01:00
2017-12-11 15:19:28 +01:00
// Disconnect
client.stop();
2015-01-10 15:25:27 +01:00
}
2017-12-11 15:19:28 +01:00
// See also
// --------
//
// The website arduinojson.org contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// serialization problem.
// Please check it out at: https://arduinojson.org/
//
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
// It begins with a simple example, then adds more features like serializing
// directly to a file or an HTTP client.
2017-12-27 14:15:49 +01:00
// Please check it out at: https://arduinojson.org/book/