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:
Wed Feb 05 21:31:10 2014 +0000
Revision:
26:f5df80e990f4
Parent:
25:db6874b7ac4b
Child:
29:c6d037cceb02
Doc updates for XBeeApiCmdAt and XBeeApiCmdAtBlocking

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 8:1b48b619d7f6 61 bool m_haveHwVer;
johnb 26:f5df80e990f4 62 /** Indicates whether or not m_fwVer contains data retrieved from the XBee */
johnb 8:1b48b619d7f6 63 bool m_haveFwVer;
johnb 26:f5df80e990f4 64 /** Indicates whether or not m_chan contains data retrieved from the XBee */
johnb 8:1b48b619d7f6 65 bool m_haveChan;
johnb 26:f5df80e990f4 66 /** Indicates whether or not m_PANId contains data retrieved from the XBee */
johnb 13:302e7c1ea0b3 67 bool m_havePANId;
johnb 26:f5df80e990f4 68 /** Indicates whether or not m_EDA contains data retrieved from the XBee */
johnb 13:302e7c1ea0b3 69 bool m_haveEDA;
johnb 26:f5df80e990f4 70 /** Indicates whether or not m_CE contains data retrieved from the XBee */
johnb 13:302e7c1ea0b3 71 bool m_haveCE;
johnb 13:302e7c1ea0b3 72
johnb 8:1b48b619d7f6 73 uint16_t m_hwVer;
johnb 8:1b48b619d7f6 74 uint16_t m_fwVer;
johnb 13:302e7c1ea0b3 75 channel_t m_chan;
johnb 13:302e7c1ea0b3 76 channel_t m_chanPend;
johnb 13:302e7c1ea0b3 77 panId_t m_PANId;
johnb 13:302e7c1ea0b3 78 panId_t m_PANIdPend;
johnb 13:302e7c1ea0b3 79 bool m_EDA;
johnb 13:302e7c1ea0b3 80 bool m_EDAPend;
johnb 13:302e7c1ea0b3 81 bool m_CE;
johnb 13:302e7c1ea0b3 82 bool m_CEPend;
johnb 8:1b48b619d7f6 83
johnb 26:f5df80e990f4 84 /** Template class to create an XBeeApiFrame which can be used to change
johnb 26:f5df80e990f4 85 the value of one of the XBee parameters. This class is used by the
johnb 26:f5df80e990f4 86 setXXX methods in XBeeApiCmdAt */
johnb 25:db6874b7ac4b 87 template< typename T >
johnb 25:db6874b7ac4b 88 class XBeeApiCmdAtSet : public XBeeApiFrame {
johnb 26:f5df80e990f4 89 uint8_t m_buffer[ XBEE_API_CMD_SET_HEADER_LEN + sizeof( T ) ];
johnb 13:302e7c1ea0b3 90 public:
johnb 26:f5df80e990f4 91 /** Constructor
johnb 26:f5df80e990f4 92
johnb 26:f5df80e990f4 93 \param p_data Pointer to a buffer of length 3 bytes containing a
johnb 26:f5df80e990f4 94 single byte frame ID followed by 2 bytes identifying
johnb 26:f5df80e990f4 95 the command, e.g. '0', 'V', 'R' would set up a version
johnb 26:f5df80e990f4 96 request with a frame identifier of 48 (ASCII value of
johnb 26:f5df80e990f4 97 '0').
johnb 26:f5df80e990f4 98 \param p_val New value for the parameter
johnb 26:f5df80e990f4 99 */
johnb 25:db6874b7ac4b 100 XBeeApiCmdAtSet( const uint8_t* const p_data,
johnb 25:db6874b7ac4b 101 const T p_val );
johnb 26:f5df80e990f4 102 /** Destructor */
johnb 26:f5df80e990f4 103 virtual ~XBeeApiCmdAtSet();
johnb 13:302e7c1ea0b3 104 };
johnb 13:302e7c1ea0b3 105
johnb 26:f5df80e990f4 106 /* Implement XBeeApiCmdDecoder interface */
johnb 26:f5df80e990f4 107 virtual bool decodeCallback( const uint8_t* const p_data, size_t p_len );
johnb 26:f5df80e990f4 108
johnb 8:1b48b619d7f6 109 public:
johnb 13:302e7c1ea0b3 110
johnb 26:f5df80e990f4 111 /** Constructor */
johnb 26:f5df80e990f4 112 XBeeApiCmdAt( );
johnb 26:f5df80e990f4 113
johnb 26:f5df80e990f4 114 /** Destructor */
johnb 26:f5df80e990f4 115 virtual ~XBeeApiCmdAt( void ) {};
johnb 8:1b48b619d7f6 116
johnb 26:f5df80e990f4 117 /** Request the hardware version identifier from the XBee.
johnb 26:f5df80e990f4 118 As the data is retrieved asynchronously to this call,
johnb 26:f5df80e990f4 119 once the response is received it can be accessed via
johnb 26:f5df80e990f4 120 getHardwareVersion() */
johnb 26:f5df80e990f4 121 bool requestHardwareVersion( void );
johnb 26:f5df80e990f4 122 bool requestFirmwareVersion( void );
johnb 26:f5df80e990f4 123 bool requestChannel( void );
johnb 26:f5df80e990f4 124 bool requestCoordinatorEnabled( void );
johnb 26:f5df80e990f4 125 bool requestEndDeviceAssociationEnabled( void );
johnb 26:f5df80e990f4 126 bool requestPanId( void );
johnb 8:1b48b619d7f6 127
johnb 26:f5df80e990f4 128 /** Read the XBee's hardware version identifier.
johnb 26:f5df80e990f4 129
johnb 26:f5df80e990f4 130 This method does not initiate any communication with the
johnb 26:f5df80e990f4 131 XBee - the identifier must previously have been requested
johnb 26:f5df80e990f4 132 via requestHardwareVersion(). The method is non-blocking. */
johnb 26:f5df80e990f4 133 virtual bool getHardwareVersion( uint16_t* const p_ver );
johnb 26:f5df80e990f4 134
johnb 26:f5df80e990f4 135 /** Read the XBee's firmware version identifier.
johnb 26:f5df80e990f4 136
johnb 26:f5df80e990f4 137 This method does not initiate any communication with the
johnb 26:f5df80e990f4 138 XBee - the identifier must previously have been requested
johnb 26:f5df80e990f4 139 via requestFirmwareVersion(). The method is non-blocking. */
johnb 26:f5df80e990f4 140 virtual bool getFirmwareVersion( uint16_t* const p_ver );
johnb 13:302e7c1ea0b3 141
johnb 8:1b48b619d7f6 142 virtual bool getChannel( uint8_t* const p_chan );
johnb 13:302e7c1ea0b3 143 virtual bool setChannel( uint8_t const p_chan );
johnb 8:1b48b619d7f6 144
johnb 13:302e7c1ea0b3 145 virtual bool getCoordinatorEnabled( bool* constp_en );
johnb 13:302e7c1ea0b3 146 virtual bool setCoordinatorEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 147
johnb 13:302e7c1ea0b3 148 virtual bool getEndDeviceAssociationEnabled( bool* const p_en );
johnb 13:302e7c1ea0b3 149 virtual bool setEndDeviceAssociationEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 150
johnb 13:302e7c1ea0b3 151 virtual bool getPanId( panId_t* const p_id );
johnb 13:302e7c1ea0b3 152 virtual bool setPanId( const panId_t p_id );
johnb 8:1b48b619d7f6 153 };
johnb 8:1b48b619d7f6 154
johnb 26:f5df80e990f4 155 /** Class to access the configuration interface of the XBee.
johnb 26:f5df80e990f4 156 In contrast to XBeeApiCmdAt, the getXXX methods block
johnb 26:f5df80e990f4 157 until the data is received (or a timeout has occurred)
johnb 26:f5df80e990f4 158 which means that the caller doesn't have to deal with the
johnb 26:f5df80e990f4 159 asynchronous nature of the API provided by XBeeApiCmdAt.
johnb 26:f5df80e990f4 160
johnb 26:f5df80e990f4 161 It's not necessary to use any of the requestXXX methods
johnb 26:f5df80e990f4 162 (as the getXXX methods will take care of this, however
johnb 26:f5df80e990f4 163 calling a requestXXX method will effectively pre-fetch the
johnb 26:f5df80e990f4 164 data meaning that getXXX will not have to block */
johnb 8:1b48b619d7f6 165 class XBeeApiCmdAtBlocking : public XBeeApiCmdAt
johnb 8:1b48b619d7f6 166 {
johnb 8:1b48b619d7f6 167 protected:
johnb 26:f5df80e990f4 168 /** Timeout used for blocking methods in milliseconds */
johnb 8:1b48b619d7f6 169 uint16_t m_timeout;
johnb 26:f5df80e990f4 170
johnb 26:f5df80e990f4 171 /** Wait slice time while blocking. The function will
johnb 26:f5df80e990f4 172 wait_ms(m_slice) until the XBee responds with the
johnb 26:f5df80e990f4 173 data or m_timeout elapses */
johnb 8:1b48b619d7f6 174 uint16_t m_slice;
johnb 8:1b48b619d7f6 175
johnb 8:1b48b619d7f6 176 public:
johnb 26:f5df80e990f4 177 /** Constructor
johnb 26:f5df80e990f4 178
johnb 26:f5df80e990f4 179 \param p_timeout Timeout to be used when waiting for
johnb 26:f5df80e990f4 180 data from the XBee, specified in
johnb 26:f5df80e990f4 181 milliseconds
johnb 26:f5df80e990f4 182 \param p_slice While waiting for data, blocking methods
johnb 26:f5df80e990f4 183 will call the OS wait_ms() function, using
johnb 26:f5df80e990f4 184 the value specified by p_slice */
johnb 8:1b48b619d7f6 185 XBeeApiCmdAtBlocking( const uint16_t p_timeout = 1000, const uint16_t p_slice = 100);
johnb 8:1b48b619d7f6 186
johnb 26:f5df80e990f4 187 /** Destructor */
johnb 8:1b48b619d7f6 188 virtual ~XBeeApiCmdAtBlocking( void ) {};
johnb 26:f5df80e990f4 189
johnb 8:1b48b619d7f6 190 /* Implement XBeeApiCmdAt's virtual methods */
johnb 26:f5df80e990f4 191
johnb 8:1b48b619d7f6 192 virtual bool getHardwareVersion( uint16_t* const p_ver );
johnb 8:1b48b619d7f6 193 virtual bool getFirmwareVersion( uint16_t* const p_ver );
johnb 13:302e7c1ea0b3 194
johnb 8:1b48b619d7f6 195 virtual bool getChannel( uint8_t* const p_chan );
johnb 13:302e7c1ea0b3 196 virtual bool setChannel( uint8_t const p_chan );
johnb 13:302e7c1ea0b3 197
johnb 13:302e7c1ea0b3 198 virtual bool getCoordinatorEnabled( bool* constp_en );
johnb 13:302e7c1ea0b3 199 virtual bool setCoordinatorEnabled( const bool p_en );
johnb 8:1b48b619d7f6 200
johnb 13:302e7c1ea0b3 201 virtual bool getEndDeviceAssociationEnabled( bool* const p_en );
johnb 13:302e7c1ea0b3 202 virtual bool setEndDeviceAssociationEnabled( const bool p_en );
johnb 13:302e7c1ea0b3 203
johnb 13:302e7c1ea0b3 204 virtual bool getPanId( panId_t* const p_id );
johnb 26:f5df80e990f4 205 virtual bool setPanId( const panId_t p_id );
johnb 8:1b48b619d7f6 206 };
johnb 8:1b48b619d7f6 207
johnb 8:1b48b619d7f6 208 #endif