Sony Arouje

a programmer's log

Communication between Raspberry Pi and Arduino using XBee

with 18 comments

Recently I was doing some experiments to establish a wireless communication between a Raspberry pi and Arduino. To establish wireless communication I used XBee Pro Series 2 from Digi International. The idea behind this test setup is to test, whether I can control devices like motor or read different sensors remotely.

One of the biggest advantage of using XBee is, you can find a lot of tutorials and libraries for any kind of system and programming languages. For this test app, I used Node js in RPi and C in Arduino.

Test Setup

XBee: I configured two xbee, one as Coordinator and another as Router. Both in API mode 2 (AP =2). I used XCTU to configure both the device. Only reason to choose API 2 is because the Arduino library I used only support API mode 2.

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

Arduino Uno: connected the Router xbee to one of my Arduino. The connection is pretty simple as below.

  • XBee Rx –> Arduino Tx
  • XBee Tx -> Arduino Rx
  • XBee 3.3v-> Arduino 3.3v
  • XBee Gnd –>Arduino Gnd

 

Raspberry Pi Node js code

Modules used

  • 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: 2 }); var serialport = new SerialPort("/dev/ttyAMA0", { baudrate: 9600, parser: xbeeAPI.rawParser() }); var frame_obj = { type: 0x10, id: 0x01, destination64: "0013A200407A25AB", broadcastRadius: 0x00, options: 0x00, data: "MTON" }; serialport.on("open", function () { serialport.write(xbeeAPI.buildFrame(frame_obj)); console.log('Sent to serial port.'); }); // All frames parsed by the XBee will be emitted here xbeeAPI.on("frame_object", function (frame) { console.log(">>", frame); if(frame.data!== undefined) console.log(frame.data.toString('utf8')); });

 

 

Arduino Sketch

This sketch uses a XBee library, to add the library, goto Sketch->Include Library->Manage Libraries. From the window search for XBee and install the library. I am using Arduino IDE 1.6.7.

I use SoftwareSerial to establish serial communication with XBee, Pin 2 is Arduino Rx and Pin 3 is Arduino Tx.

 

#include <Printers.h> #include <XBee.h> #include <SoftwareSerial.h> unsigned long previousMillis = 0; const long interval = 4000; // the interval in mS XBee xbee = XBee(); // XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX) // XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX) SoftwareSerial soft(2,3);// RX, TX Rx16Response rx16 = Rx16Response(); ZBRxResponse rx = ZBRxResponse(); XBeeAddress64 addr64 = XBeeAddress64(0x0013a200,0x407a25b5); char Buffer[128]; char RecBuffer[200]; void setup() { // put your setup code here, to run once: soft.begin(9600); Serial.begin(9600); xbee.setSerial(soft); } void print8Bits(byte c){ uint8_t nibble = (c >> 4); if (nibble <= 9) Serial.write(nibble + 0x30); else Serial.write(nibble + 0x37); nibble = (uint8_t) (c & 0x0F); if (nibble <= 9) Serial.write(nibble + 0x30); else Serial.write(nibble + 0x37); } void print32Bits(uint32_t dw){ print16Bits(dw >> 16); print16Bits(dw & 0xFFFF); } void print16Bits(uint16_t w){ print8Bits(w >> 8); print8Bits(w & 0x00FF); } void loop() { // put your main code here, to run repeatedly: xbee.readPacket(); if (xbee.getResponse().isAvailable()) { if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) { xbee.getResponse().getZBRxResponse(rx); if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) { // the sender got an ACK Serial.println("got ACK"); } else { // we got it (obviously) but sender didn't get an ACK Serial.println("not got ACK"); } Serial.print("Got an rx packet from: "); XBeeAddress64 senderLongAddress = rx.getRemoteAddress64(); print32Bits(senderLongAddress.getMsb()); Serial.print(" "); print32Bits(senderLongAddress.getLsb()); Serial.println(' '); // this is the actual data you sent Serial.println("Received Data: "); for (int i = 0; i < rx.getDataLength(); i++) { print8Bits(rx.getData()[i]); Serial.print(' '); } //Received data as string to serial Serial.println(' '); Serial.println("In string format"); for (int i = 0; i < rx.getDataLength(); i++) { if (iscntrl(rx.getData()[i])) RecBuffer[i] =' '; else RecBuffer[i]=rx.getData()[i]; } Serial.println(RecBuffer); String myString = String((char *)RecBuffer); if(myString=="MTON"){ Serial.println("Switching on Motor"); } else if(myString=="MTOFF"){ Serial.println("Switching off Motor"); } } //clear the char array, other wise data remains in the //buffer and creates unwanted results. memset(&RecBuffer[0], 0, sizeof(RecBuffer)); memset(&Buffer[0], 0, sizeof(Buffer)); } //Send a packet every 4 sec. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; strcpy(Buffer,"RSLOW"); uint8_t payload[20]= "RSLOW"; // ZBTxRequest zbtx = ZBTxRequest(addr64,(uint8_t *)Buffer,sizeof(Buffer)); ZBTxRequest zbtx = ZBTxRequest(addr64,payload,sizeof(payload)); xbee.send(zbtx); } }

 

Burn the sketch to Arduino.

Testing

Run node js code in RPi and you start receiving the frames from Arduino.

 

 

Happy coding….

Written by Sony Arouje

January 21, 2016 at 12:06 am

18 Responses

Subscribe to comments with RSS.

  1. I want to connect xbee coordinator in API mode to raspbery pi and xbee router in API mode to arduino , and coonect one or more sensors to arduino and read this data remotely from raspbery pi through xbee .
    I wish I could explain my idea because I have many many problem and I can not solve any think and I now still do nothing…. please if you can help me
    Thanks alot

    taif503

    October 13, 2016 at 12:20 pm

    • what did you try to achieve the communication? Have you created any codes?

      If you havent done anything, probably you need to start doing some thing. If you find any difficulty with the code, post a question in Arduino.cc forum and me or some one else can help you better.

      Sony Arouje

      October 13, 2016 at 2:22 pm

  2. Can you please guide me how to implement mqtt on same infrastructure….What I am trying to achieve is i have a PIR sensor(hc-sr501 ) connected to arduino uno and i want to send o/p data(only in the form of 0’s and 1’s) to raspberry pi(local server) using mqtt protocol over zigbee.

    hacktipsadmin

    November 30, 2016 at 3:44 pm

    • I am not so sure whether MQTT works in xbee. MQTT is more of an internet protocol and xbee works in zigbee protocol.

      Sony Arouje

      December 9, 2016 at 9:44 am

  3. Thanks for your sharing.
    Because I need to let my Arduino go to sleep and wake up every 30s.
    The Arduino will send the data when it wake up and then go to sleep.
    What I think is make “ACK” at Pi and send it when Pi receive one of Xbee router’s data.

    Does the code below are necessary?
    serialport.on(“open”, function () {
    var frame_obj = {
    type: 0x10,
    id: 0x01,
    //destination64: “0013A20040D8DA8D”,
    destination64: “0013A20040D8DA68”,
    destination64: “0013A20040D8DA93”,
    broadcastRadius: 0x00,
    options: 0x00,
    data: “ack”
    };
    serialport.write(xbeeAPI.buildFrame(frame_obj));
    console.log(‘Sent to serial port.’);
    });

    Because what I’m trying is just listen and send the ack when Pi receive any Xbee router’s data.

    Sam

    February 21, 2017 at 10:35 am

  4. Hi sir, thanks for your sharing
    I’m trying to send a frame from coordinator to other xbee periodically.
    But at the same time I’m using xbeeAPI.on(“frame_object”, function (frame) to receiving frames.
    That makes the serial port be locked and can’t use it!
    How should i solved this kind of problem?
    Thank a lot. good day.

    Sam

    March 24, 2017 at 11:47 am

    • Sam, where u able to find a solution for this?

      Lucas

      July 20, 2017 at 11:54 am

  5. Hello sir, thanks for sharing
    but i have to write Rpi program in python since i have no knowledge of node js
    and also i am using speech recognition in python and send the converted string to arduino.
    any help would be great.

    prabes

    July 22, 2017 at 8:25 am

    • I haven’t used python for this experiment , you can search for xbee library for python.

      Sony Arouje

      July 22, 2017 at 9:18 am

    • Prabes, I am using python on the pi to talk to my Arduino. I used the python-xbee library. Maybe I could help u out

      Lucas

      July 22, 2017 at 10:19 am

      • lucas, sorry for the late reply . I am also trying to talk to arduino through pi using arduino and haven’t been able to make much progress so far so any help would be greatly appreciated.

        prabes

        July 28, 2017 at 8:14 am

  6. Prabes, I am using python on the pi to talk to my Arduino. I used the python-xbee library. Maybe I could help u out

    Lucas

    July 22, 2017 at 10:18 am

  7. i want to transfer a string from rpi3 to arduino using xbee modules
    will this code work for my project

    harshitha

    April 8, 2018 at 2:30 pm

    • yes it should work, if you configure the xbee correctly. Now I am using HC12 as its easy to work with.

      Sony Arouje

      April 10, 2018 at 9:54 am

  8. Lovvely blog you have

    Ellis

    January 17, 2023 at 4:51 am


Leave a comment