John Bailey
/
XBeeApiReceiveCallbackExample
Example of using the XBeeApi library to receive a message via a callback method
main.cpp@1:d8492e31f655, 2014-07-08 (annotated)
- Committer:
- johnb
- Date:
- Tue Jul 08 20:24:04 2014 +0000
- Revision:
- 1:d8492e31f655
- Parent:
- 0:2c02b2e821f2
Set the XBee's 16-bit address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:2c02b2e821f2 | 1 | /** |
johnb | 0:2c02b2e821f2 | 2 | @file |
johnb | 0:2c02b2e821f2 | 3 | @brief Example of using the XBeeApi library to receive a message |
johnb | 0:2c02b2e821f2 | 4 | via a callback method |
johnb | 0:2c02b2e821f2 | 5 | This example has a minimum of error checking in order |
johnb | 0:2c02b2e821f2 | 6 | to keep the code compact |
johnb | 0:2c02b2e821f2 | 7 | |
johnb | 0:2c02b2e821f2 | 8 | @author John Bailey |
johnb | 0:2c02b2e821f2 | 9 | |
johnb | 0:2c02b2e821f2 | 10 | @copyright Copyright 2014 John Bailey |
johnb | 0:2c02b2e821f2 | 11 | |
johnb | 0:2c02b2e821f2 | 12 | @section LICENSE |
johnb | 0:2c02b2e821f2 | 13 | |
johnb | 0:2c02b2e821f2 | 14 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:2c02b2e821f2 | 15 | you may not use this file except in compliance with the License. |
johnb | 0:2c02b2e821f2 | 16 | You may obtain a copy of the License at |
johnb | 0:2c02b2e821f2 | 17 | |
johnb | 0:2c02b2e821f2 | 18 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:2c02b2e821f2 | 19 | |
johnb | 0:2c02b2e821f2 | 20 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:2c02b2e821f2 | 21 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:2c02b2e821f2 | 22 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:2c02b2e821f2 | 23 | See the License for the specific language governing permissions and |
johnb | 0:2c02b2e821f2 | 24 | limitations under the License. |
johnb | 0:2c02b2e821f2 | 25 | |
johnb | 0:2c02b2e821f2 | 26 | */ |
johnb | 0:2c02b2e821f2 | 27 | |
johnb | 0:2c02b2e821f2 | 28 | #include "mbed.h" |
johnb | 0:2c02b2e821f2 | 29 | #include "xbeeapi.hpp" |
johnb | 0:2c02b2e821f2 | 30 | |
johnb | 0:2c02b2e821f2 | 31 | Serial pc(USBTX, USBRX); // tx, rx |
johnb | 0:2c02b2e821f2 | 32 | |
johnb | 0:2c02b2e821f2 | 33 | /* TODO: You may need to change these based on the device/connections that you're using */ |
johnb | 0:2c02b2e821f2 | 34 | #define XBEE_TX_PIN PTA2 |
johnb | 0:2c02b2e821f2 | 35 | #define XBEE_RX_PIN PTA1 |
johnb | 0:2c02b2e821f2 | 36 | |
johnb | 1:d8492e31f655 | 37 | /* Network address for our XBee */ |
johnb | 1:d8492e31f655 | 38 | const uint16_t myNetworkAddress = 0x1234; |
johnb | 1:d8492e31f655 | 39 | |
johnb | 0:2c02b2e821f2 | 40 | /* ID for the Personal Area Network we're going to join */ |
johnb | 0:2c02b2e821f2 | 41 | const XBeeApiCmdAt::panId_t myPANId = 1000; |
johnb | 0:2c02b2e821f2 | 42 | |
johnb | 0:2c02b2e821f2 | 43 | /* Network channel to use */ |
johnb | 0:2c02b2e821f2 | 44 | const XBeeApiCmdAt::channel_t myChannelId = 14; |
johnb | 0:2c02b2e821f2 | 45 | |
johnb | 0:2c02b2e821f2 | 46 | XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC ); |
johnb | 0:2c02b2e821f2 | 47 | |
johnb | 0:2c02b2e821f2 | 48 | class FrameDumper : public XBeeApiRxFrameDecoder |
johnb | 0:2c02b2e821f2 | 49 | { |
johnb | 0:2c02b2e821f2 | 50 | public: |
johnb | 0:2c02b2e821f2 | 51 | FrameDumper( XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device ) |
johnb | 0:2c02b2e821f2 | 52 | { |
johnb | 0:2c02b2e821f2 | 53 | } |
johnb | 0:2c02b2e821f2 | 54 | |
johnb | 0:2c02b2e821f2 | 55 | virtual void frameRxCallback( XBeeApiRxFrame* const p_frame ) |
johnb | 0:2c02b2e821f2 | 56 | { |
johnb | 0:2c02b2e821f2 | 57 | const uint8_t* outData; |
johnb | 0:2c02b2e821f2 | 58 | uint16_t outLoop; |
johnb | 0:2c02b2e821f2 | 59 | pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() ); |
johnb | 0:2c02b2e821f2 | 60 | p_frame->getDataPtr( 0, &outData, &outLoop ); |
johnb | 0:2c02b2e821f2 | 61 | pc.printf("Data [%d]: ",outLoop); |
johnb | 0:2c02b2e821f2 | 62 | for( ; |
johnb | 0:2c02b2e821f2 | 63 | outLoop > 0; |
johnb | 0:2c02b2e821f2 | 64 | outLoop--,outData++ ) |
johnb | 0:2c02b2e821f2 | 65 | { |
johnb | 0:2c02b2e821f2 | 66 | pc.printf("0x%02X ",*outData); |
johnb | 0:2c02b2e821f2 | 67 | } |
johnb | 0:2c02b2e821f2 | 68 | pc.printf("\r\n"); |
johnb | 0:2c02b2e821f2 | 69 | } |
johnb | 0:2c02b2e821f2 | 70 | }; |
johnb | 0:2c02b2e821f2 | 71 | |
johnb | 0:2c02b2e821f2 | 72 | int main() { |
johnb | 0:2c02b2e821f2 | 73 | /* This example will use the blocking API for simplicity */ |
johnb | 0:2c02b2e821f2 | 74 | XBeeApiCmdAtBlocking atIf( &xbeeDevice ); |
johnb | 0:2c02b2e821f2 | 75 | |
johnb | 0:2c02b2e821f2 | 76 | XBeeDevice::XBeeDeviceReturn_t status; |
johnb | 0:2c02b2e821f2 | 77 | |
johnb | 0:2c02b2e821f2 | 78 | /* This is the object that will be receiving the RX call-backs */ |
johnb | 0:2c02b2e821f2 | 79 | FrameDumper decoder( &xbeeDevice ); |
johnb | 0:2c02b2e821f2 | 80 | |
johnb | 0:2c02b2e821f2 | 81 | /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions. |
johnb | 0:2c02b2e821f2 | 82 | This step may not be needed in the case that the XBee has already been configured |
johnb | 0:2c02b2e821f2 | 83 | to use Mode 2 and the setting has been stored in NV */ |
johnb | 0:2c02b2e821f2 | 84 | status = xbeeDevice.setUpApi(); |
johnb | 0:2c02b2e821f2 | 85 | |
johnb | 0:2c02b2e821f2 | 86 | if( status == XBeeDevice::XBEEDEVICE_OK ) |
johnb | 0:2c02b2e821f2 | 87 | { |
johnb | 1:d8492e31f655 | 88 | /* Set the 16-bit source address of this XBee */ |
johnb | 1:d8492e31f655 | 89 | atIf.setSourceAddress( myNetworkAddress ); |
johnb | 1:d8492e31f655 | 90 | |
johnb | 0:2c02b2e821f2 | 91 | /* Set up a peer-to-peer network using the specified PAN and channel */ |
johnb | 0:2c02b2e821f2 | 92 | if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) ) |
johnb | 0:2c02b2e821f2 | 93 | { |
johnb | 0:2c02b2e821f2 | 94 | while( 1 ) |
johnb | 0:2c02b2e821f2 | 95 | { |
johnb | 0:2c02b2e821f2 | 96 | /* Wait while frames are received */ |
johnb | 0:2c02b2e821f2 | 97 | } |
johnb | 0:2c02b2e821f2 | 98 | } |
johnb | 0:2c02b2e821f2 | 99 | else |
johnb | 0:2c02b2e821f2 | 100 | { |
johnb | 0:2c02b2e821f2 | 101 | pc.printf("xbeeSetNetworkTypeP2P failed\r\n"); |
johnb | 0:2c02b2e821f2 | 102 | } |
johnb | 0:2c02b2e821f2 | 103 | } |
johnb | 0:2c02b2e821f2 | 104 | else |
johnb | 0:2c02b2e821f2 | 105 | { |
johnb | 0:2c02b2e821f2 | 106 | pc.printf("setUpApi failed with status %d\r\n",status); |
johnb | 0:2c02b2e821f2 | 107 | } |
johnb | 0:2c02b2e821f2 | 108 | } |