Getting started with QtXBee SDK

This guide is made to help you to get started with the QtXBee SDK.

Note
Before getting started, ensure that your XBee module is in API1 mode (ATAP1).

Summary

XBee serial port connection/configuration

There are several way to connect to the XBee' serial port. The easiest one, is to use the default configuration (XBee factory settings), by setting directly the serial port in the XBee's factory default settings. In you do so, the default serial port's configuration will be applied :

  • Baud Rate : 9600
  • Data Bits : 8 bits
  • Parity : No
  • Stop Bits : One stop bit
  • Flow Control : No flow control
#include <QCoreApplication>
#include <XBee>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb("/dev/ttyUSB0");
if(!xb.open()) {
// Handle error
}
// Your code here
return 0;
}

You can also initialise the serial port to the default configuration by doing :

#include <QCoreApplication>
#include <XBee>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
xb.setSerialPort("/dev/ttyUSB0");
if(!xb.open()) {
// Handle error
}
// Your code here
return 0;
}

If your XBee module doesn't use the default serial port configuration, you can specify it in this way :

#include <QCoreApplication>
#include <XBee>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
xb.setSerialPort("/dev/ttyUSB0",
QSerialPort::Baud9600,
QSerialPort::Data8,
QSerialPort::NoParity,
QSerialPort::OneStop,
QSerialPort::NoFlowControl);
if(!xb.open()) {
// Handle error
}
// Your code here
return 0;
}

Or by doing :

#include <QCoreApplication>
#include <XBee>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
xb.setSerialPort("/dev/ttyUSB0");
xb.setSerialPortConfiguration(QSerialPort::Baud9600,
QSerialPort::Data8,
QSerialPort::NoParity,
QSerialPort::OneStop,
QSerialPort::NoFlowControl);
if(!xb.open()) {
// Handle error
}
// Your code here
return 0;
}

Synchronous AT command

This part describes the way to send synchronous AT Commands to the XBee module.

#include <QCoreApplication>
#include <QDebug>
#include <XBee>
#include <ATCommand>
#include <ATCommandResponse>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
ATCommand panId;
ATCommandResponse *rep = NULL;
// Configure serial
// port here
if(!xb.open()) {
qFatal("Failed to open serial port");
}
rep = xb.sendATCommandSync(&panId);
if(!rep) {
qFatal("No response");
}
if(rep->status() != ATCommandResponse::Ok) {
qWarning() << "AT Command Failed with status" << rep->status();
}
else {
qDebug() << "PAN ID =" << rep->data().toHex();
}
delete rep;
return 0;
}

Asynchronous AT command

This part describes the way to send asynchronous AT Commands to the XBee module.

//main.cpp
#include <QCoreApplication>
#include <XBee>
#include <ATCommand>
#include <ATCommandResponse>
#include "xbeeresponseprinter.h"
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBeeResponsePrinter printer;
XBee xb;
ATCommand panId;
// Configure serial
// port here
if(!xb.open()) {
qFatal("Failed to open serial port");
}
QObject::connect(&xb,
SIGNAL(receivedATCommandResponse(ATCommandResponse*)),
&printer,
SLOT(onPacketReceived(ATCommandResponse*))
);
xb.sendATCommandAsync(&panId);
return a.exec();
}


//xbeeresponseprinter.h
#ifndef XBEERESPONSEPRINTER
#define XBEERESPONSEPRINTER
#include <QObject>
#include <QDebug>
#include <ATCommandResponse>
using namespace QtXBee;
class XBeeResponsePrinter : public QObject {
Q_OBJECT
public:
explicit XBeeResponsePrinter(QObject *parent = 0) : QObject(parent) {}
~XBeeResponsePrinter() {}
public slots:
void onPacketReceived(ATCommandResponse *packet) {
qDebug() << "Received packet";
qDebug() << qPrintable(packet->toString());
}
};
#endif // XBEERESPONSEPRINTER

In my case, the result is:

Received packet
Raw packet : 0x7e00078801494400333284
Frame id : 1 (0x1)
Frame type : AT Command Response (0x88)
AT command : ID (0x4944)
Start delimiter : 0x7e
Length : 7 bytes
Data : 0x3332
Checksum : 4294967172
Command Status : OK (0x0)
Raw packet : 0x7e00078801494400333284
Frame id : 1 (0x1)
Frame type : AT Command Response (0x88)
AT command : ID (0x4944)
Start delimiter : 0x7e
Length : 7 bytes
Data : 0x3332
Checksum : 4294967172
Command Status : OK (0x0)

Synchronous remote AT command

#include <QCoreApplication>
#include <QDebug>
#include <XBee>
#include <RemoteATCommandRequest>
#include <XBeeResponse>
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
XBeeResponse * rep = NULL;
// Configure serial
// port here
if(!xb.open()) {
qFatal("Failed to open serial port");
}
remoteNI.setCommand(ATCommand::ATNI); // Node Indentifier
remoteNI.setDestinationAddress16(0x2);
rep = xb.sendSync(&remoteNI);
if(!rep) {
qFatal("No response");
}
qDebug() << qPrintable(rep->toString());
qDebug() << "Node Identifier =" << rep->data();
delete rep;
return 0;
}

In my case the result is:

Raw packet : 0x7e001697020013a20040cabb3800024e4900576561746865724b
Start delimiter : 0x7e
Frame type : Remote AT Command Response (0x97)
Length : 22 bytes
Frame id : 2
Data : 0x57656174686572 (Weather)
Source Address 64bits : 0x13a20040cabb38
Source Address 16bits : 0x2
AT Command : NI (0x4e49)
Command Status : OK (0x0)
Checksum : 75
Node Identifier = "Weather"

Asynchronous remote AT command

//main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <XBee>
#include <RemoteATCommandRequest>
#include "xbeeresponseprinter.h"
using namespace QtXBee;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
XBeeResponsePrinter printer;
// Configure serial
// port here
xb.setSerialPort("/dev/ttyUSB0");
if(!xb.open()) {
qFatal("Failed to open serial port");
}
QObject::connect(&xb,
SIGNAL(receivedRemoteCommandResponse(RemoteATCommandResponse*)),
&printer,
SLOT(onPacketReceived(RemoteATCommandResponse*))
);
remoteNI.setCommand(ATCommand::ATNI); // Node Indentifier
remoteNI.setDestinationAddress16(0x2);
xb.sendAsync(&remoteNI);
return a.exec();
}


//xbeeresponseprinter.h
#ifndef XBEERESPONSEPRINTER
#define XBEERESPONSEPRINTER
#include <QObject>
#include <QDebug>
#include <remoteatcommandresponse.h>
using namespace QtXBee;
class XBeeResponsePrinter : public QObject {
Q_OBJECT
public:
explicit XBeeResponsePrinter(QObject *parent = 0) : QObject(parent) {}
~XBeeResponsePrinter() {}
public slots:
void onPacketReceived(RemoteATCommandResponse *packet) {
qDebug() << "Received packet";
qDebug() << qPrintable(packet->toString());
}
};
#endif // XBEERESPONSEPRINTER

In my case the result is:

Received packet
Raw packet : 0x7e001697010013a20040cabb3800024e4900576561746865724c
Start delimiter : 0x7e
Frame type : Remote AT Command Response (0x97)
Length : 22 bytes
Frame id : 1
Data : 0x57656174686572 (Weather)
Source Address 64bits : 0x13a20040cabb38
Source Address 16bits : 0x2
AT Command : NI (0x4e49)
Command Status : OK (0x0)
Checksum : 7

Wpan transmit request (16 bits addressing)

This example shows how to send data to a remote XBee, using 16-bits addressing.

In order to be able to send data, you must set the packet's destination address of the remote XBee (MY parameter of the remote node).

We will use the QtXBee::Wpan::TxRequest16 class to send the data, and the QtXBee::Wpan::TxStatusResponse class to check the transmit status.

#include <QCoreApplication>
#include <QDebug>
#include <XBee>
#include <wpan/TxRequest16>
#include <wpan/TxStatusResponse>
using namespace QtXBee;
using namespace QtXBee::Wpan;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
TxStatusResponse *rep = NULL;
// Configure serial
// port here
if(!xb.open()) {
qFatal("Failed to open serial port");
}
req.setDestinationAddress(0x2); // Remote node 16-bit address (remote node's MY parameter)
req.setData("Hello World !"); // Data to send
rep = qobject_cast<TxStatusResponse*>(xb.sendSync(&req));
if(!rep) { // Check the response
qFatal("No response");
}
qDebug() << "Transmit"
qPrintable("succeeded") :
(qPrintable("failed with error " + TxStatusResponse::statusToString(rep->status()))));
delete rep;
return 0;
}

Wpan transmit request (64 bits addressing)

This example shows how to send data to a remote XBee, using 64-bits addressing.

In order to be able to send data, you must set the packet's destination address of the remote XBee (SH+SL parameters of the remote node).

We will use the QtXBee::Wpan::TxRequest64 class to send the data, and the QtXBee::Wpan::TxStatusResponse class to check the transmit status.

#include <QCoreApplication>
#include <QDebug>
#include <XBee>
#include <wpan/TxRequest64>
#include <wpan/TxStatusResponse>
using namespace QtXBee;
using namespace QtXBee::Wpan;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
XBee xb;
TxStatusResponse *rep = NULL;
// Configure serial
// port here
if(!xb.open()) {
qFatal("Failed to open serial port");
}
req.setDestinationAddress(0x13a20040cabb38); // Remote node 64-bit address (remote node's SH+SL parameters)
req.setData("Hello World !"); // Data to send
rep = qobject_cast<TxStatusResponse*>(xb.sendSync(&req));
if(!rep) { // Check the response
qFatal("No response");
}
qDebug() << "Transmit"
qPrintable("succeeded") :
(qPrintable("failed with error " + TxStatusResponse::statusToString(rep->status()))));
delete rep;
return 0;
}