2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2017-01-06 21:07:34 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2017
|
2016-04-08 20:11:36 +02:00
|
|
|
// MIT License
|
2017-11-13 15:47:26 +01:00
|
|
|
//
|
|
|
|
// Example of an HTTP client parsing a JSON response.
|
|
|
|
//
|
|
|
|
// This program perform an HTTP GET of arduinojson.org/example.json
|
|
|
|
// Here is the expected response:
|
|
|
|
// {
|
|
|
|
// "sensor": "gps",
|
|
|
|
// "time": 1351824120,
|
|
|
|
// "data": [
|
|
|
|
// 48.756080,
|
|
|
|
// 2.302038
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
// See http://arduinojson.org/assistant/ to compute the size of the buffer.
|
|
|
|
//
|
|
|
|
// Disclaimer: the code emphasize the communication between client and server,
|
|
|
|
// it doesn't claim to be a reference of good coding practices.
|
2016-04-08 20:11:36 +02:00
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <Ethernet.h>
|
2017-01-03 22:03:50 +01:00
|
|
|
#include <SPI.h>
|
2016-04-08 20:11:36 +02:00
|
|
|
|
|
|
|
void setup() {
|
2017-11-13 15:47:26 +01:00
|
|
|
Serial.begin(9600);
|
|
|
|
while (!Serial);
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Initialize Ethernet library");
|
2016-04-08 20:11:36 +02:00
|
|
|
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
2017-11-13 15:47:26 +01:00
|
|
|
Ethernet.begin(mac) || die("Failed to configure Ethernet");
|
2016-04-08 20:11:36 +02:00
|
|
|
delay(1000);
|
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Connect to HTTP server");
|
|
|
|
EthernetClient client;
|
|
|
|
client.setTimeout(10000);
|
|
|
|
client.connect("arduinojson.org", 80) || die("Connection failed");
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Send HTTP request");
|
|
|
|
client.println("GET /example.json HTTP/1.0");
|
|
|
|
client.println("Host: arduinojson.org");
|
2016-04-08 20:11:36 +02:00
|
|
|
client.println("Connection: close");
|
2017-11-13 15:47:26 +01:00
|
|
|
client.println() || die("Failed to send request");
|
|
|
|
|
|
|
|
echo("Check HTTP status");
|
|
|
|
char status[32] = {0};
|
|
|
|
client.readBytesUntil('\r', status, sizeof(status));
|
|
|
|
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
|
|
|
|
echo(status);
|
|
|
|
die("Unexpected HTTP response");
|
2016-04-08 20:11:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Skip HTTP headers");
|
|
|
|
char endOfHeaders[] = "\r\n\r\n";
|
|
|
|
client.find(endOfHeaders) || die("Invalid response");
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Allocate JsonBuffer");
|
|
|
|
const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
|
2017-01-03 22:03:50 +01:00
|
|
|
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Parse JSON object");
|
2017-01-03 22:03:50 +01:00
|
|
|
JsonObject& root = jsonBuffer.parseObject(client);
|
2017-11-13 15:47:26 +01:00
|
|
|
if (!root.success()) die("Parsing failed!");
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Extract values");
|
|
|
|
echo(root["sensor"].as<char*>());
|
|
|
|
echo(root["time"].as<char*>());
|
|
|
|
echo(root["data"][0].as<char*>());
|
|
|
|
echo(root["data"][1].as<char*>());
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
echo("Disconnect");
|
|
|
|
client.stop();
|
2016-04-08 20:11:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
void loop() {}
|
2016-04-08 20:11:36 +02:00
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
void echo(const char* message) {
|
|
|
|
Serial.println(message);
|
2016-04-08 20:11:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-13 15:47:26 +01:00
|
|
|
bool die(const char* message) {
|
|
|
|
Serial.println(message);
|
|
|
|
while (true); // loop forever
|
|
|
|
return false;
|
|
|
|
}
|