UDP supported?

sonnyyu:
UDP is a lightweight protocol that by design doesn’t handle things like packet sequencing. It does not guarantee that your packets will arrive in order (It does not even guarantee that your packets will arrive at all.) .

In my UDP application the received packets are order independent and keyed so that I can action them correctly.

My problem is that I can’t get started with how to implement UDP on the Yun so I would appreciate a complete sketch from someone.

Yun is based on Linux that has full stack of TCP/UDP. It answer PnP problem by implemented Zero-configuration networking via Avahi (similar to Apple Bonjour). Avahi uses IP multicast User Datagram Protocol (UDP) packets.

Beside broadcast, anytime low-latency, real-time overweight guarantee packet delivery and sequencing such as audio or video stream use UDP at most case.

My "Let's stay in TCP" which only stay bridge between Arduino and Yun.

Lets stay TCP and UDP :P

When I send the command ‘echo “G” | nc -u 192.168.1.123 8888’ to an Arduino Uno on my network from the Terminal app in OS X I receive the expected response of G,42

When I try to send the same command from a Yun, using the script shown below, the p.runShellCommand never seems to finish.

Sending ‘echo “L12” | nc -u 192.168.1.123 8888’ to my Uno from the Yun should switch on one of my Wi-Fi lights but this does not happen, although the same command issued in the Terminal app does.

How can I check that the UDP message is being sent?

/*
  Running shell commands using Process class.
 */

#include <Process.h>
// cmd = echo "G" | nc -u 192.168.1.123 8888
String cmd = "echo \"G\" | nc -u 192.168.1.123 8888";

void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial

  // Wait until a Serial Monitor is connected.
  while(!Serial);
}

void loop() {
  Process p;
  p.runShellCommand(cmd);

  // do nothing until the process finishes, so you get the whole output:
  while(p.running());

  // Read command output.
  while (p.available() > 0) {
    char c = p.read();
    Serial.print(c);
  }
  Serial.flush();
  delay(5000);  // wait 5 seconds before you do it again
}

At Yun:

nano /mnt/sda1/udp.py
#!/usr/bin/python
import socket
UDP_IP = "192.168.0.230"   #Mac or Linux box's IP
UDP_PORT = 8888
MESSAGE = "G"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT))
sock.close()
chmod 755 /mnt/sda1/udp.py

At Linux/Mac box( wait to call):

nc -ul 192.168.0.230  8888

At Yun:

/mnt/sda1/udp.py

If every thing is OK( Mac Box got "G"), then change UDP_IP = "Uno IP address", test it again.

Now is time make Arduino of Yun to call "/mnt/sda1/udp.py"

...
String cmd = "/mnt/sda1/udp.py";
...

Thank you for taking the time to help me. You have certainly given me a lot to think about.

I understand the procedure that you have set out but I still need to sort out how to implement the various bits of code but I will try.

Many thanks, Richard

Thank you for putting me on the right track :) I am now able to control my Wi-Fi lights from the Yun.

I have learnt a lot with your help.

However, I am missing one feature now. Some of my UDP commands have replies and I would like to retrieve them. I think that I can modify the Python script to get the reply but how do I pass the reply back to the Arduino side of the Yun?

Did you like try this?

...
recvmsg=sock.recv(1024)
#print recvmsg
sock.close()

sonnyyu: Did you like try this?

...
recvmsg=sock.recv(1024)
#print recvmsg
sock.close()

I have just modified the Python code to include recvmsg=sock.recv(1024)

print recvmsg

and I still do not get a reply sent across to the Arduino side of the bridge. What does #print recvmsg do?

uncomment "#print recvmsg" for debug code at Yun console.

The code send reply back to Yun:

...
import sys  
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
...
bc = bridgeclient()                          
bc.put('to_arduino',recvmsg)
...

I'm sorry but I don't understand what the complete Python code should be now.

Please would you post the complete code for me.

Regards, Richard

At Yun:

nano /mnt/sda1/udp.py
#!/usr/bin/python
import socket
UDP_IP = "192.168.0.230"   #Uno IP address
UDP_PORT = 8888
MESSAGE = "G"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT))
recvmsg=sock.recv(1024)
print recvmsg
sock.close()
/mnt/sda1/udp.py

make sure return value from nano.

#!/usr/bin/python
import socket
import sys  
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
UDP_IP = "192.168.0.230"   #Uno IP address
UDP_PORT = 8888
MESSAGE = "G"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT))
recvmsg=sock.recv(1024)
#print recvmsg
sock.close()
bc = bridgeclient()                          
bc.put('to_arduino',recvmsg)

Thank you once again. I have tried your code that uses the Yun Bridge and it works. I am using it to control my Wi-Fi lights and to retrieve the reply sent by the Uno controlling them.

Without your help I would have given up long ago. I have learnt a lot about the Yun with your help.

Regards, Richard

Hey, been following this trying to get it to work, and I think I'm very slowly understanding this.

I've got nano /mnt/sda1/udp.py to run, and that's to edit a file on the sd card right? To do that I need to use

export TERM='xterm-color'

To stop the error

Error opening terminal: xterm-256color.

So it opens fine, but when I try and write the next line of code I get [ Error writing /mnt/sda1/udp.py: No such file or directory ]

Have I just done something really silly somewhere?

Thanks! John

vi /etc/profile
export TERM=xterm-colo

exit ssh then relogin

https://forum.openwrt.org/viewtopic.php?id=13230

Plan B:

Start from fresh (backup your own file!):

/usr/bin/reset-to-factory-anyway
reboot

If I try

vi /etc/profile
export TERM=xterm-colo

I get

#!b/in/sh
rt TERM=xterm-colo                   
[ -f /etc/banner ] && cat /etc/banner
                                         
export PATH=/bin:/sbin:/usr/bin:/usr/sbin                             
export HOME=$(grep -e "^${USER:-root}:" /etc/passwd | cut -d ":" -f 6)
export HOME=${HOME:-/root}
export PS1='\u@\h:\w\$ '
                                   
[ -x /bin/more ] || alias more=less                
[ -x /usr/bin/vim ] && alias vi=vim || alias vim=vi
                                                           
[ -z "$KSH_VERSION" -o \! -s /etc/mkshrc ] || . /etc/mkshrc
                                                   
[ -x /usr/bin/arp ] || arp() { cat /proc/net/arp; }           
[ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
~
~
~
~
~
~
~
I /etc/profile [Modified] 2/16 12%

and every time i enter it increases both sides by 1, 3/17 etc And I'm still getting the error no file or directory. Thanks!

Plan B: ...

That was after trying plan b.

Post

uname -a
vi /etc/profile

modify file:

...
export PS1='\u@\h:\w\$ '
export TERM=xterm-colo
...

Plan C:

Upgrading the Linino image on the Yún

http://arduino.cc/en/Tutorial/YunSysupgrade

On it now!
Thank you so much for taking the time to help me!