API for communicating with XBee devices.

Dependencies:   CircularBuffer FixedLengthList

Dependents:   XBeeApiTest XBeeApiSimpleATCmdsExample XBeeApiBroadcastExample XBeeApiBroadcastExampleRTOS ... more

Overview

XBeeApi is intended to be a library for providing a high-level API interface to the XBee - for example getChannel() and setChannel(2) methods rather than needing to send( "ATCH" ) and send( "ATCH 2" ) - and then de-code the responses.

See the notebook page here for a description of how the API works & some details on the various classes.

Features:

  • Support for transmission & reception of data packets
  • Support for reading & changing settings
  • Support for "Remote AT" interface to access settings & I/O channels on remote XBees
  • XBeeApi should work if you're using mbed-rtos, though it is not currently threadsafe. Take a look at the XBeeApiBroadcastExampleRTOS example if you're including mbed-rtos.

Example Programs

There are also example programs available:

Transmit

Import programXBeeApiSimpleBroadcastExample

Simple example of how to use XBeeApi - set up the XBee, configure P2P networking then transmit a frame.

Import programXBeeApiBroadcastExample

Example for XBeeAPI; a little more involved than XBeeApiSimpleBroadcastExample with report on failure to set up the XBee and on the transmit status of the message.

Import programXBeeApiBroadcastExampleRTOS

Example of using the XBeeApi library to broadcast a message, based on XBeeApiBroadcastExample. This example shows how to use the library when using mbed-rtos. Before compiling you must open "XbeeApi\Config\XBeeApiCfg.hpp" and change the '#if 0' to '#if 1' on the line above the comment reading "Use RTOS features to make XBeeApi threadsafe"

Settings/Status

Import programXBeeApiSimpleATCmdsExample

Simple example of using XBeeApi to send AT-style commands to the XBee

Import programXBeeApiRemoteATCmdsExample

Example of using the XBeeApi library to send AT commands to remote XBee devices in order to read/write settings

Receive

Import programXBeeApiSimpleReceiveExample

Simple example of using XBeeApi to receive data packets via wireless

Import programXBeeApiReceiveCallbackExample

Example of using the XBeeApi library to receive a message via a callback method

Import programXBeeApiReceiveCallbackExampleRTOS

Example of using the XBeeApi library to receive a message via a callback method. This example shows how to use the library when using mbed-rtos. See the comment at the top of main.cpp

Remote I/O

Import programXBeeApiRemoteIOExample

Example of using the XBeeApi library to read inputs on a remote XBee

If you have 2 mbed connected XBees available then you can use XBeeApiSimpleReceiveExample and XBeeApiSimpleBroadcastExample as a pair.

Note that this is still a work in progress! XBeeApiTodoList tracks some of the functionality still to be added.

Committer:
johnb
Date:
Thu Feb 06 22:26:27 2014 +0000
Revision:
31:c144106e55b5
Parent:
29:c6d037cceb02
Child:
32:af4e495afd62
Add method to set XBee's source address; Macro-ise XBeeApiCmdAt's method generation; Fix bug with timeout on XBeeApiCmdAtBlocking's get/set methods

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 8:1b48b619d7f6 1 /**
johnb 8:1b48b619d7f6 2 @file
johnb 8:1b48b619d7f6 3 @brief Class to abstract AT commands send to the XBee API
johnb 8:1b48b619d7f6 4
johnb 8:1b48b619d7f6 5 AT commands have the payload:
johnb 8:1b48b619d7f6 6
johnb 8:1b48b619d7f6 7 Byte 1 : Frame ID
johnb 8:1b48b619d7f6 8 Byte 2 & 3 : AT command
johnb 8:1b48b619d7f6 9 Byte 4-n : Parameter Value
johnb 8:1b48b619d7f6 10
johnb 8:1b48b619d7f6 11 @author John Bailey
johnb 8:1b48b619d7f6 12
johnb 8:1b48b619d7f6 13 @copyright Copyright 2014 John Bailey
johnb 8:1b48b619d7f6 14
johnb 8:1b48b619d7f6 15 @section LICENSE
johnb 8:1b48b619d7f6 16
johnb 8:1b48b619d7f6 17 Licensed under the Apache License, Version 2.0 (the "License");
johnb 8:1b48b619d7f6 18 you may not use this file except in compliance with the License.
johnb 8:1b48b619d7f6 19 You may obtain a copy of the License at
johnb 8:1b48b619d7f6 20
johnb 8:1b48b619d7f6 21 http://www.apache.org/licenses/LICENSE-2.0
johnb 8:1b48b619d7f6 22
johnb 8:1b48b619d7f6 23 Unless required by applicable law or agreed to in writing, software
johnb 8:1b48b619d7f6 24 distributed under the License is distributed on an "AS IS" BASIS,
johnb 8:1b48b619d7f6 25 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 8:1b48b619d7f6 26 See the License for the specific language governing permissions and
johnb 8:1b48b619d7f6 27 limitations under the License.
johnb 8:1b48b619d7f6 28
johnb 8:1b48b619d7f6 29 */
johnb 8:1b48b619d7f6 30
johnb 8:1b48b619d7f6 31 #if !defined XBEEAPICMDAT_HPP
johnb 8:1b48b619d7f6 32 #define XBEEAPICMDAT_HPP
johnb 8:1b48b619d7f6 33
johnb 8:1b48b619d7f6 34 #include "XBeeApiFrame.hpp"
johnb 8:1b48b619d7f6 35 #include "XBeeDevice.hpp"
johnb 8:1b48b619d7f6 36
johnb 8:1b48b619d7f6 37 #include <stdint.h>
johnb 8:1b48b619d7f6 38
johnb 26:f5df80e990f4 39 #define XBEE_API_CMD_SET_HEADER_LEN 3U
johnb 26:f5df80e990f4 40
johnb 26:f5df80e990f4 41 /** Class to access the configuration interface of the XBee.
johnb 26:f5df80e990f4 42 Requests to the XBee are non-blocking meaning that code
johnb 26:f5df80e990f4 43 which utilises this class must deal with the fact that
johnb 26:f5df80e990f4 44 there will be a delay between requesting the data from
johnb 26:f5df80e990f4 45 the XBee and the data being available via the API. See
johnb 26:f5df80e990f4 46 XBeeApiCmdAtBlocking for a blocking version.
johnb 26:f5df80e990f4 47
johnb 26:f5df80e990f4 48 Parameters from the XBee are cached in the object so
johnb 26:f5df80e990f4 49 subsequent requests do not need have the overhead of
johnb 26:f5df80e990f4 50 communication with the XBee */
johnb 8:1b48b619d7f6 51 class XBeeApiCmdAt : public XBeeApiFrameDecoder
johnb 8:1b48b619d7f6 52 {
johnb 13:302e7c1ea0b3 53 public:
johnb 26:f5df80e990f4 54 /** Type to represent the ID of a PAN (Personal Area Network) */
johnb 26:f5df80e990f4 55 typedef uint16_t panId_t;
johnb 26:f5df80e990f4 56 /** Type to represent a wireless channel number */
johnb 26:f5df80e990f4 57 typedef uint8_t channel_t;
johnb 13:302e7c1ea0b3 58
johnb 8:1b48b619d7f6 59 protected:
johnb 26:f5df80e990f4 60 /** Indicates whether or not m_hwVer contains data retrieved from the XBee */
johnb 31:c144106e55b5 61 bool m_have_hwVer;
johnb 26:f5df80e990f4 62 /** Indicates whether or not m_fwVer contains data retrieved from the XBee */
johnb 31:c144106e55b5 63 bool m_have_fwVer;
johnb 26:f5df80e990f4 64 /** Indicates whether or not m_chan contains data retrieved from the XBee */
johnb 31:c144106e55b5 65 bool m_have_chan;
johnb 26:f5df80e990f4 66 /** Indicates whether or not m_PANId contains data retrieved from the XBee */
johnb 31:c144106e55b5 67 bool m_have_PANId;
johnb 26:f5df80e990f4 68 /** Indicates whether or not m_EDA contains data retrieved from the XBee */
johnb 31:c144106e55b5 69 bool m_have_EDA;
johnb 26:f5df80e990f4 70 /** Indicates whether or not m_CE contains data retrieved from the XBee */
johnb 31:c144106e55b5 71 bool m_have_CE;
johnb 31:c144106e55b5 72 bool m_have_sourceAddress;
johnb 13:302e7c1ea0b3 73
johnb 8:1b48b619d7f6 74 uint16_t m_hwVer;
johnb 8:1b48b619d7f6 75 uint16_t m_fwVer;
johnb 13:302e7c1ea0b3 76 channel_t m_chan;
johnb 13:302e7c1ea0b3 77 channel_t m_chanPend;
johnb 13:302e7c1ea0b3 78 panId_t m_PANId;
johnb 13:302e7c1ea0b3 79 panId_t m_PANIdPend;
johnb 13:302e7c1ea0b3 80 bool m_EDA;
johnb 13:302e7c1ea0b3 81 bool m_EDAPend;
johnb 13:302e7c1ea0b3 82 bool m_CE;
johnb 13:302e7c1ea0b3 83 bool m_CEPend;
johnb 31:c144106e55b5 84 uint16_t m_sourceAddress;
johnb 31:c144106e55b5 85 uint16_t m_sourceAddressPend;
johnb 8:1b48b619d7f6 86
johnb 26:f5df80e990f4 87 /** Template class to create an XBeeApiFrame which can be used to change
johnb 26:f5df80e990f4 88 the value of one of the XBee parameters. This class is used by the
johnb 26:f5df80e990f4 89 setXXX methods in XBeeApiCmdAt */
johnb 25:db6874b7ac4b 90 template< typename T >
johnb 25:db6874b7ac4b 91 class XBeeApiCmdAtSet : public XBeeApiFrame {
johnb 26:f5df80e990f4 92 uint8_t m_buffer[ XBEE_API_CMD_SET_HEADER_LEN + sizeof( T ) ];
johnb 13:302e7c1ea0b3 93 public:
johnb 26:f5df80e990f4 94 /** Constructor
johnb 26:f5df80e990f4 95
johnb 26:f5df80e990f4 96 \param p_data Pointer to a buffer of length 3 bytes containing a
johnb 26:f5df80e990f4 97 single byte frame ID followed by 2 bytes identifying
johnb 26:f5df80e990f4 98 the command, e.g. '0', 'V', 'R' would set up a version
johnb 26:f5df80e990f4 99 request with a frame identifier of 48 (ASCII value of
johnb 26:f5df80e990f4 100 '0').
johnb 26:f5df80e990f4 101 \param p_val New value for the parameter
johnb 26:f5df80e990f4 102 */
johnb 25:db6874b7ac4b 103 XBeeApiCmdAtSet( const uint8_t* const p_data,
johnb 25:db6874b7ac4b 104 const T p_val );
johnb 26:f5df80e990f4 105 /** Destructor */
johnb 26:f5df80e990f4 106 virtual ~XBeeApiCmdAtSet();
johnb 13:302e7c1ea0b3 107 };
johnb 13:302e7c1ea0b3 108
johnb 26:f5df80e990f4 109 /* Implement XBeeApiCmdDecoder interface */
johnb 26:f5df80e990f4 110 virtual bool decodeCallback( const uint8_t* const p_data, size_t p_len );
johnb 26:f5df80e990f4 111
johnb 8:1b48b619d7f6 112 public:
johnb 13:302e7c1ea0b3 113
johnb 29:c6d037cceb02 114 /** Constructor
johnb 29:c6d037cceb02 115
johnb 29:c6d037cceb02 116 \param p_device XBee device with which this object should be associated */
johnb 29:c6d037cceb02 117 XBeeApiCmdAt( XBeeDevice* const p_device = NULL );
johnb 26:f5df80e990f4 118
johnb 26:f5df80e990f4 119 /** Destructor */
johnb 26:f5df80e990f4 120 virtual ~XBeeApiCmdAt( void ) {};
johnb 8:1b48b619d7f6 121
johnb 26:f5df80e990f4 122 /** Request the hardware version identifier from the XBee.
johnb 26:f5df80e990f4 123 As the data is retrieved asynchronously to this call,
johnb 26:f5df80e990f4 124 once the response is received it can be accessed via
johnb 26:f5df80e990f4 125 getHardwareVersion() */
johnb 26:f5df80e990f4 126 bool requestHardwareVersion( void );
johnb 26:f5df80e990f4 127 bool requestFirmwareVersion( void );
johnb 26:f5df80e990f4 128 bool requestChannel( void );
johnb 26:f5df80e990f4 129 bool requestCoordinatorEnabled( void );
johnb 26:f5df80e990f4 130 bool requestEndDeviceAssociationEnabled( void );
johnb 26:f5df80e990f4 131 bool requestPanId( void );
johnb 31:c144106e55b5 132 bool requestSourceAddress( void );
johnb 8:1b48b619d7f6 133
johnb 26:f5df80e990f4 134 /** Read the XBee's hardware version identifier.
johnb 26:f5df80e990f4 135
johnb 26:f5df80e990f4 136 This method does not initiate any communication with the
johnb 26:f5df80e990f4 137 XBee - the identifier must previously have been requested
johnb 26:f5df80e990f4 138 via requestHardwareVersion(). The method is non-blocking. */
johnb 26:f5df80e990f4 139 virtual bool getHardwareVersion( uint16_t* const p_ver );
johnb 26:f5df80e990f4 140
johnb 26:f5df80e990f4 141 /** Read the XBee's firmware version identifier.
johnb 26:f5df80e990f4 142
johnb 26:f5df80e990f4 143 This method does not initiate any communication with the
johnb 26:f5df80e990f4 144 XBee - the identifier must previously have been requested
johnb 26:f5df80e990f4 145 via requestFirmwareVersion(). The method is non-blocking. */
johnb 26:f5df80e990f4 146 virtual bool getFirmwareVersion( uint16_t* const p_ver );
johnb 13:302e7c1ea0b3 147
johnb 8:1b48b619d7f6 148 virtual bool getChannel( uint8_t* const p_chan );
johnb 13:302e7c1ea0b3 149 virtual bool setChannel( uint8_t const p_chan );
johnb 8:1b48b619d7f6 150
johnb 13:302e7c1ea0b3 151 virtual bool getCoordinatorEnabled( bool* constp_en );
johnb 13:302e7c1ea0b3 152 virtual bool setCoordinatorEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 153
johnb 13:302e7c1ea0b3 154 virtual bool getEndDeviceAssociationEnabled( bool* const p_en );
johnb 13:302e7c1ea0b3 155 virtual bool setEndDeviceAssociationEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 156
johnb 13:302e7c1ea0b3 157 virtual bool getPanId( panId_t* const p_id );
johnb 13:302e7c1ea0b3 158 virtual bool setPanId( const panId_t p_id );
johnb 31:c144106e55b5 159
johnb 31:c144106e55b5 160 virtual bool getSourceAddress( uint16_t* const p_addr );
johnb 31:c144106e55b5 161 virtual bool setSourceAddress( const uint16_t p_addr );
johnb 31:c144106e55b5 162
johnb 8:1b48b619d7f6 163 };
johnb 8:1b48b619d7f6 164
johnb 26:f5df80e990f4 165 /** Class to access the configuration interface of the XBee.
johnb 26:f5df80e990f4 166 In contrast to XBeeApiCmdAt, the getXXX methods block
johnb 26:f5df80e990f4 167 until the data is received (or a timeout has occurred)
johnb 26:f5df80e990f4 168 which means that the caller doesn't have to deal with the
johnb 26:f5df80e990f4 169 asynchronous nature of the API provided by XBeeApiCmdAt.
johnb 26:f5df80e990f4 170
johnb 26:f5df80e990f4 171 It's not necessary to use any of the requestXXX methods
johnb 26:f5df80e990f4 172 (as the getXXX methods will take care of this, however
johnb 26:f5df80e990f4 173 calling a requestXXX method will effectively pre-fetch the
johnb 26:f5df80e990f4 174 data meaning that getXXX will not have to block */
johnb 8:1b48b619d7f6 175 class XBeeApiCmdAtBlocking : public XBeeApiCmdAt
johnb 8:1b48b619d7f6 176 {
johnb 8:1b48b619d7f6 177 protected:
johnb 26:f5df80e990f4 178 /** Timeout used for blocking methods in milliseconds */
johnb 8:1b48b619d7f6 179 uint16_t m_timeout;
johnb 26:f5df80e990f4 180
johnb 26:f5df80e990f4 181 /** Wait slice time while blocking. The function will
johnb 26:f5df80e990f4 182 wait_ms(m_slice) until the XBee responds with the
johnb 26:f5df80e990f4 183 data or m_timeout elapses */
johnb 8:1b48b619d7f6 184 uint16_t m_slice;
johnb 8:1b48b619d7f6 185
johnb 8:1b48b619d7f6 186 public:
johnb 26:f5df80e990f4 187 /** Constructor
johnb 26:f5df80e990f4 188
johnb 29:c6d037cceb02 189 \param p_device XBee device with which this object should
johnb 29:c6d037cceb02 190 be associated
johnb 26:f5df80e990f4 191 \param p_timeout Timeout to be used when waiting for
johnb 26:f5df80e990f4 192 data from the XBee, specified in
johnb 26:f5df80e990f4 193 milliseconds
johnb 26:f5df80e990f4 194 \param p_slice While waiting for data, blocking methods
johnb 26:f5df80e990f4 195 will call the OS wait_ms() function, using
johnb 26:f5df80e990f4 196 the value specified by p_slice */
johnb 29:c6d037cceb02 197 XBeeApiCmdAtBlocking( XBeeDevice* const p_device = NULL,
johnb 29:c6d037cceb02 198 const uint16_t p_timeout = 1000,
johnb 29:c6d037cceb02 199 const uint16_t p_slice = 100);
johnb 8:1b48b619d7f6 200
johnb 26:f5df80e990f4 201 /** Destructor */
johnb 8:1b48b619d7f6 202 virtual ~XBeeApiCmdAtBlocking( void ) {};
johnb 26:f5df80e990f4 203
johnb 8:1b48b619d7f6 204 /* Implement XBeeApiCmdAt's virtual methods */
johnb 26:f5df80e990f4 205
johnb 8:1b48b619d7f6 206 virtual bool getHardwareVersion( uint16_t* const p_ver );
johnb 8:1b48b619d7f6 207 virtual bool getFirmwareVersion( uint16_t* const p_ver );
johnb 13:302e7c1ea0b3 208
johnb 8:1b48b619d7f6 209 virtual bool getChannel( uint8_t* const p_chan );
johnb 13:302e7c1ea0b3 210 virtual bool setChannel( uint8_t const p_chan );
johnb 13:302e7c1ea0b3 211
johnb 13:302e7c1ea0b3 212 virtual bool getCoordinatorEnabled( bool* constp_en );
johnb 13:302e7c1ea0b3 213 virtual bool setCoordinatorEnabled( const bool p_en );
johnb 8:1b48b619d7f6 214
johnb 13:302e7c1ea0b3 215 virtual bool getEndDeviceAssociationEnabled( bool* const p_en );
johnb 13:302e7c1ea0b3 216 virtual bool setEndDeviceAssociationEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 217
johnb 13:302e7c1ea0b3 218 virtual bool getPanId( panId_t* const p_id );
johnb 26:f5df80e990f4 219 virtual bool setPanId( const panId_t p_id );
johnb 31:c144106e55b5 220
johnb 31:c144106e55b5 221 virtual bool getSourceAddress( uint16_t* const p_addr );
johnb 31:c144106e55b5 222 virtual bool setSourceAddress( const uint16_t p_addr );
johnb 8:1b48b619d7f6 223 };
johnb 8:1b48b619d7f6 224
johnb 8:1b48b619d7f6 225 #endif