upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Member Functions | Protected Attributes

API for the XBee modules. More...

Detailed Description

This is a generic UART driver for use with Digi XBee modules. It was tested with the XBee S1 802.15.4 module and the XBee S6B WiFi module.

It provides basic UART support for sending and receiving data to and from the device. It is controlled by an AT or API command set.

It is connected at 9600 baud by default.

These devices are typically configured using Digi's X-CTU windows software, however it is possible of course to configure them manually using AT commands. See the examples.

xbee.jpg


XBee Sensor image provided by SparkFun* under CC BY 2.0.

// simple helper function to send a command and wait for a response
void
sendCommand(upm::XBee& sensor, string cmd)
{
// commands need to be terminated with a carriage return
cmd += "\r";
sensor.writeDataStr(cmd);
string resp;
while (sensor.dataAvailable(2000)) {
resp += sensor.readDataStr(1024);
}
if (resp.empty()) {
cerr << "Timed out waiting for response" << endl;
return;
}
resp = sensor.stringCR2LF(resp);
cout << "Returned (" << resp.size() << " bytes): " << endl;
cout << resp << endl;
}
int
main(int argc, char** argv)
{
// Instantiate a XBee Module on UART 0
upm::XBee sensor(0);
// Set the baud rate, 9600 baud is the default.
if (sensor.setBaudRate(9600)) {
cerr << "Failed to set tty baud rate" << endl;
return 1;
}
printUsage(argv[0]);
if (argc > 1) {
// enable command mode
sensor.commandMode();
cout << "Sending command line argument (" << argv[1] << ")..." << endl;
sendCommand(sensor, argv[1]);
} else {
// enable command mode
sensor.commandMode();
// query the verbose firmware revision
cout << "Querying verbose firmware revision (ATVL)..." << endl;
sendCommand(sensor, "ATVL");
// query the number
cout << "Querying Serial Number High (ATSH)..." << endl;
sendCommand(sensor, "ATSH");
cout << "Querying Serial Number Low (ATSL)..." << endl;
sendCommand(sensor, "ATSL");
cout << "Querying address, if set (ATMY)..." << endl;
sendCommand(sensor, "ATMY");
// For the XBee WiFi S6B
// A comprehensive list of commands and command modes is
// available from the datasheet at:
// ftp1.digi.com/support/documentation/90002180_L.pdf
// For the XBee S1
// A comprehensive list of commands and command modes is
// available from the datasheet at:
// http://www.sparkfun.com/datasheets/Wireless/Zigbee/XBee-Datasheet.pdf
// For the XBee WiFi module:
// An example using AT commands to connect to an AP, with a
// private Key using WPA2:
// Connect to AP with SSID 'mySSID':
// ATIDmySSID
// Provide the private key 'secret':
// ATPKsecret
// Use WPA2 encryption
// ATEE2
}
return 0;
}

Public Member Functions

 XBee (int uart=XBEE_DEFAULT_UART)
 
 ~XBee ()
 
bool dataAvailable (unsigned int millis)
 
int readData (char *buffer, unsigned int len)
 
std::string readDataStr (int len)
 
int writeData (char *buffer, unsigned len)
 
int writeDataStr (std::string data)
 
mraa::Result setBaudRate (int baud=9600)
 
bool commandMode (std::string cmdChars="+++", int guardTimeMS=1000)
 
std::string stringCR2LF (std::string str)
 

Protected Attributes

mraa::Uart m_uart
 

Constructor & Destructor Documentation

XBee ( int  uart = XBEE_DEFAULT_UART)

XBee object constructor

Parameters
uartDefault UART to use (0 or 1). Default is 0.
~XBee ( )

XBee object destructor

Member Function Documentation

bool dataAvailable ( unsigned int  millis)

Checks to see if there is data available for reading

Parameters
millisNumber of milliseconds to wait; 0 means no waiting
Returns
true if there is data available for reading

Here is the caller graph for this function:

int readData ( char *  buffer,
unsigned int  len 
)

Reads any available data into a user-supplied buffer. Note: the call blocks until data is available for reading. Use dataAvailable() to determine whether there is data available beforehand, to avoid blocking.

Parameters
bufferBuffer to hold the data read
lenLength of the buffer
Returns
Number of bytes read
std::string readDataStr ( int  len)

Reads any available data and returns it in a std::string. Note: the call blocks until data is available for reading. Use dataAvailable() to determine whether there is data available beforehand, to avoid blocking.

Parameters
lenMaximum length of the data to be returned
Returns
string containing the data read

Here is the caller graph for this function:

int writeData ( char *  buffer,
unsigned  len 
)

Writes the data in the buffer to the device. If you are writing an AT command, be sure to terminate it with a carriage return ()

Parameters
bufferBuffer to hold the data to write
lenLength of the buffer
Returns
Number of bytes written
int writeDataStr ( std::string  data)

Writes the std:string data to the device. If you are writing an AT command, be sure to terminate it with a carriage return ()

Parameters
dataBuffer to write to the device
Returns
Number of bytes written

Here is the caller graph for this function:

mraa::Result setBaudRate ( int  baud = 9600)

Sets the baud rate for the device. The default is 9600.

Parameters
baudDesired baud rate
Returns
true if successful
bool commandMode ( std::string  cmdChars = "+++",
int  guardTimeMS = 1000 
)

Attempts to enter AT Command Mode. When Idle, data sent to the device (assuming it is not in API mode) is silently transmitted to the configured destination. Running this command attempts to place the device into command mode, allowing you to send AT commands. Note, after a configurable period of inactivity, the device will exit command mode automatically (default 10 seconds).

Both the cmdChars (+++) and the Guard Time can be configured on the device to different values using AT configuration commands.

Parameters
cmdCharsThe command mode characters, default "+++"
guardTimeMSThe number of milliseconds to wait before and after sending the command characters. Default is 1000 (1 second).
Returns
true if successful (received an "OK"), false otherwise

Here is the call graph for this function:

string stringCR2LF ( std::string  str)

This is a convenience method that converts each CR () in a string to a LF (
) and returns it. This is useful for outputting the response to an AT command for instance, which is typically CR terminated.

Parameters
strThe string to convert
Returns
The converted string

The documentation for this class was generated from the following files: