Files
ArduinoJson/examples/JsonUdpBeacon/JsonUdpBeacon.ino

101 lines
2.6 KiB
Arduino
Raw Normal View History

2017-12-11 15:19:28 +01:00
// ArduinoJson - arduinojson.org
2020-01-09 15:48:38 +01:00
// Copyright Benoit Blanchon 2014-2020
2017-12-11 15:19:28 +01:00
// MIT License
2015-02-25 22:27:30 +01:00
//
2017-12-15 17:52:47 +01:00
// This example shows how to send a JSON document to a UDP socket.
2017-12-11 15:19:28 +01:00
// At regular interval, it sends a UDP packet that contains the status of
// analog and digital pins.
// It looks like that:
2017-12-11 15:19:28 +01:00
// {
// "analog": [0, 76, 123, 158, 192, 205],
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
2017-12-11 15:19:28 +01:00
// }
2015-02-25 22:27:30 +01:00
//
2017-12-11 15:19:28 +01:00
// If you want to test this program, you need to be able to receive the UDP
// packets.
// For example, you can run netcat on your computer
// $ ncat -ulp 8888
// See https://nmap.org/ncat/
//
// https://arduinojson.org/v6/example/udp-beacon/
2015-02-25 22:27:30 +01:00
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
2015-02-25 22:27:30 +01:00
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
2017-12-11 15:19:28 +01:00
IPAddress remoteIp(192, 168, 0, 108); // <- EDIT!!!!
unsigned short remotePort = 8888;
unsigned short localPort = 8888;
2015-02-25 22:27:30 +01:00
EthernetUDP udp;
2017-12-11 15:19:28 +01:00
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize Ethernet libary
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to initialize Ethernet library"));
return;
}
// Enable UDP
udp.begin(localPort);
}
void loop() {
// Allocate a temporary JsonDocument
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<500> doc;
2017-12-11 15:19:28 +01:00
// Create the "analog" array
JsonArray analogValues = doc.createNestedArray("analog");
2015-02-25 22:27:30 +01:00
for (int pin = 0; pin < 6; pin++) {
2017-12-11 15:19:28 +01:00
// Read the analog input
2015-02-25 22:27:30 +01:00
int value = analogRead(pin);
2017-12-11 15:19:28 +01:00
// Add the value at the end of the array
2015-02-25 22:27:30 +01:00
analogValues.add(value);
}
2017-12-11 15:19:28 +01:00
// Create the "digital" array
JsonArray digitalValues = doc.createNestedArray("digital");
2015-02-25 22:27:30 +01:00
for (int pin = 0; pin < 14; pin++) {
2017-12-11 15:19:28 +01:00
// Read the digital input
2015-02-25 22:27:30 +01:00
int value = digitalRead(pin);
2017-12-11 15:19:28 +01:00
// Add the value at the end of the array
2015-02-25 22:27:30 +01:00
digitalValues.add(value);
}
2017-12-11 15:19:28 +01:00
// Log
Serial.print(F("Sending to "));
Serial.print(remoteIp);
Serial.print(F(" on port "));
Serial.println(remotePort);
serializeJson(doc, Serial);
2015-02-25 22:27:30 +01:00
2017-12-11 15:19:28 +01:00
// Send UDP packet
2015-02-25 22:27:30 +01:00
udp.beginPacket(remoteIp, remotePort);
serializeJson(doc, udp);
2015-02-25 22:27:30 +01:00
udp.println();
udp.endPacket();
2017-12-11 15:19:28 +01:00
// Wait
delay(10000);
2015-02-25 22:27:30 +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 serialization.
// It begins with a simple example, then adds more features like serializing
// directly to a file or any stream.
// Learn more at https://arduinojson.org/book/
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤