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.
XBeeDevice.hpp@1:1f3ffa394a1e, 2014-01-18 (annotated)
- Committer:
- johnb
- Date:
- Sat Jan 18 19:44:01 2014 +0000
- Revision:
- 1:1f3ffa394a1e
- Parent:
- 0:9f44c388ca00
- Child:
- 2:7196461129e9
Work-in-progress commit : Can get XBee into AT command mode, configure API mode 2, exit command code and successfully perform API command.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:9f44c388ca00 | 1 | /** |
johnb | 0:9f44c388ca00 | 2 | @file |
johnb | 0:9f44c388ca00 | 3 | @brief Class to abstract the XBee serial interface |
johnb | 0:9f44c388ca00 | 4 | |
johnb | 0:9f44c388ca00 | 5 | @author John Bailey |
johnb | 0:9f44c388ca00 | 6 | |
johnb | 0:9f44c388ca00 | 7 | @copyright Copyright 2013 John Bailey |
johnb | 0:9f44c388ca00 | 8 | |
johnb | 0:9f44c388ca00 | 9 | @section LICENSE |
johnb | 0:9f44c388ca00 | 10 | |
johnb | 0:9f44c388ca00 | 11 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:9f44c388ca00 | 12 | you may not use this file except in compliance with the License. |
johnb | 0:9f44c388ca00 | 13 | You may obtain a copy of the License at |
johnb | 0:9f44c388ca00 | 14 | |
johnb | 0:9f44c388ca00 | 15 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:9f44c388ca00 | 16 | |
johnb | 0:9f44c388ca00 | 17 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:9f44c388ca00 | 18 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:9f44c388ca00 | 19 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:9f44c388ca00 | 20 | See the License for the specific language governing permissions and |
johnb | 0:9f44c388ca00 | 21 | limitations under the License. |
johnb | 0:9f44c388ca00 | 22 | |
johnb | 0:9f44c388ca00 | 23 | */ |
johnb | 0:9f44c388ca00 | 24 | |
johnb | 0:9f44c388ca00 | 25 | #if !defined XBEEDEVICE_HPP |
johnb | 0:9f44c388ca00 | 26 | #define XBEEDEVICE_HPP |
johnb | 0:9f44c388ca00 | 27 | |
johnb | 0:9f44c388ca00 | 28 | #include "XBeeApiCfg.hpp" |
johnb | 0:9f44c388ca00 | 29 | |
johnb | 0:9f44c388ca00 | 30 | #include "mbed.h" // For serial interface |
johnb | 0:9f44c388ca00 | 31 | #if defined XBEEAPI_CONFIG_USING_RTOS |
johnb | 0:9f44c388ca00 | 32 | #include "rtos.h" |
johnb | 0:9f44c388ca00 | 33 | #endif |
johnb | 0:9f44c388ca00 | 34 | |
johnb | 0:9f44c388ca00 | 35 | #include "FixedLengthList.hpp" |
johnb | 0:9f44c388ca00 | 36 | #include "CircularBuffer.h" |
johnb | 0:9f44c388ca00 | 37 | |
johnb | 0:9f44c388ca00 | 38 | #include "XBeeApiCmd.hpp" |
johnb | 0:9f44c388ca00 | 39 | |
johnb | 0:9f44c388ca00 | 40 | typedef enum |
johnb | 0:9f44c388ca00 | 41 | { |
johnb | 0:9f44c388ca00 | 42 | XBEE_SB_XON = 0x11, |
johnb | 0:9f44c388ca00 | 43 | XBEE_SB_XOFF = 0x13, |
johnb | 0:9f44c388ca00 | 44 | XBEE_SB_FRAME_DELIMITER = 0x7E, |
johnb | 0:9f44c388ca00 | 45 | XBEE_SB_ESCAPE = 0x7D |
johnb | 0:9f44c388ca00 | 46 | } XBeeSerialSpecialBytes_e; |
johnb | 0:9f44c388ca00 | 47 | |
johnb | 0:9f44c388ca00 | 48 | class XBeeDevice |
johnb | 0:9f44c388ca00 | 49 | { |
johnb | 0:9f44c388ca00 | 50 | private: |
johnb | 0:9f44c388ca00 | 51 | |
johnb | 0:9f44c388ca00 | 52 | #if defined XBEEAPI_CONFIG_USING_RTOS |
johnb | 0:9f44c388ca00 | 53 | /** Mutex for accessing the serial interface */ |
johnb | 0:9f44c388ca00 | 54 | rtos::Mutex m_ifMutex; |
johnb | 0:9f44c388ca00 | 55 | #endif |
johnb | 0:9f44c388ca00 | 56 | |
johnb | 0:9f44c388ca00 | 57 | /** Serial interface for the XBee comms */ |
johnb | 0:9f44c388ca00 | 58 | Serial m_if; |
johnb | 0:9f44c388ca00 | 59 | |
johnb | 0:9f44c388ca00 | 60 | /** Call-back function from MBED triggered when data is |
johnb | 0:9f44c388ca00 | 61 | received on the XBee's serial interface */ |
johnb | 0:9f44c388ca00 | 62 | void if_rx( void ); |
johnb | 0:9f44c388ca00 | 63 | |
johnb | 0:9f44c388ca00 | 64 | /** Write a byte to the XBee serial interface, taking care of any |
johnb | 0:9f44c388ca00 | 65 | escaping requirements (see m_escape) |
johnb | 0:9f44c388ca00 | 66 | |
johnb | 0:9f44c388ca00 | 67 | @param p_byte Byte to be written |
johnb | 0:9f44c388ca00 | 68 | */ |
johnb | 0:9f44c388ca00 | 69 | void xbeeWrite( uint8_t p_byte, bool p_doEscape = true ); |
johnb | 0:9f44c388ca00 | 70 | |
johnb | 0:9f44c388ca00 | 71 | bool m_escape; |
johnb | 0:9f44c388ca00 | 72 | |
johnb | 0:9f44c388ca00 | 73 | CircularBuffer<XBEEAPI_CONFIG_RX_BUFFER_SIZE> m_rxBuff; |
johnb | 0:9f44c388ca00 | 74 | |
johnb | 0:9f44c388ca00 | 75 | public: |
johnb | 1:1f3ffa394a1e | 76 | typedef enum { |
johnb | 1:1f3ffa394a1e | 77 | XBEEDEVICE_OK = 0, |
johnb | 1:1f3ffa394a1e | 78 | XBEEDEVICE_TIMEOUT = 1, |
johnb | 1:1f3ffa394a1e | 79 | XBEEDEVICE_UNEXPECTED_LENGTH = 2, |
johnb | 1:1f3ffa394a1e | 80 | XBEEDEVICE_UNEXPECTED_DATA = 3 |
johnb | 1:1f3ffa394a1e | 81 | } XBeeDeviceReturn_t; |
johnb | 1:1f3ffa394a1e | 82 | |
johnb | 0:9f44c388ca00 | 83 | /** |
johnb | 0:9f44c388ca00 | 84 | @param p_tx Serial interface TX pin |
johnb | 0:9f44c388ca00 | 85 | @param p_rx Serial interface RX pin |
johnb | 0:9f44c388ca00 | 86 | @param p_rts Pin to use for RTS (flow control). Will only be used if supported. Can specify NC to disable. |
johnb | 0:9f44c388ca00 | 87 | @param p_rts Pin to use for CTS (flow control). Will only be used if supported. Can specify NC to disable. |
johnb | 0:9f44c388ca00 | 88 | */ |
johnb | 0:9f44c388ca00 | 89 | XBeeDevice( PinName p_tx, PinName p_rx, PinName p_rts, PinName p_cts ); |
johnb | 0:9f44c388ca00 | 90 | |
johnb | 0:9f44c388ca00 | 91 | void SendCmd( const XBeeApiCmd* const p_cmd ); |
johnb | 0:9f44c388ca00 | 92 | |
johnb | 1:1f3ffa394a1e | 93 | XBeeDeviceReturn_t setUpApi( void ); |
johnb | 1:1f3ffa394a1e | 94 | |
johnb | 1:1f3ffa394a1e | 95 | XBeeDeviceReturn_t SendCmd( const char* const p_dat, size_t p_len, int p_wait_ms = 1000 ); |
johnb | 0:9f44c388ca00 | 96 | |
johnb | 0:9f44c388ca00 | 97 | #if defined XBEEAPI_CONFIG_ENABLE_DEVELOPER |
johnb | 0:9f44c388ca00 | 98 | void dumpRxBuffer( Stream* p_buf, const bool p_hexView ); |
johnb | 0:9f44c388ca00 | 99 | #endif |
johnb | 0:9f44c388ca00 | 100 | |
johnb | 0:9f44c388ca00 | 101 | }; |
johnb | 0:9f44c388ca00 | 102 | |
johnb | 0:9f44c388ca00 | 103 | #endif |