2
2
// Copyright Benoit Blanchon 2014-2017
3
3
// MIT License
4
4
//
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.
6
7
//
7
- // This program perform an HTTP GET of arduinojson.org/example.json
8
+ // It performs a GET resquest on arduinojson.org/example.json
8
9
// Here is the expected response:
9
10
// {
10
11
// "sensor": "gps",
14
15
// 2.302038
15
16
// ]
16
17
// }
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.
21
18
22
19
#include < ArduinoJson.h>
23
20
#include < Ethernet.h>
24
21
#include < SPI.h>
25
22
26
23
void setup () {
24
+ // Initialize Serial port
27
25
Serial.begin (9600 );
28
- while (!Serial);
26
+ while (!Serial) continue ;
29
27
30
- echo ( " Initialize Ethernet library" );
28
+ // Initialize Ethernet library
31
29
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
+ }
33
34
delay (1000 );
34
35
35
- echo (" Connect to HTTP server" );
36
+ Serial.println (F (" Connecting..." ));
37
+
38
+ // Connect to HTTP server
36
39
EthernetClient client;
37
40
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!" ));
39
47
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
+ }
45
56
46
- echo ( " Check HTTP status" );
57
+ // Check HTTP status
47
58
char status[32 ] = {0 };
48
59
client.readBytesUntil (' \r ' , status, sizeof (status));
49
60
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 ;
52
64
}
53
65
54
- echo ( " Skip HTTP headers" );
66
+ // Skip HTTP headers
55
67
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
+ }
57
72
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);
61
77
62
- echo ( " Parse JSON object" );
78
+ // Parse JSON object
63
79
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
+ }
65
84
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 *>());
71
91
72
- echo ( " Disconnect" );
92
+ // Disconnect
73
93
client.stop ();
74
94
}
75
95
76
- void loop () {}
77
-
78
- void echo (const char * message) {
79
- Serial.println (message);
96
+ void loop () {
97
+ // not used in this example
80
98
}
81
99
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