This project is about using Xbee API mode to transmit sound detection data in a wireless. Keywords Sound detection, Arduino, Raspberry Pi, Xbee API mode.
The continuation of this post is here. So, since I have the little Pi working with the internet, how about getting an XBee hooked to it and start receiving data from my home network? Well, it isn't hard to hook an XBee up to a Pi; the little device uses 3.3 volts and has an output pin, so I hooked one up. Four wires later, I had the XBee attached, powered and ready to go. Of course, the serial port (the only one on the Pi) is already used for a console, but there's (again) a thousand web sites out there that show how to change the init files to allow the serial port to be used directly, so I used one of the examples and freed the serial port for use.
A quick aside here. In looking around the web, I haven't found a single instance where someone successfully hooked a Pi up to an XBee and did something real with it. This didn't bode well for me, I want this thing to monitor a network of a dozen XBees and keep the devices they're attached to under control, not print pretty messages on the screen. It actually looks like a lot of people buy the Pi because it's cool and don't do much besides bring up a little web server on it. But, the first thing is to see how I can read data from the XBee on the Raspberry Pi.
So, prowling around I found an XBee library for Python and it seems to support everything I need as well as some things I might want to use someday. I installed it on the Pi and wrote a little test program to see what happens. #!/usr/bin/python import serial from xbee import ZigBee serialport = serial.Serial('/dev/ttyAMA0', 9600) zb = ZigBee(serialport) while True: try: data = zb.waitreadframe #Get data for later use #print data # for debugging only print data'rfdata' except KeyboardInterrupt: break serialport.close This actually wasn't as simple as it looks. This tiny little piece of code took almost all day to get to work and gave me a lot of trouble. Sure, it looks easy, but that's after failing over and over again. Most of my problems came from not having a clear example to work from and a horrible dearth of documentation. As usual, libraries provided by community efforts have minimal documentation, but this was an example of even less than that.
There was also problems with not having any description of the proper XBee settings. For example, the author wrote that the XBee had to be in API mode, so following the Andrew Rapp library example, I set it to API mode 2. I got nothing. That led me to look at the code for the library and it turns out this library uses API mode 1 by default.
Switching to API mode 1 allowed me to actually see data. And, there were a number of problems like that. Another one that drove me nuts for an hour or so was that he (the author) sees the XBees as either Series 1 which uses different packets or Series 2 which is ZigBee based (mostly), but requires you to use special classes for each of them So, if you import like this: from xbee import XBee I won't work at all using a series 2 XBee running the various ZigBee software. It will work with the older software.
That's why the example above has: from xbee import ZigBee The author didn't understand that the series 2 devices can run multiple styles and versions of the protocols. So, I can read XBee packets and pick the data out of them to do something with, but now along comes another problem.
All he supplies is a blocking read. That's about useless for any project that needs to be able to catch packets and do something else at the same time. He does have an asyncronous read in the code, but it uses threads to set aside a process that handles the XBee interaction, but since threads can't share data, it would require interprocess communication or queues to pass the data around. Something like that takes all the usefullness out of an incredibly simple device like the XBee. If you have to have multiple processes, queues, locks, and such to read a message, no one is going to bother. I'm still looking at it, but this particular path doesn't show much promise so far. It is true the Raspberry Pi draws a lot more power than the Arduino, but it can do so much more.
For power, check out the Motorola Micro-USB charger, around $5 on Amazon. I use a USB keyboard and an LCD monitor from Goodwill to get mine configured as a headless server. From there I do everything through an secure shell connection. I have several of these Pis between home and work, used for network monitoring, environmental control and web severs.
One, makes a great Xbee to Ethernet gateway using ideas from your blog. I just love them. Have fun with yours. I remember looking at Skinner's blog and several others as I was first working with the XBee, but I basically wound up doing it on my own using the Digi documentation. There is so much mis-information out there that doing it from the docs was easiest. If you have a specific question on getting started with the XBee, drop a note here somewhere, and I'll try to help you. Once you get started, it turns into fun again.
The thing about my code up on this site is that I'm actually running it. It may have a few changes to add things or fix bugs, but what I post is what I actually run (warts and all). Once I got my first received message from an XBee on the Pi, I was off and running.
My first and main question is where did you import your xbbe-python library from? Could you paste a link. I just don't want to have to download a bunch of different libraries and comb through them to see if they even work or have enough functionality. Also how did you set up your network so that all of the nodes send data at relatively the same time? I'm sleeping mine and sending data every hour but what I'm not sure of is how to get all of them to send the data at pretty much the same time. I'm thinking that I need to set the SM to 5 on the end devices in xctu and then send out the ST SN and SP as a broadcast so that every node receives the instructions at the same time and are all timed together. How well do xbees keep time?
Could I set up a network that would take a reading at say 4am everyday and not drift to much from that or would I have to send out commands to re-time the nodes every so often? Sorry to ask for more than I've given. I hope to test out most of this over the next two weekends but I have a mid-term paper that is due on Monday so I have to dedicate some time to that. I used pip to install xbee and got version 2.1.0 the command was: sudo pip install xbee Then you can use pip to tell you where it wound up.
Once you get it installed, you can just import it and start using it. I have an arduino hooked to most of my XBees. There is one standalone thermometer and three standalone battery monitors. The standalone devices are just set up to transmit roughly once a minute and they do it whenever they want to, there's no coordination.
The ones with XBees receive a time sync packet from a clock I made that has a GPS chip in it. It sends the time about every 15 seconds as a broadcast that each device picks up and sets their internal clock to.
I use the arduino time and timeAlarm libraries to keep track of time and schedule events. Things like automatically turning on motors and things are basically alarms that fire at a preset time. I don't have any real time clocks because I didn't want to worry about batteries. Doing it this way, I have semi-autonomous devices that report back to a monitor and I used the monitor to either override or control the devices. If there's a power failure, each device takes care of its own init and startup. Some of the devices wait until a time signal comes in to really go to work, other can start without it.
Details are under each device. Heh, you didn't follow the link at the top of the post did you? But, the reasons for not hooking it up with USB at first are: I didn't have a USB adapter handy. I wanted to see how it would work direct. Other folk reading this might not have a USB adapter.
I wanted to use as little hardware as possible. I didn't think of it until later. I have it hooked up to the USB port now. Actually, I have two of them hooked to the USB ports handling different networks for different purposes. Check around the blog, it's all there. Hi, Unable to receive data in XBee coordinator radio connected to raspberry pi from Xbee router radio connected to PC.
Xbee coordinator radio configurations are as below. Connected to Raspberry pi- RPi 3.3 volt to XBee 3.3 volt pin,Ground to XBee ground, Rx to Xbee Tx(Data Out),Tx to XBee Rx (Data In) Product family: XB24-ZB Function set: ZigBee coordinator API Firmware version: 21A7 Port: COM7 - 9600/8/N/1/N- API 1 Written and not default values PAN ID: 2001 DH: 13A200 DL: 40D85671 (my router id) rest all values are default 2. Xbee router configurations are as below Connected using XBEE exploere USB to PC and using next Gen X-CTU Product family: XB24-ZB Function set: ZigBee Router AT Firmware version: 22A7 Port: COM17 - 9600/8/N/1/N- AT Written and not default values PAN ID: 2001 rest all values are default (DH & Dl are 0) 3.
Running below python script on Raspberry pi to get the data sent by router ####### import pprint import serial import xbee SERIALPORT = '/dev/ttyAMA0' BAUDRATE = 9600 serport = serial.Serial(SERIALPORT, BAUDRATE) xbee1 = xbee.zigbee.ZigBee(serport) while True: try: datasamples = xbee1.waitreadframe pprint.pprint(datasamples) except KeyboardInterrupt: break serport.close ######### Pleas help me to make it work. Hi, I am able to send and receive messages when i connect xbee radios(series 2) to PC but when i connect them to raspberry pi i am not able to receive any message. Router is in AT mode (22A7) and Coordinator is also in AT mode(20A7). Coordinator connected to PC(Next gen X-CTU) and router connected to raspberry pi. Below is python script to receive messages.
Import serial import pprint from xbee import ZigBee serialport = serial.Serial('/dev/ttyAMA0', 9600) zb = ZigBee(serialport) while True: try: data = zb.waitreadframe #Get data for later use #print data # for debugging only pprint.pprint(data) print data'rfdata' except KeyboardInterrupt: break. I don't see anything wrong with your code, but did you make sure the XBees are talking by looking at the network connections in XCTU? Sometimes, when you unplug the coordinator, you have to reestablish the network since the XBee might start up on a new channel.
Also, try to just read the serial port on the pi using cat with input redirected to the serial port to see if it opens and can receive data. You'll get gibberish since the baud rate is probably wrong, but it will tell you if it sees something. Exactly, on my network the coordinator XBee is running in a clock I made that syncs all the house's actions to GPS satellite time. This little machine doesn't do anything else.
All my XBee messages are addressed to a Pi that handles the data. The rest of the XBees are scattered around the garage, barn, pool, etc.
I let the XBee mesh network handled the actual message routing on its own. So, the temperature sensor out on the fence wakes up from being asleep for a couple of minutes, sends a message to the Pi which is routed by the closest XBee to it forward. I don't have to do anything to have this happen, the XBee does all that for me.
Hello, I'm a bit new to xbee and raspberry pi in general, so bare with me here. I have an xbee set up sending data from an Arudino. I'm must using serial.println to send the data. This works fine when I do serial.ReadString on an arduino. So I know the xbee is configured properly.
Now I'm moving the 'receiver' to the raspberry pi 3 and am having difficulties reading the data from serial. My xbee and raspberry pi Except I don't have the DIN pin on the xbee wired to the tx pin on the Raspberry Pi (I only need to receive data on the raspberry pi). Anyways, I and following. I've made sure the baud rates are correct (set to 9600 on the xbees). I've installed xbee and pyserial using pip. I've changed SERIALPORT = '/dev/ttyAMA0' to SERIALPORT = '/dev/ttySO' since I'm using a pi 3.
It seems to hang on response = xbee.waitreadframe I've since then removed the import statements for the xbee stuff and have also tried import serial import time ser = serial.Serial('/dev/ttyAMA0', 9600) while 1: data=ser.readline And that also hangs on the ser.readline. Any idea of what I may be doing wrong?
Are there any additional steps I need to be taking? Are you using a series 1 xbee or series 2? I used series 1 a lot in university so I can probably help you a bit Can you try and run 'minicom -b 9600 -o -D /dev/ttySO' and see if anything is being received from your arduino? If that doesn't work try and connect the TX pin and enter command mode on the xbee ( and wait 3 seconds) and see if you get OK back to see if it's configured properly. Also are you using the pi user or another user? And if you get OK back, are you sending a frame in API or transparent mode?