| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  | // ArduinoJson - https://arduinojson.org
 | 
					
						
							| 
									
										
										
										
											2022-01-01 10:00:54 +01:00
										 |  |  | // Copyright © 2014-2022, Benoit BLANCHON
 | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | // MIT License
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  | // This example shows how to parse a JSON document in an HTTP response.
 | 
					
						
							| 
									
										
										
										
											2017-12-15 17:52:47 +01:00
										 |  |  | // It uses the Ethernet library, but can be easily adapted for Wifi.
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  | // It performs a GET resquest on https://arduinojson.org/example.json
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  | // Here is the expected response:
 | 
					
						
							|  |  |  | // {
 | 
					
						
							|  |  |  | //   "sensor": "gps",
 | 
					
						
							|  |  |  | //   "time": 1351824120,
 | 
					
						
							|  |  |  | //   "data": [
 | 
					
						
							|  |  |  | //     48.756080,
 | 
					
						
							|  |  |  | //     2.302038
 | 
					
						
							|  |  |  | //   ]
 | 
					
						
							|  |  |  | // }
 | 
					
						
							| 
									
										
										
										
											2019-03-04 12:17:41 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // https://arduinojson.org/v6/example/http-client/
 | 
					
						
							| 
									
										
										
										
											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-12-11 15:19:28 +01:00
										 |  |  |   // Initialize Serial port
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  |   Serial.begin(9600); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   while (!Serial) continue; | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Initialize Ethernet library
 | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  |   byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   if (!Ethernet.begin(mac)) { | 
					
						
							|  |  |  |     Serial.println(F("Failed to configure Ethernet")); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  |   delay(1000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   Serial.println(F("Connecting...")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Connect to HTTP server
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  |   EthernetClient client; | 
					
						
							|  |  |  |   client.setTimeout(10000); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   if (!client.connect("arduinojson.org", 80)) { | 
					
						
							|  |  |  |     Serial.println(F("Connection failed")); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Serial.println(F("Connected!")); | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Send HTTP request
 | 
					
						
							|  |  |  |   client.println(F("GET /example.json HTTP/1.0")); | 
					
						
							|  |  |  |   client.println(F("Host: arduinojson.org")); | 
					
						
							|  |  |  |   client.println(F("Connection: close")); | 
					
						
							|  |  |  |   if (client.println() == 0) { | 
					
						
							|  |  |  |     Serial.println(F("Failed to send request")); | 
					
						
							| 
									
										
										
										
											2021-02-01 09:16:23 +01:00
										 |  |  |     client.stop(); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Check HTTP status
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  |   char status[32] = {0}; | 
					
						
							|  |  |  |   client.readBytesUntil('\r', status, sizeof(status)); | 
					
						
							| 
									
										
										
										
											2019-05-26 21:07:01 +02:00
										 |  |  |   // It should be "HTTP/1.0 200 OK" or "HTTP/1.1 200 OK"
 | 
					
						
							|  |  |  |   if (strcmp(status + 9, "200 OK") != 0) { | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |     Serial.print(F("Unexpected response: ")); | 
					
						
							|  |  |  |     Serial.println(status); | 
					
						
							| 
									
										
										
										
											2021-02-01 09:16:23 +01:00
										 |  |  |     client.stop(); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |     return; | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Skip HTTP headers
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  |   char endOfHeaders[] = "\r\n\r\n"; | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   if (!client.find(endOfHeaders)) { | 
					
						
							|  |  |  |     Serial.println(F("Invalid response")); | 
					
						
							| 
									
										
										
										
											2021-02-01 09:16:23 +01:00
										 |  |  |     client.stop(); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Allocate the JSON document
 | 
					
						
							| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  |   // Use https://arduinojson.org/v6/assistant to compute the capacity.
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60; | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   DynamicJsonDocument doc(capacity); | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Parse JSON object
 | 
					
						
							| 
									
										
										
										
											2018-05-15 18:23:09 +02:00
										 |  |  |   DeserializationError error = deserializeJson(doc, client); | 
					
						
							| 
									
										
										
										
											2018-03-09 16:58:01 +01:00
										 |  |  |   if (error) { | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |     Serial.print(F("deserializeJson() failed: ")); | 
					
						
							| 
									
										
										
										
											2020-09-14 09:30:58 +02:00
										 |  |  |     Serial.println(error.f_str()); | 
					
						
							| 
									
										
										
										
											2021-02-01 09:16:23 +01:00
										 |  |  |     client.stop(); | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Extract values
 | 
					
						
							|  |  |  |   Serial.println(F("Response:")); | 
					
						
							| 
									
										
										
										
											2021-03-08 09:58:09 +01:00
										 |  |  |   Serial.println(doc["sensor"].as<const char*>()); | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   Serial.println(doc["time"].as<long>()); | 
					
						
							|  |  |  |   Serial.println(doc["data"][0].as<float>(), 6); | 
					
						
							|  |  |  |   Serial.println(doc["data"][1].as<float>(), 6); | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  |   // Disconnect
 | 
					
						
							| 
									
										
										
										
											2017-11-13 15:47:26 +01:00
										 |  |  |   client.stop(); | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 15:19:28 +01:00
										 |  |  | void loop() { | 
					
						
							|  |  |  |   // not used in this example
 | 
					
						
							| 
									
										
										
										
											2016-04-08 20:11:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 09:26:29 +01:00
										 |  |  | // Performance issue?
 | 
					
						
							|  |  |  | // ------------------
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
 | 
					
						
							|  |  |  | // See: https://arduinojson.org/v6/how-to/improve-speed/
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 12:17:41 +01:00
										 |  |  | // See also
 | 
					
						
							|  |  |  | // --------
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // https://arduinojson.org/ contains the documentation for all the functions
 | 
					
						
							|  |  |  | // used above. It also includes an FAQ that will help you solve any
 | 
					
						
							|  |  |  | // serialization  problem.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The book "Mastering ArduinoJson" contains a tutorial on deserialization
 | 
					
						
							|  |  |  | // showing how to parse the response from GitHub's API. In the last chapter,
 | 
					
						
							|  |  |  | // it shows how to parse the huge documents from OpenWeatherMap
 | 
					
						
							|  |  |  | // and Reddit.
 | 
					
						
							|  |  |  | // Learn more at https://arduinojson.org/book/
 | 
					
						
							|  |  |  | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
 |