22// Copyright Benoit Blanchon 2014-2017
33// MIT License
44//
5- // Example of an HTTP client parsing a JSON response.
5+ // This example shows how to parse a JSON document in an HTTP response.
6+ // It uses the Ethernet library, but can be easily adapter for Wifi.
67//
7- // This program perform an HTTP GET of arduinojson.org/example.json
8+ // It performs a GET resquest on arduinojson.org/example.json
89// Here is the expected response:
910// {
1011// "sensor": "gps",
1415// 2.302038
1516// ]
1617// }
17- // See http://arduinojson.org/assistant/ to compute the size of the buffer.
18- //
19- // Disclaimer: the code emphasize the communication between client and server,
20- // it doesn't claim to be a reference of good coding practices.
2118
2219#include < ArduinoJson.h>
2320#include < Ethernet.h>
2421#include < SPI.h>
2522
2623void setup () {
24+ // Initialize Serial port
2725 Serial.begin (9600 );
28- while (!Serial);
26+ while (!Serial) continue ;
2927
30- echo ( " Initialize Ethernet library" );
28+ // Initialize Ethernet library
3129 byte mac[] = {0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED };
32- Ethernet.begin (mac) || die (" Failed to configure Ethernet" );
30+ if (!Ethernet.begin (mac)) {
31+ Serial.println (F (" Failed to configure Ethernet" ));
32+ return ;
33+ }
3334 delay (1000 );
3435
35- echo (" Connect to HTTP server" );
36+ Serial.println (F (" Connecting..." ));
37+
38+ // Connect to HTTP server
3639 EthernetClient client;
3740 client.setTimeout (10000 );
38- client.connect (" arduinojson.org" , 80 ) || die (" Connection failed" );
41+ if (!client.connect (" arduinojson.org" , 80 )) {
42+ Serial.println (F (" Connection failed" ));
43+ return ;
44+ }
45+
46+ Serial.println (F (" Connected!" ));
3947
40- echo (" Send HTTP request" );
41- client.println (" GET /example.json HTTP/1.0" );
42- client.println (" Host: arduinojson.org" );
43- client.println (" Connection: close" );
44- client.println () || die (" Failed to send request" );
48+ // Send HTTP request
49+ client.println (F (" GET /example.json HTTP/1.0" ));
50+ client.println (F (" Host: arduinojson.org" ));
51+ client.println (F (" Connection: close" ));
52+ if (client.println () == 0 ) {
53+ Serial.println (F (" Failed to send request" ));
54+ return ;
55+ }
4556
46- echo ( " Check HTTP status" );
57+ // Check HTTP status
4758 char status[32 ] = {0 };
4859 client.readBytesUntil (' \r ' , status, sizeof (status));
4960 if (strcmp (status, " HTTP/1.1 200 OK" ) != 0 ) {
50- echo (status);
51- die (" Unexpected HTTP response" );
61+ Serial.print (F (" Unexpected response: " ));
62+ Serial.println (status);
63+ return ;
5264 }
5365
54- echo ( " Skip HTTP headers" );
66+ // Skip HTTP headers
5567 char endOfHeaders[] = " \r\n\r\n " ;
56- client.find (endOfHeaders) || die (" Invalid response" );
68+ if (!client.find (endOfHeaders)) {
69+ Serial.println (F (" Invalid response" ));
70+ return ;
71+ }
5772
58- echo (" Allocate JsonBuffer" );
59- const size_t BUFFER_SIZE = JSON_OBJECT_SIZE (3 ) + JSON_ARRAY_SIZE (2 ) + 60 ;
60- DynamicJsonBuffer jsonBuffer (BUFFER_SIZE);
73+ // Allocate JsonBuffer
74+ // (see https://arduinojson.org/assistant/ to compute the capacity)
75+ const size_t capacity = JSON_OBJECT_SIZE (3 ) + JSON_ARRAY_SIZE (2 ) + 60 ;
76+ DynamicJsonBuffer jsonBuffer (capacity);
6177
62- echo ( " Parse JSON object" );
78+ // Parse JSON object
6379 JsonObject& root = jsonBuffer.parseObject (client);
64- if (!root.success ()) die (" Parsing failed!" );
80+ if (!root.success ()) {
81+ Serial.println (F (" Parsing failed!" ));
82+ return ;
83+ }
6584
66- echo (" Extract values" );
67- echo (root[" sensor" ].as <char *>());
68- echo (root[" time" ].as <char *>());
69- echo (root[" data" ][0 ].as <char *>());
70- echo (root[" data" ][1 ].as <char *>());
85+ // Extract values
86+ Serial.println (F (" Response:" ));
87+ Serial.println (root[" sensor" ].as <char *>());
88+ Serial.println (root[" time" ].as <char *>());
89+ Serial.println (root[" data" ][0 ].as <char *>());
90+ Serial.println (root[" data" ][1 ].as <char *>());
7191
72- echo ( " Disconnect" );
92+ // Disconnect
7393 client.stop ();
7494}
7595
76- void loop () {}
77-
78- void echo (const char * message) {
79- Serial.println (message);
96+ void loop () {
97+ // not used in this example
8098}
8199
82- bool die (const char * message) {
83- Serial.println (message);
84- while (true ); // loop forever
85- return false ;
86- }
100+ // See also
101+ // --------
102+ //
103+ // The website arduinojson.org contains the documentation for all the functions
104+ // used above. It also includes an FAQ that will help you solve any
105+ // serialization problem.
106+ // Please check it out at: https://arduinojson.org/
107+ //
108+ // The book "Mastering ArduinoJson" contains a tutorial on deserialization
109+ // showing how to parse the response from Yahoo Weather. In the last chapter,
110+ // it shows how to parse the huge documents from OpenWeatherMap
111+ // and Weather Underground.
112+ // Please check it out at: https://leanpub.com/arduinojson/
0 commit comments