Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8ArduinoJsonexamplesJsonUdpBeaconJsonUdpBeacon.ino

106 lines · 2.7 KB

1// ArduinoJson - https://arduinojson.org
2// Copyright © 2014-2026, Benoit BLANCHON
3// MIT License
4//
5// This example shows how to send a JSON document to a UDP socket.
6// At regular interval, it sends a UDP packet that contains the status of
7// analog and digital pins.
8// It looks like that:
9// {
10// "analog": [0, 76, 123, 158, 192, 205],
11// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
12// }
13//
14// If you want to test this program, you need to be able to receive the UDP
15// packets.
16// For example, you can run netcat on your computer
17// $ ncat -ulp 8888
18// See https://nmap.org/ncat/
19//
20// https://arduinojson.org/v7/example/udp-beacon/
21
22#include <ArduinoJson.h>
23#include <Ethernet.h>
24#include <SPI.h>
25
26byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
27IPAddress remoteIp(192, 168, 0, 108); // <- EDIT!!!!
28unsigned short remotePort = 8888;
29unsigned short localPort = 8888;
30EthernetUDP udp;
31
32void setup() {
33 // Initialize serial port
34 Serial.begin(9600);
35 while (!Serial)
36 continue;
37
38 // Initialize Ethernet libary
39 if (!Ethernet.begin(mac)) {
40 Serial.println(F("Failed to initialize Ethernet library"));
41 return;
42 }
43
44 // Enable UDP
45 udp.begin(localPort);
46}
47
48void loop() {
49 // Allocate a temporary JsonDocument
50 JsonDocument doc;
51
52 // Create the "analog" array
53 JsonArray analogValues = doc["analog"].to<JsonArray>();
54 for (int pin = 0; pin < 6; pin++) {
55 // Read the analog input
56 int value = analogRead(pin);
57
58 // Add the value at the end of the array
59 analogValues.add(value);
60 }
61
62 // Create the "digital" array
63 JsonArray digitalValues = doc["digital"].to<JsonArray>();
64 for (int pin = 0; pin < 14; pin++) {
65 // Read the digital input
66 int value = digitalRead(pin);
67
68 // Add the value at the end of the array
69 digitalValues.add(value);
70 }
71
72 // Log
73 Serial.print(F("Sending to "));
74 Serial.print(remoteIp);
75 Serial.print(F(" on port "));
76 Serial.println(remotePort);
77 serializeJson(doc, Serial);
78
79 // Send UDP packet
80 udp.beginPacket(remoteIp, remotePort);
81 serializeJson(doc, udp);
82 udp.println();
83 udp.endPacket();
84
85 // Wait
86 delay(10000);
87}
88
89// Performance issue?
90// ------------------
91//
92// EthernetUDP is an unbuffered stream, which is not optimal for ArduinoJson.
93// See: https://arduinojson.org/v7/how-to/improve-speed/
94
95// See also
96// --------
97//
98// https://arduinojson.org/ contains the documentation for all the functions
99// used above. It also includes an FAQ that will help you solve any
100// serialization problem.
101//
102// The book "Mastering ArduinoJson" contains a tutorial on serialization.
103// It begins with a simple example, then adds more features like serializing
104// directly to a file or any stream.
105// Learn more at https://arduinojson.org/book/
106// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤