Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8ArduinoJsonexamplesJsonServerJsonServer.ino

118 lines · 3.1 KB

1// ArduinoJson - https://arduinojson.org
2// Copyright © 2014-2026, Benoit BLANCHON
3// MIT License
4//
5// This example shows how to implement an HTTP server that sends a JSON document
6// in the response.
7// It uses the Ethernet library but can be easily adapted for Wifi.
8//
9// The JSON document contains the values of the analog and digital pins.
10// It looks like that:
11// {
12// "analog": [0, 76, 123, 158, 192, 205],
13// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
14// }
15//
16// https://arduinojson.org/v7/example/http-server/
17
18#include <ArduinoJson.h>
19#include <Ethernet.h>
20#include <SPI.h>
21
22byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
23EthernetServer server(80);
24
25void setup() {
26 // Initialize serial port
27 Serial.begin(9600);
28 while (!Serial)
29 continue;
30
31 // Initialize Ethernet libary
32 if (!Ethernet.begin(mac)) {
33 Serial.println(F("Failed to initialize Ethernet library"));
34 return;
35 }
36
37 // Start to listen
38 server.begin();
39
40 Serial.println(F("Server is ready."));
41 Serial.print(F("Please connect to http://"));
42 Serial.println(Ethernet.localIP());
43}
44
45void loop() {
46 // Wait for an incomming connection
47 EthernetClient client = server.available();
48
49 // Do we have a client?
50 if (!client)
51 return;
52
53 Serial.println(F("New client"));
54
55 // Read the request (we ignore the content in this example)
56 while (client.available())
57 client.read();
58
59 // Allocate a temporary JsonDocument
60 JsonDocument doc;
61
62 // Create the "analog" array
63 JsonArray analogValues = doc["analog"].to<JsonArray>();
64 for (int pin = 0; pin < 6; pin++) {
65 // Read the analog input
66 int value = analogRead(pin);
67
68 // Add the value at the end of the array
69 analogValues.add(value);
70 }
71
72 // Create the "digital" array
73 JsonArray digitalValues = doc["digital"].to<JsonArray>();
74 for (int pin = 0; pin < 14; pin++) {
75 // Read the digital input
76 int value = digitalRead(pin);
77
78 // Add the value at the end of the array
79 digitalValues.add(value);
80 }
81
82 Serial.print(F("Sending: "));
83 serializeJson(doc, Serial);
84 Serial.println();
85
86 // Write response headers
87 client.println(F("HTTP/1.0 200 OK"));
88 client.println(F("Content-Type: application/json"));
89 client.println(F("Connection: close"));
90 client.print(F("Content-Length: "));
91 client.println(measureJsonPretty(doc));
92 client.println();
93
94 // Write JSON document
95 serializeJsonPretty(doc, client);
96
97 // Disconnect
98 client.stop();
99}
100
101// Performance issue?
102// ------------------
103//
104// EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
105// See: https://arduinojson.org/v7/how-to/improve-speed/
106
107// See also
108// --------
109//
110// https://arduinojson.org/ contains the documentation for all the functions
111// used above. It also includes an FAQ that will help you solve any
112// serialization problem.
113//
114// The book "Mastering ArduinoJson" contains a tutorial on serialization.
115// It begins with a simple example, then adds more features like serializing
116// directly to a file or an HTTP client.
117// Learn more at https://arduinojson.org/book/
118// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤