Arduino Forum

Products => Arduino Yún => Topic started by: RalfK on Sep 10, 2013, 06:49 pm

Title: UDP supported?
Post by: RalfK on Sep 10, 2013, 06:49 pm
Yun seems to be great!

Can i send UDP Packets with Bridge Library?
Title: Re: UDP supported?
Post by: federicofissore on Sep 10, 2013, 09:09 pm
Short answer: no, but you can make a script in python (or your favourite language) and invoke it with the Bridge

Longer: Bridge is made to give your sketch access to the linux console, to a shared in-memory storage to share data between your sketch and the outside world, and to create sort of REST APIs to communicate with your sketch (see the Bridge and the TemperatureWebPanel examples)
In order to play with low level networking, your should create a script with python (or ruby or others) and call that script from your sketch via Bridge
Title: Re: UDP supported?
Post by: RalfK on Sep 11, 2013, 08:03 am
hmmmmmmm .....

understand, this makes YUN really flexible.

But basic stuff like UDP should work out of the box.

Wifi Library and Ethernet Library supports UDP.

If you have existing UDP code (which uses Ethernet or Wifi Library) and you want to migrate to YUN, then you will miss the UDP library ...

i hope this is coming soon


Title: Re: UDP supported?
Post by: RalfK on Sep 17, 2013, 01:43 pm
UDP broadcasts are very helpful to announce hardware on LANs and is necessary for Plug and Play Hardware if YUN acts as a server ...

Title: Re: UDP supported?
Post by: RalfK on Sep 17, 2013, 01:58 pm
no, UDP broadcasts are required
Title: Re: UDP supported?
Post by: RalfK on Sep 17, 2013, 05:45 pm
i want easy UDP support for YUN from the ARDUINO side - out of the box

Most Arduino users want it convenient. The Ethernet Library and WiFi Library did it in a good way.

It looks for me that it is simply forgotten in the YUN Bridge Library

I know that i can do a lot of stuff on the linux side as Federico Fissore already explained, but this it is inconvenient ...


Title: Re: UDP supported?
Post by: federicofissore on Sep 17, 2013, 10:31 pm
@RalfK if your requirements for the broadcasted message are not too strict, you can do what the yun is already doing to notify the IDE of its presence

In folder /etc/avahi/services you'll find an arduino.service file: google for more info on avahi and how it works

Basically, dropping an appropriate .service file in that folder, avahi will broadcast its contents
Title: Re: UDP supported?
Post by: RalfK on Sep 17, 2013, 10:59 pm
Federico, thanks for the hint, i think this will be helpful ...

But for now, i will put the YUN in the corner and progress the project with Arduino Ethernet. Still hoping that the bridge library will be extended soon ...

What do you think, any chance that there will be an updated or extended Bridge Library in the near future?



Title: Re: UDP supported?
Post by: federicofissore on Sep 18, 2013, 09:18 am
There surely will be, not just one, but many updates.
Title: Re: UDP supported?
Post by: richard47 on Mar 22, 2014, 08:07 am

There surely will be, not just one, but many updates.

This thread is now 6 months old!

I would like to use the standard Arduino Ethernet UDP and Wi-Fi UDP libraries with the Yun and I was hoping that this would have been implemented by now.

Failing that would someone post a *complete* sketch that would show me (an oldie) how to send and receive simple UDP messages from the "Arduino" side of the Yun. I have a number of sketches running on a Uno with an ethernet shield and I would like to run these using the Wi-Fi on the Yun.
Title: Re: UDP supported?
Post by: sonnyyu on Mar 22, 2014, 09:02 am
Even use the standard Arduino Ethernet UDP and Wi-Fi UDP libraries at regular arduino might be not smart move.


my 2 cents;-

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.) .
Say send command  set voltage=1 v, then set voltage=4 v, You might get 0v or 1v or 4v if default is 0v. TCP is a better choice if you want robust packet delivery and sequencing.
If you're limited to using UDP you would have to develop a method of identifying the out of sequence packets and resequencing them or request to resend, but for Arduino is tough task.

a lot people implement UDP with Arduino, I mean neither OP nor this thread please hold fire.

I guess either they drink too much Italian coffee or I drink too much, might be both.  :P


Let's stay in TCP.
Title: Re: UDP supported?
Post by: RalfK on Mar 22, 2014, 10:11 am
Quote

Let's stay in TCP.


The advantages of TCP are known

But it is NOT possible to broadcast with TCP

UDP is required !!! 

It is a best practicee to announce a arduino device with UDP broacast in a network. This makes PnP possible ...

Lets stay TCP and UDP
Title: Re: UDP supported?
Post by: richard47 on Mar 22, 2014, 11:14 am

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.
Title: Re: UDP supported?
Post by: sonnyyu on Mar 22, 2014, 11:19 am
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





Title: Re: UDP supported?
Post by: richard47 on Mar 22, 2014, 10:41 pm
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?

Code: [Select]
/*
  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
}
Title: Re: UDP supported?
Post by: sonnyyu on Mar 22, 2014, 11:24 pm
At Yun:

Code: [Select]
nano /mnt/sda1/udp.py

Code: [Select]
#!/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()


Code: [Select]
chmod 755 /mnt/sda1/udp.py

At Linux/Mac box( wait to call):

Code: [Select]
nc -ul 192.168.0.230  8888

At Yun:

Code: [Select]
/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"

Code: [Select]
...
String cmd = "/mnt/sda1/udp.py";
...





Title: Re: UDP supported?
Post by: richard47 on Mar 23, 2014, 07:27 am
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
Title: Re: UDP supported?
Post by: richard47 on Mar 23, 2014, 01:14 pm
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?
Title: Re: UDP supported?
Post by: sonnyyu on Mar 23, 2014, 03:30 pm
Did you like try this?
Code: [Select]
...
recvmsg=sock.recv(1024)
#print recvmsg
sock.close()
Title: Re: UDP supported?
Post by: richard47 on Mar 23, 2014, 06:01 pm

Did you like try this?
Code: [Select]
...
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?
Title: Re: UDP supported?
Post by: sonnyyu on Mar 23, 2014, 06:20 pm
uncomment  "#print recvmsg" for debug code at Yun console.

The code send reply back to Yun:
Code: [Select]
...
import sys  
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
...
bc = bridgeclient()                          
bc.put('to_arduino',recvmsg)
...

Title: Re: UDP supported?
Post by: richard47 on Mar 23, 2014, 06:28 pm
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
Title: Re: UDP supported?
Post by: sonnyyu on Mar 23, 2014, 08:24 pm
At Yun:

Code: [Select]
nano /mnt/sda1/udp.py

Code: [Select]
#!/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()


Code: [Select]
/mnt/sda1/udp.py

make sure return value from nano.

Code: [Select]
#!/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)




Title: Re: UDP supported?
Post by: richard47 on Mar 24, 2014, 07:48 am
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
Title: Re: UDP supported?
Post by: esbjerg on Mar 24, 2014, 03:01 pm
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
Title: Re: UDP supported?
Post by: sonnyyu on Mar 25, 2014, 02:35 am
Code: [Select]
vi /etc/profile
export TERM=xterm-colo


exit ssh then relogin

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

Plan B:

Start from fresh (backup your own file!):

Code: [Select]
/usr/bin/reset-to-factory-anyway
reboot

Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 03:30 pm
If I try

Code: [Select]
vi /etc/profile
export TERM=xterm-colo


I get

Code: [Select]
#!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!
Title: Re: UDP supported?
Post by: sonnyyu on Mar 25, 2014, 04:29 pm
Plan B: ...
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 04:52 pm
That was after trying plan b.
Title: Re: UDP supported?
Post by: sonnyyu on Mar 25, 2014, 05:00 pm
Post

Code: [Select]
uname -a

Code: [Select]
vi /etc/profile

modify file:

Code: [Select]

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




Title: Re: UDP supported?
Post by: sonnyyu on Mar 25, 2014, 05:05 pm
Plan C:

Upgrading the Linino image on the Yún

http://arduino.cc/en/Tutorial/YunSysupgrade (http://arduino.cc/en/Tutorial/YunSysupgrade)
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 05:14 pm
On it now!
Thank you so much for taking the time to help me!
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 05:22 pm
Okay, so nothing to reset is coming up on the arduino.local page. I used my computer to put the file on the SD card.
The SD card is called SD and I put the file in the root folder

EDIT: Tried using terminal too, same message as before, no such file or directory
Title: Re: UDP supported?
Post by: sonnyyu on Mar 25, 2014, 05:41 pm
Re: Problem with microSD

http://forum.arduino.cc/index.php?topic=222069.msg1614584#msg1614584 (http://forum.arduino.cc/index.php?topic=222069.msg1614584#msg1614584)

PC is no needed for helping create file or directory at linino.
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 06:19 pm
Next issue ,followed the steps in that link, I can't  see the sd coming up with  fdisk -l.
Just keep asking if I specified it correctly.
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 11:17 pm
Looking at this link I think it may be faluty.

http://forum.arduino.cc/index.php?topic=201829.0

Going to try this all with a flash drive... I read they mount in similar ways, so thinking that should work the same?
Title: Re: UDP supported?
Post by: esbjerg on Mar 25, 2014, 11:26 pm
Tried with the USB flash drive and I can now write the lines of code! (success)
but when I try and run it I now get permission denied! (aww)

Any ideas?
Title: Re: UDP supported?
Post by: sonnyyu on Mar 26, 2014, 01:43 am
chmod 755  run.py
Title: Re: UDP supported?
Post by: esbjerg on Mar 26, 2014, 01:53 am
Awesome, I can receive the G over on another laptop running Max, woo.
Now I'm trying to send 4 analogue pins over it (and ideally a couple of digital things, but not essential at the moment). Looking into bridge etc, but I can't work out how to get it from the arduino sketch into the python sketch (then onto Max).
I'm guessing that I'll need to change MESSAGE into something? Again, thankyou!
Title: Re: UDP supported?
Post by: esbjerg on Mar 26, 2014, 11:24 pm
Sorry to be that guy, but I feel like I'm so close to having it working!!

To try and get serial input to be posted straight out of the python sketch, it now looks like this:

Code: [Select]
#!/usr/bin/python
import socket
import serial

ser = serial.Serial('/dev/ttyATH0', 9600)

while ser.readline() != 'A':
#   do nothing
   pass

UDP_IP = "192.168.1.242"   #Max IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

data_raw = read(8) #read 8 bytes
sock.sendto(bytes(data_raw), (UDP_IP, UDP_PORT)) #sends the byte
#recvmsg=sock.recv(1024) #read response
#print recvmsg
sock.close()
               


Was getting an indent error (now fixed), but now it seems to run perfectly, but doesn't put anything out into udp...

Thanks again..
Title: Re: UDP supported?
Post by: richard47 on Mar 29, 2014, 09:22 am
I have a general question about developing on the Yun using Python.

If my Python code has an error in it then how can I see the error messages?

I can test the Python code in Python on my iMac but there appear to be differences in the syntax so I could still get an error when the code is run on the Yun.
Title: Re: UDP supported?
Post by: sonnyyu on Mar 29, 2014, 04:35 pm
Yun's python version:

Code: [Select]
python  -V

Code: [Select]
Python 2.7.3

My iMac's python version:

Code: [Select]
python  -V

Code: [Select]
Python 2.7.5

The  differences in the syntax should be very minimum if it exists.

Python's community is vast:

https://www.python.org/community/ (https://www.python.org/community/)
Title: Re: UDP supported?
Post by: richard47 on Mar 30, 2014, 09:12 am
I am running v3.4 on my iMac. Yesterday I tried going back to v2.7 and for some reason I couldn't get it to run any of my Python code.

Is there any way to see errors in the Python code on the Yun?
Title: Re: UDP supported?
Post by: richard47 on Mar 30, 2014, 05:59 pm

At Yun:

Code: [Select]
nano /mnt/sda1/udp.py

Code: [Select]
#!/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()


Code: [Select]
/mnt/sda1/udp.py

make sure return value from nano.

Code: [Select]
#!/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)


sannyyu: I am using your code shown above to control my WiFi lights successfully but now I would like to be able to receive UDP messages from my iPhone/iMac and to send a reply. The messages from the iPhone/iMac can be sent at any time.
I have added the following code to receive UDP messages from my iPhone and to send a reply. However, the Python code hangs and I never see the output from the statement  - print("After dataFromClient"). If I send a UDP message from my iPhone then the print is executed.

I would like the code to continue processing if no UDP data has been received. I am testing this on my iMac running Python v3.4 and sending UDP test messages from my iPhone.

Code: [Select]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('', 6666))
print ("UDPServer listening on port 6666")

while True:
  dataFromClient, address = server_socket.recvfrom(256)
  print ("Received %d bytes: '%s'" % (len(dataFromClient), dataFromClient))
  if (dataFromClient == b'g'):
    msg = 'G,' + str(lightReading)
    server_socket.sendto(bytes(msg, 'utf-8'), address)
  elif (dataFromClient == b'n'):
    msg = 'N,' + str(nSchedules) + str(lightReading)
    server_socket.sendto(bytes(msg, 'utf-8'), address)
  else:
    print ("( " ,address[0], " " , address[1] , " ) said : ", dataFromClient)

  print("After dataFromClient")
Title: Re: UDP supported?
Post by: sonnyyu on Mar 30, 2014, 06:29 pm
Quote
sannyyu: I am using your code shown above to control my WiFi lights successfully but now I would like to be able to receive UDP messages from my iPhone/iMac and to send a reply.


It is sonnyyu.  :smiley-eek-blue:

I never code python on Mac or Windows but Linux:

Code: [Select]
#!/usr/bin/python3
....


It will run at 3.X

Code: [Select]
#!/usr/bin/python
...


It will run at 2.X

I believe that Python 2 and Python 3 could coexist peacefully at Macintosh as well.


Title: Re: UDP supported?
Post by: richard47 on Mar 30, 2014, 08:13 pm
I am using the iMac to develop code that will eventually run on the Yun. I will change the syntax of my Python v3 code to run under v2.7 on the Yun when fully developed.

The problem that I cannot solve is how to get past the line
Code: [Select]
dataFromClient, address = server_socket.recvfrom(256) in my Python code when no UDP message has been sent.

If you can help with that I will be very grateful. In the meantime I will see if I can get Python v2.7 to run on my iMac.
Edit: Python v2.7 running on iMac now so the code I develop should transfer straight across to the Yun :)
Title: Re: UDP supported?
Post by: malcriado on Jun 07, 2014, 07:21 pm
Hi guys,
is there somewhere any sketch for sending udp data from the yun to other devices?
Just starting a project for sending sensors data through udp and have no idea if this can be acomplished with yun wifi.
Thanks in advance.

Francisco.
Title: Re: UDP supported?
Post by: sonnyyu on Jun 08, 2014, 02:01 am
Reply #25

http://forum.arduino.cc/index.php?topic=187287.msg1647539#msg1647539 (http://forum.arduino.cc/index.php?topic=187287.msg1647539#msg1647539)
Title: Re: UDP supported?
Post by: federicofissore on Jun 08, 2014, 03:25 pm
As sonnyyu reported, UDP is currently not supported by Bridge directly, so you'll have to run a python script
Title: Re: UDP supported?
Post by: cvanbeber on Apr 15, 2015, 09:07 pm
I am new to the arduino Yun and its capablity. Could you explain more how to exactly implement a Python code in the Linux side. Do you use the arduino IDE and import the python code or exactly how does it work?

Any help would be much appreciated! Trying to finish a senior design project
Title: Re: UDP supported?
Post by: Angelo9999 on Apr 15, 2015, 10:18 pm
@cvanbeber:
Please start a new thread and expose your question. Don't make generic question.
Try to read some documentation or search in the forum and then make a specific question with issues that you encounter.