123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #include <ArduinoJson.h>
- #include <Ethernet.h>
- #include <SPI.h>
- void setup() {
-
- Serial.begin(9600);
- while (!Serial) continue;
-
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- if (!Ethernet.begin(mac)) {
- Serial.println(F("Failed to configure Ethernet"));
- return;
- }
- delay(1000);
- Serial.println(F("Connecting..."));
-
- EthernetClient client;
- client.setTimeout(10000);
- if (!client.connect("arduinojson.org", 80)) {
- Serial.println(F("Connection failed"));
- return;
- }
- Serial.println(F("Connected!"));
-
- 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"));
- return;
- }
-
- char status[32] = {0};
- client.readBytesUntil('\r', status, sizeof(status));
-
- if (strcmp(status + 9, "200 OK") != 0) {
- Serial.print(F("Unexpected response: "));
- Serial.println(status);
- return;
- }
-
- char endOfHeaders[] = "\r\n\r\n";
- if (!client.find(endOfHeaders)) {
- Serial.println(F("Invalid response"));
- return;
- }
-
-
- const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
- DynamicJsonDocument doc(capacity);
-
- DeserializationError error = deserializeJson(doc, client);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- return;
- }
-
- Serial.println(F("Response:"));
- Serial.println(doc["sensor"].as<char*>());
- Serial.println(doc["time"].as<long>());
- Serial.println(doc["data"][0].as<float>(), 6);
- Serial.println(doc["data"][1].as<float>(), 6);
-
- client.stop();
- }
- void loop() {
-
- }
|