Here is a quick note on using USB serial with Python. I hooked up a Sparkfun explorer board with a Xbee series 1.
Sparkfun Xbee Explorer |
First you need to install the Serial library:
apt-get install python-serial
The Sparkfun adapter uses FTDI so it shows up as
/dev/ttyUSB0
I found this, it works really well.
Simple TCP/IP bridge
http://pyserial.sourceforge.net/examples.html#tcp-ip-serial-bridge
I started with this link:
http://www.doctormonk.com/2012/04/raspberry-pi-and-arduino.html
I couldn't get it to show data from the Xbee at first so I used Minicom to test:
http://codeandlife.com/2012/07/29/arduino-and-raspberry-pi-serial-communication/#more-859
Somehow it started working once I saw data using Minicom.
Here is the final code to test read from serial with Python
[xbee_read.py]
#!/usr/bin/python
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.open()
try:
while 1 :
result = ser.readline()
print result
except KeyboardInterrupt:
ser.close()
Now I don't need to use my desktop as a Serial-TCP/IP gateway and I can use Python to process the data from the Smart Outlet or anything else. Or talk directly to my robots.
This raspberry Pi is becoming more helpful by the day.
Now I am not sure if multiple devices can use the serial at the same time, have to look into that or combine a the TCP/IP server with extra later for processing data. Maybe I could make all my Python script use the TCP/IP gateway instead of Serial. I might use API mode on the xbee to talk to multiple xbees or have packet headers to know which device they come from.
Update: The first time I did this it worked, then while writing this I couldn't get it to work again. I will have to try again later with a powered USB hub, but I am pretty sure that isn't the problem. It is the same problem I have originally where there isn't any output from the Xbee. Now Minicom just exits instead of showing the data from the Xbee.
08/28/2012 Update 2: I ended up connecting the Xbee to the UART. You need to disable getty first which you can find here: http://www.irrational.net/2012/04/19/using-the-raspberry-pis-serial-port/
After I got it working on the UART minicom still wouldn't work. Then I realized it has to be run with SUDO! D'oh! Now I might try the USB again, but reading it via Python didn't work over USB anyway, which I did run with Sudo. At least now I freed up a USB port. I have to at least try USB again.
08/28/2012 Update 3: I tired again with USB and it works but only if you run Minicom first using sudo. Once you do that then the Python script will work. I am going to stick with using the UART which works perfectly.
If you get it to work reliably let me know in the comments.
I was hitting the same thing with my scripts: http://www.ciscomonkey.net/xbee-cheerlights/
ReplyDeleteI found that after you exit Minicom, it would work. If you look at the settings right as you exit Minicom (stty -F /dev/ttyUSB0) you'll see that the speed is set to 0. If you run stty -F /dev/ttyUSB0 speed 0 you'll get some bogus error that it can't complete, but it actually does set the speed to 0.
I added that shell command to the start of my scripts and it works every time now.
I still haven't had time to dig into the root cause though.