123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include <ArduinoJson.h>
- #include <Ethernet.h>
- #include <SPI.h>
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- EthernetServer server(80);
- void setup() {
-
- Serial.begin(9600);
- while (!Serial) continue;
-
- if (!Ethernet.begin(mac)) {
- Serial.println(F("Failed to initialize Ethernet library"));
- return;
- }
-
- server.begin();
- Serial.println(F("Server is ready."));
- Serial.print(F("Please connect to http://"));
- Serial.println(Ethernet.localIP());
- }
- void loop() {
-
- EthernetClient client = server.available();
-
- if (!client) return;
- Serial.println(F("New client"));
-
- while (client.available()) client.read();
-
-
- StaticJsonDocument<500> doc;
-
- JsonArray analogValues = doc.createNestedArray("analog");
- for (int pin = 0; pin < 6; pin++) {
-
- int value = analogRead(pin);
-
- analogValues.add(value);
- }
-
- JsonArray digitalValues = doc.createNestedArray("digital");
- for (int pin = 0; pin < 14; pin++) {
-
- int value = digitalRead(pin);
-
- digitalValues.add(value);
- }
- Serial.print(F("Sending: "));
- serializeJson(doc, Serial);
- Serial.println();
-
- client.println(F("HTTP/1.0 200 OK"));
- client.println(F("Content-Type: application/json"));
- client.println(F("Connection: close"));
- client.print(F("Content-Length: "));
- client.println(measureJsonPretty(doc));
- client.println();
-
- serializeJsonPretty(doc, client);
-
- client.stop();
- }
|