|
|
@@ -1,76 +1,109 @@
|
|
|
-// Sample Arduino Json Web Server
|
|
|
-// Created by Benoit Blanchon.
|
|
|
-// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe
|
|
|
+// ArduinoJson - arduinojson.org
|
|
|
+// Copyright Benoit Blanchon 2014-2017
|
|
|
+// MIT License
|
|
|
+//
|
|
|
+// This example shows how to implement an HTTP server that sends JSON document
|
|
|
+// in the responses.
|
|
|
+// It uses the Ethernet library but can be easily adapter for Wifi.
|
|
|
+//
|
|
|
+// 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 ]
|
|
|
+// }
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
#include <Ethernet.h>
|
|
|
#include <SPI.h>
|
|
|
|
|
|
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
|
|
-IPAddress ip(192, 168, 0, 177);
|
|
|
EthernetServer server(80);
|
|
|
|
|
|
-bool readRequest(EthernetClient& client) {
|
|
|
- bool currentLineIsBlank = true;
|
|
|
- while (client.connected()) {
|
|
|
- if (client.available()) {
|
|
|
- char c = client.read();
|
|
|
- if (c == '\n' && currentLineIsBlank) {
|
|
|
- return true;
|
|
|
- } else if (c == '\n') {
|
|
|
- currentLineIsBlank = true;
|
|
|
- } else if (c != '\r') {
|
|
|
- currentLineIsBlank = false;
|
|
|
- }
|
|
|
- }
|
|
|
+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;
|
|
|
}
|
|
|
- return false;
|
|
|
+
|
|
|
+ // Start to listen
|
|
|
+ server.begin();
|
|
|
+
|
|
|
+ Serial.println(F("Server is ready."));
|
|
|
+ Serial.print(F("Please connect to http://"));
|
|
|
+ Serial.println(Ethernet.localIP());
|
|
|
}
|
|
|
|
|
|
-JsonObject& prepareResponse(JsonBuffer& jsonBuffer) {
|
|
|
+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 http://arduinojson.org/assistant/ to compute the right capacity
|
|
|
+ StaticJsonBuffer<500> jsonBuffer;
|
|
|
+
|
|
|
+ // Create the root object
|
|
|
JsonObject& root = jsonBuffer.createObject();
|
|
|
|
|
|
+ // Create the "analog" array
|
|
|
JsonArray& analogValues = root.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 = root.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);
|
|
|
}
|
|
|
|
|
|
- return root;
|
|
|
-}
|
|
|
+ Serial.print(F("Sending: "));
|
|
|
+ root.printTo(Serial);
|
|
|
+ Serial.println();
|
|
|
|
|
|
-void writeResponse(EthernetClient& client, JsonObject& json) {
|
|
|
- client.println("HTTP/1.1 200 OK");
|
|
|
+ // Write response headers
|
|
|
+ client.println("HTTP/1.0 200 OK");
|
|
|
client.println("Content-Type: application/json");
|
|
|
client.println("Connection: close");
|
|
|
client.println();
|
|
|
|
|
|
- json.prettyPrintTo(client);
|
|
|
-}
|
|
|
+ // Write JSON document
|
|
|
+ root.prettyPrintTo(client);
|
|
|
|
|
|
-void setup() {
|
|
|
- Ethernet.begin(mac, ip);
|
|
|
- server.begin();
|
|
|
+ // Disconnect
|
|
|
+ client.stop();
|
|
|
}
|
|
|
|
|
|
-void loop() {
|
|
|
- EthernetClient client = server.available();
|
|
|
- if (client) {
|
|
|
- bool success = readRequest(client);
|
|
|
- if (success) {
|
|
|
- // Use http://arduinojson.org/assistant/ to
|
|
|
- // compute the right size for the buffer
|
|
|
- StaticJsonBuffer<500> jsonBuffer;
|
|
|
- JsonObject& json = prepareResponse(jsonBuffer);
|
|
|
- writeResponse(client, json);
|
|
|
- }
|
|
|
- delay(1);
|
|
|
- client.stop();
|
|
|
- }
|
|
|
-}
|
|
|
+// 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.
|
|
|
+// Please check it out at: https://leanpub.com/arduinojson/
|