Files
ArduinoJson/examples/JsonUdpBeacon/JsonUdpBeacon.ino

101 lines
2.7 KiB
Arduino
Raw Normal View History

2017-12-11 15:19:28 +01:00
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2017
// MIT License
2015-02-25 22:27:30 +01:00
//
2017-12-11 15:19:28 +01:00
// This example shows how to JSON document to a UDP socket.
// At regular interval, it sends a UDP packet that contains the status of
// analog and digital pins.
// The JSON document looks like the following:
// {
// "analog": [ 0, 1, 2, 3, 4, 5 ],
// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
// }
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/
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 JsonBuffer
// Use http://arduinojson.org/assistant/ to compute the right capacity.
StaticJsonBuffer<500> jsonBuffer;
// Create the root object
2015-02-25 22:27:30 +01:00
JsonObject& root = jsonBuffer.createObject();
2017-12-11 15:19:28 +01:00
// Create the "analog" array
2015-02-25 22:27:30 +01:00
JsonArray& analogValues = root.createNestedArray("analog");
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
2015-02-25 22:27:30 +01:00
JsonArray& digitalValues = root.createNestedArray("digital");
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);
root.printTo(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);
2017-12-11 15:19:28 +01:00
root.printTo(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
}
2017-12-11 15:19:28 +01:00
// See also
// --------
//
// The website arduinojson.org contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// serialization problem.
// Please check it out at: https://arduinojson.org/
//
// 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.
// Please check it out at: https://leanpub.com/arduinojson/