Arduino Yun and UDP broadcast.

Hello,

I've searched a lot of topics, but haven't found any simple solution for UDP broadcast with Yun.

If someone also face with this problem, here is the solution:

0. Connect your Arduino Yun to your local network (WiFi or Ethernet)

1. Download Arduino Yun Bridge code

2. Get from there file: sockets_udp.py

3. Upload sockets_udp.py to your Arduino Yun, to location: /usr/lib/python2.7/bridge

4. Edit on your Arduino Yun file: bridge.py

AFTER:

import sockets
sockets.init(cp)

ADD THIS CODE:

import sockets_udp
sockets_udp.init(cp)

5. Reboot Arduino Yun.

6. Make new sketch in Arduino Studio with this code (and upload it to Yun):

#include <Process.h>
#include <BridgeUdp.h>

BridgeUDP Udp;

void setup() {
  Bridge.begin();        
  Udp.begin(8888);  

  Serial.begin(9600);     
}

void loop() 
{
  uint8_t data[] = {'T', 'E', 'S', 'T'};
  Udp.beginBroadcastPacket(8888); 
  Udp.write(data, sizeof(data)); 
  Udp.endPacket();

  Serial.println("<Data sent");
  delay(500);

  int packetSize = Udp.parsePacket();
  if (packetSize) 
  {
    Serial.print(">Received data:");
    
    char packetBuffer [5];
    packetBuffer[4] = '\0';
    Udp.read(packetBuffer, 4);
    
    Serial.println(packetBuffer);
  }
}

7. If everything went ok, you should see something like this in your Console:

<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent
>Received data:TEST
<Data sent

This solution is based on the standard Arduino libraries (Process, BridgeUDP). The BridgeUDP already contains beginBroadcastPacket function which is all you need (except Python code uploaded to Yun). The broadcast destination is 255.255.255.255.

Hope this will help somebody, cause I registered just for post it. :slight_smile:

Best regards,