JsonUdpBeacon.ino 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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/v6/example/udp-beacon/
  21. #include <ArduinoJson.h>
  22. #include <Ethernet.h>
  23. #include <SPI.h>
  24. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  25. IPAddress remoteIp(192, 168, 0, 108); // <- EDIT!!!!
  26. unsigned short remotePort = 8888;
  27. unsigned short localPort = 8888;
  28. EthernetUDP udp;
  29. void setup() {
  30. // Initialize serial port
  31. Serial.begin(9600);
  32. while (!Serial) continue;
  33. // Initialize Ethernet libary
  34. if (!Ethernet.begin(mac)) {
  35. Serial.println(F("Failed to initialize Ethernet library"));
  36. return;
  37. }
  38. // Enable UDP
  39. udp.begin(localPort);
  40. }
  41. void loop() {
  42. // Allocate a temporary JsonDocument
  43. // Use arduinojson.org/v6/assistant to compute the capacity.
  44. StaticJsonDocument<500> doc;
  45. // Create the "analog" array
  46. JsonArray analogValues = doc.createNestedArray("analog");
  47. for (int pin = 0; pin < 6; pin++) {
  48. // Read the analog input
  49. int value = analogRead(pin);
  50. // Add the value at the end of the array
  51. analogValues.add(value);
  52. }
  53. // Create the "digital" array
  54. JsonArray digitalValues = doc.createNestedArray("digital");
  55. for (int pin = 0; pin < 14; pin++) {
  56. // Read the digital input
  57. int value = digitalRead(pin);
  58. // Add the value at the end of the array
  59. digitalValues.add(value);
  60. }
  61. // Log
  62. Serial.print(F("Sending to "));
  63. Serial.print(remoteIp);
  64. Serial.print(F(" on port "));
  65. Serial.println(remotePort);
  66. serializeJson(doc, Serial);
  67. // Send UDP packet
  68. udp.beginPacket(remoteIp, remotePort);
  69. serializeJson(doc, udp);
  70. udp.println();
  71. udp.endPacket();
  72. // Wait
  73. delay(10000);
  74. }
  75. // See also
  76. // --------
  77. //
  78. // https://arduinojson.org/ contains the documentation for all the functions
  79. // used above. It also includes an FAQ that will help you solve any
  80. // serialization problem.
  81. //
  82. // The book "Mastering ArduinoJson" contains a tutorial on serialization.
  83. // It begins with a simple example, then adds more features like serializing
  84. // directly to a file or any stream.
  85. // Learn more at https://arduinojson.org/book/
  86. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤