Connecting XBee to Raspberry Pi
To configure the XBee’s I use the X-CTU software from Digi, you can download it free from Digi’s website.
Join the DZone community and get the full member experience.
Join For Freelast week i was doing some research on rf communication and controlling devices using rf. so what am i going to control here, i wanted to control the water pump running in my hydroponic reservoir without running wires from my raspberry pi. this way i can extend my hydroponic system to more balconies without buying extra rpi, a single rpi will send a switch on/off command and the rf client will switch on/off the motor via a relay.
my research for rf communication platform leads to zigbee protocol and xbee component. xbee is a very popular zigbee complaint product from digi . for my testing i got two xbee pro s2 and two xbee explorer. the explorer i bought uses usb to a/b cable, if you are buying it make sure buy a/b cable as well. you will get micro usb explorer as well.
to configure the xbee’s i use the x-ctu software from digi, you can download it free from digi’s website . i use the legacy x-ctu for configuring my modules and next generation x-ctu for issuing commands. configuration is pretty simple and so many sites will walk you through it. i configure my xbee’s as shown below. one xbee act as coordinator and enabled api mode and another xbee act as router and enabled router at.
xbee coordinator
-
modem: xbp24-zb
-
function set: zigbee cordinator api
-
pan: <set a pan id, say 123>
-
destination address low: ffff
xbee router
-
modem: xbp24-zb
-
function set: zigbee router at
-
pan: <set a pan id, say 123>
-
destination address low: 0000
i left all other settings as default.
connecting to raspberry pi
for testing i connected the xbee coordinator to my computer and xbee router to my rpi. in rpi i created a simple node app to send some thing to coordinator. below diagram will show, how i connected router xbee to my rpi. here we use serial communication between rpi and xbee. rpi has only one set of serial communication pin and by default it’s configured for console i/o, there are so many tutorial out there to free it up. i use one from them .
image developed using fritzing
i connected the xbee directly using jumper wires, the above diagram is just for illustration and i use the bread board.
connect 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).
sending some data from router xbee to coordinator
now i wanted to send some data from router xbee connected to my rpi to the coordinator connected to my computer.
i created a small app using node js, below is the code. to run the code we have to install two node modules.
- xbee-api : npm install xbee-api
- serialport : npm install serialport
var util = require('util');
var serialport = require('serialport').serialport;
var xbee_api = require('xbee-api');
var c = xbee_api.constants;
var xbeeapi = new xbee_api.xbeeapi({
api_mode: 1
});
var serialport = new serialport("/dev/ttyama0", {
baudrate: 9600,
parser: xbeeapi.rawparser()
});
serialport.on("open", function () {
var frame_obj = {
type: 0x10,
id: 0x01,
destination64: "0013a200407a25b5",
broadcastradius: 0x00,
options: 0x00,
data: "hello world"
};
serialport.write(xbeeapi.buildframe(frame_obj));
console.log('sent to serial port.');
});
serialport.on('data', function (data) {
console.log('data received: ' + data);
});
// all frames parsed by the xbee will be emitted here
xbeeapi.on("frame_object", function (frame) {
console.log(">>", frame);
});
when i run the above node app, i can see the data receiving in my coordinator’s x-ctu app. i could also send a remote at (0x17) command to the router to one of the digital pin and could turn on/off a led.
seems like the communication is working fine. let’s see what i can come up next.
Published at DZone with permission of Sony Arouje, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
The Native Way To Configure Path Aliases in Frontend Projects
-
What Is JHipster?
-
Multi-Stream Joins With SQL
-
Managing Data Residency, the Demo
Comments