UDP supported?

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.

Yun's python version:

python  -V
Python 2.7.3

My iMac's python version:

python  -V
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/

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?

sonnyyu: 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)

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.

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")

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. :fearful:

I never code python on Mac or Windows but Linux:

#!/usr/bin/python3
....

It will run at 3.X

#!/usr/bin/python
...

It will run at 2.X

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

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 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 :)

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.

Reply #25

As sonnyyu reported, UDP is currently not supported by Bridge directly, so you'll have to run a python script

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

@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.