Saturday, February 26, 2011

Arduino to Chumby to UDP

So after getting Chumby Python + PySerial on a USB stick, I hooked up a USB hub to the Chumby One, hooked up the USB stick to the hub (which ends up at /mnt/usb on the Chumby), and also hooked up the Arduino to the USB hub:




Then I wrote this program on the Chumby:

from socket import *
import serial
from time import sleep
ser=serial.Serial('/dev/ttyUSB0',9600,timeout=0)
s=socket(AF_INET,SOCK_DGRAM)
s.bind(('',10001))

while 1:
data = ser.read(256)
if len(data) > 0:
print "Got:",data
s.sendto(data,('[my server IP addr]',10001))
sleep(0.5)
print "not blocked"

Note that this program is not blocked waiting for Serial input from the Arduino. On my Internet server, I wrote:

from socket import *
s=socket(AF_INET,SOCK_DGRAM) # create UDP socket
s.bind(('[my server IP addr',10001)) # bind to port 10001

while 1:
[msg,addr]=s.recvfrom(256) # receive packet of up to 256 bytes
print "addr:",addr," message:",msg

Then everything that the Arduino sends out on Serial to the Chumby goes over UDP to my Internet server.

This is easy!

No comments: