Sony Arouje

a programmer's log

Connecting XBee to Raspberry pi

with 19 comments

Last one week I was doing some research in 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. Also I don’t need to setup a wifi network in my controller RPi’s.

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 text message to coordinator. Below diagram will show you, 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 and I use one from them.

connection_diagram_bb

image developed using Fritzing

I connected the XBee directly using jumper wires, the above diagram is just for illustration for that I use the breadboard.

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: "0013A200407A25A7",
        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.

 

download

Scan using your mobile phone and access/share the post from your phone.

Written by Sony Arouje

December 20, 2014 at 6:07 pm

Posted in Raspberry Pi

Tagged with , ,

19 Responses

Subscribe to comments with RSS.

  1. […] in the age of connected devices, devices could talk each other either via RF or wired. In one of my post I explained about Radio Frequency communication using Xbee. In this post lets see how devices can […]

  2. Sir I actually am working on something similar.But I am very new to raspPi.Can u guide me as to how to program the serial port and make it communicate with the Zigbee, transmitting and receiving data.Can u please provide me with some of your sources of guidance
    (URL’s etc).

    Anonymous

    July 9, 2015 at 12:08 pm

  3. Hi sir,

    i am manikanta ,iam new to the xbee .i want to interface xbee to the raspberry pi in raspbian os platform without using XCTU software .i have downloaded Digi XBee Ansi C Library from this https://docs.digi.com/display/WirelessConnectivityKit/Libraries link .iam using raspberry pi b+ board.
    please send information to the below mentioned email id .

    manibenwa@gmail.com

    Manikanta

    September 18, 2015 at 4:25 pm

    • Hi Manikanta,
      I don’t have the dev environment set up right now. I am sorry to say I cant give you any help apart from what I wrote in my blog.

      Regards,
      Sony Arouje

      Sony Arouje

      September 18, 2015 at 5:01 pm

  4. Thanks for replay sir
    I found the solution

    manikanta

    September 22, 2015 at 10:42 pm

  5. Hey Sony. Thanks for the post. I’m currently working with two XBee S1 modules with the DigiMesh firmware. The documentation for DigiMesh seems to be somewhat lacking and I’ve been trying to get them connected now for several weeks without luck.

    When I run your code, it appears from the RSS LEDs that one Pi is transmitting and the other Pi is receiving, however it does not seem to log the received frames to the console. Any idea what might be going on here?

    I am new to XBee and have been having a lot of trouble getting them up and running. I tried using Python but without any luck so I thought I would try things out with Node.

    mike forcella

    October 27, 2015 at 11:30 pm

    • Hi Mike,
      I am not in touch with XBee for a long time. I am afraid to say, I dont have the answer to your question.

      Regards,
      Sony Arouje

      Sony Arouje

      October 28, 2015 at 6:09 pm

    • Hello Mike,

      DigiMesh is a proprietary protocol from Digi. So, the ansiC library won’t work at all.
      You need to download the DigiMesh user document.
      Then, you should check the API mode.
      Basically, DigiMesh follows the 802.15.4 protocol which as: Start|Length|API-Specific|CheckSum

      This API Specific, in the DigiMesh, contains the fields for each task. For instance, if you want to transmit a message, you need to check the API Specific to transmit. Each API Specific contain a CMD field and a complementary array of data.

      Click to access 90000991.pdf

      Page 107 of this manual.

      Good luck

      Thiago

      December 30, 2016 at 11:28 pm

  6. […] Raspberry pi: connected Coordinator XBee to one of my RPi. You can see more about the connection in one of my earlier post. […]

  7. Hi sir!
    Does it possible to use coordinator API and router API transfer data?
    I’m trying to send the current time information through the xbee in my project.

    Sam

    February 9, 2017 at 10:19 am

  8. Hi sir! It work greatly now,thanks a lot.
    And what if i want do use more than one Router Xbee with one coordinator?

    S

    February 20, 2017 at 8:11 am

  9. where do u use XBee explorer??

    Anonymous

    July 11, 2017 at 2:26 pm

  10. where do u use XBee explorer??

    Anonymous

    July 11, 2017 at 2:27 pm

  11. Hi sony,
    please can you help me! i m working on a project that must connect an arduino to a raspberry pi 3 via Xbee pro s1, so the arduino must collect the information from the capteur and save it in a R.PI
    if i understand i must configure one Xbee as roter an connect it with one arduino and configure another Xbee as coordinator and connect it with a raspberry pi?
    if is that true , can you tell me exactly the steps i need to do so :.
    thanks

    youssef

    July 17, 2017 at 9:41 pm

    • I dont use xbee any more and I totally forgot most of the steps to perform.

      Sony Arouje

      January 15, 2018 at 12:19 pm


Leave a comment