Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

XBee (Zigbee Complaint) Communication Using API Mode

DZone's Guide to

XBee (Zigbee Complaint) Communication Using API Mode

XBee is a very popular ZigBee complaint product from Digi. This article lists all the steps required for implementing API mode communication under one roof.

· IoT Zone
Free Resource

Build an open IoT platform with Red Hat—keep it flexible with open source software.

XBee is a very popular ZigBee complaint product from Digi. You might be aware that, there are two ways to establish a communication between XBee devices, one using AT mode and other using API mode. You will find dozens of articles on the web related to AT mode but for API mode implementation, you will find very few articles with information scattered around multiple articles.  Therefore, I thought writing a blog that can list all the steps required for implementing API mode communication under one roof would help the community. This blog will give an end to end steps required for establishing communication between two XBee modules in API mode.

Hardware Required:

  • Two Arduino UNO Boards
  • Two XBee modules
  • Two Arduino XBee shields
  • XBee USB explorer


Software Required:

  • Download and install X-CTU software for configuring XBee devices and updating firmware.
  • Arduino IDE


Updating firmware

In general XBee module comes with AT firmware. The XBee module’s firmware needs to be updated in order to use API firmware. Follow the steps mentioned below for upgrading the firmware:

  • Run X-CTU.exe program
  • Mount XBee module on XBee USB explorer and connect  it to PC
  • Click on Add radio modules icon and select the serial port to which XBee USB explorer is connected
  • Leave the other settings to default and click on ‘Finish’
  • Select the radio module to display its properties and configurations
  • Click on update firmware icon and select the product family, function set and the latest firmware version
  • Click on ‘Finish’ to complete firmware upgradation


Configuring XBee modules:


To establish a bidirectional communication between two XBee modules, one XBee has to be upgraded with coordinator API firmware and the other with router/end node API firmware


After upgrading firmware on both the XBee modules follow the below steps to configure them using X-CTU.


XBee coordinator

  • ID - PAN ID – any number between 0-65535
  • DH – Destination Address High – 0
  • DL – Destination Address Low – 1
  • AP – API Enable – 2
  • EE – Encryption Enable – 1 (for implementing secured network)
  • KY – Encryption Key


XBee router

  • ID - PAN ID – Same as the coordinator
  • DH – Destination Address High – 0
  • DL – Destination Address Low – 0
  • AP – API Enable – 2
  • EE – Encryption Enable – 1 (for implementing secured network)
  • KY – Encryption key – same as the coordinator

Leave all other settings to default


Programming:


xbee-arduino library is used in order to establish the communication between coordinator  and router/end node.

Code for transmitting data:

#include <XBee.h>
XBee xbee = XBee();
void setup() {
  Serial.begin(9600);
  xbee.setSerial(Serial);
}
void loop() {
  uint8_t data[] = {'H','I'};
  XBeeAddress64 addr64 = XBeeAddress64();
  addr64.setMsb(XXXXXX);//XXXXX -> Msb address of router/end node
  addr64.setLsb(XXXXXX);//XXXXX -> Lsb address of router/end node 
  ZBTxRequest zbTx = ZBTxRequest(addr64, data, sizeof(data));
  xbee.send(zbTx);
  delay(100);
}

Code for receiving data

#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
void setup() {
  Serial.begin(9600);
  xbee.begin(Serial);
}
void loop() {
String sample;
  xbee.readPacket(); 
    if (xbee.getResponse().isAvailable()) {
      Serial.println(xbee.getResponse().getApiId());
      if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        xbee.getResponse().getZBRxResponse(rx);
        for (int i = 0; i < rx.getDataLength(); i++) {
          sample += (char)rx.getData(i);
        }
        Serial.println(sample);
      }
    }else if (xbee.getResponse().isError()) {
      Serial.println("Error reading packet.  Error code: ");  
      Serial.println(xbee.getResponse().getErrorCode());
  } 
  delay(100);
}

After uploading code, open serial monitor of the receiver to check the output transmitted by coordinator.

Download Red Hat’s blueprint for building an open IoT platform—open source from cloud to gateways to devices.

Topics:
iot ,api ,zigbee ,xbee

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}