Simple UDP communication

I’ve checked dozens of forum posts, with no simple answer. I want to start as simply as possible. A python script sends out a UDP packet every second. I want the Arduino Yun to receive this packet and print it to the console. Here is my attempt:

#ArdUDP.py
import socket
import time

UDP_IP = "10.0.1.47"
UDP_Port = 2390

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

while True :
    sock.sendto('A', (UDP_IP,UDP_PORT)
    time.sleep(1)

Then I use the exact WiFiUdpSendReceiveString.ino example, pasted below for reference.

/*
  WiFi UDP Send and Receive String

 This sketch wait an UDP packet on localPort using a WiFi shield.
 When a packet is received an Acknowledge packet is sent to the client on port remotePort

 Circuit:
 * WiFi shield attached

 created 30 December 2012
 by dlf (Metodo2 srl)

 */


#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; //  EDIT: I put my actual wifi name here
char pass[] = "secretPassword";    // EDIT: and my actual password here
int keyIndex = 0;            // your network key Index number (needed only for WEP)

unsigned int localPort = 2390;      // local port to listen on

char packetBuffer[255]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back

WiFiUDP Udp;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);
}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

So far, nothing happens. Has anyone found a simple way to receive and process UDP commands, maybe even send a UDP packet back in response? Thanks for any help.

Yun wifi is managed by the Linux side, so WiFi lib is useless. Unfortunately, Bridge YunClient/YunServer does not support UDP. There is an old issue about that. https://github.com/arduino/Arduino/issues/1716 I've added this topic to the list of topics to report back to

Why TCP is prefer than UDP.

http://forum.arduino.cc/index.php?topic=187287.msg1645470#msg1645470

If you still like to do

http://forum.arduino.cc/index.php?topic=187287.msg1647539#msg1647539

I did it. Three things need to work together to communicate: the computer (or whatever is sending your UDP packets), the Linino side, and the ATmega side. Computer sends the UDP packets to the Linino, the Linino puts a value into the Bridge datastore, and the ATmega reads the Bridge datastore.

Here’s the three pieces of code to make the pin13 LED blink with UDP packets. I use pygame to for keyboard input on the computer side, but it’s not necessary. Everything is in python or arduino.

Computer
$ python ArdUDP.py

#ArdUDP.py
import socket
import pygame

UDP_IP = "10.0.1.48"
# UDP_IP = ""
UDP_PORT = 5555

pygame.init()

sock = socket.socket(socket.AF_INET, #Internet
	socket.SOCK_DGRAM) #UDP

done = False

key_msg = {pygame.K_UP:'U',pygame.K_DOWN:'D',pygame.K_LEFT:'L',pygame.K_RIGHT:'R'}

while not done:
	for event in pygame.event.get() :
		msg = ''
		if event.type == pygame.KEYDOWN :
			msg = key_msg.get(event.key,'')
			if event.key == pygame.K_ESCAPE :
				done = True
				break
		if msg :
			print msg
			sock.sendto(msg, (UDP_IP,UDP_PORT))

Linino:
$ ssh root@myarduinoname.local
(cd to correct directory)
# python UDPMiddleMan.py

# UDPMiddleMan.py
import socket                                                                   
import time                                                                     
from bridgeclient import BridgeClient                                           
                                                                                
bridge = BridgeClient()                                                         
                                                                                
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)                          
                                                                                
sock.bind(("",5555))                                                            
                                                                                
while True :                                                                    
        try :                                                                   
                msg = sock.recv(1024)                                           
                if msg :                                                        
                        if msg == 'U' :                                         
                                bridge.put("D13","1")                           
                        elif msg == 'D' :                                       
                                bridge.put("D13","0")                           
                        print msg                                               
        except KeyboardInterrupt :                                              
                break                                                           
        except Exception as err:                                                
                print err                                                       
                pass

ATmega:

//BridgeBlink.ino
#include <Bridge.h>

char D13char[2];
int D13int = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(13,OUTPUT);
  
  digitalWrite(13,LOW);
  Bridge.begin();
  digitalWrite(13,HIGH);
  
  delay(1000);
  
  digitalWrite(13,LOW);
  Console.begin();
  while(!Console){
    ; //wait for console port to connect.
  }
  Console.println("You're connected to the Console!!!");
  digitalWrite(13,HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  Bridge.get("D13",D13char,2);
  D13int = atoi(D13char);
  if(D13int==0 || D13int==1){
    Console.println(D13char);
    digitalWrite(13,D13int);
    Bridge.put("D13","2");
  }
  delay(50);
}