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

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Sun Jul 06 21:47:58 2014 +0000
Revision:
0:2c02b2e821f2
Child:
1:d8492e31f655
Example of using the XBeeApi library to receive a message via a callback method

Who changed what in which revision?

UserRevisionLine numberNew 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 0:2c02b2e821f2 37 /* ID for the Personal Area Network we're going to join */
johnb 0:2c02b2e821f2 38 const XBeeApiCmdAt::panId_t myPANId = 1000;
johnb 0:2c02b2e821f2 39
johnb 0:2c02b2e821f2 40 /* Network channel to use */
johnb 0:2c02b2e821f2 41 const XBeeApiCmdAt::channel_t myChannelId = 14;
johnb 0:2c02b2e821f2 42
johnb 0:2c02b2e821f2 43 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:2c02b2e821f2 44
johnb 0:2c02b2e821f2 45 class FrameDumper : public XBeeApiRxFrameDecoder
johnb 0:2c02b2e821f2 46 {
johnb 0:2c02b2e821f2 47 public:
johnb 0:2c02b2e821f2 48 FrameDumper( XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device )
johnb 0:2c02b2e821f2 49 {
johnb 0:2c02b2e821f2 50 }
johnb 0:2c02b2e821f2 51
johnb 0:2c02b2e821f2 52 virtual void frameRxCallback( XBeeApiRxFrame* const p_frame )
johnb 0:2c02b2e821f2 53 {
johnb 0:2c02b2e821f2 54 const uint8_t* outData;
johnb 0:2c02b2e821f2 55 uint16_t outLoop;
johnb 0:2c02b2e821f2 56 pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() );
johnb 0:2c02b2e821f2 57 p_frame->getDataPtr( 0, &outData, &outLoop );
johnb 0:2c02b2e821f2 58 pc.printf("Data [%d]: ",outLoop);
johnb 0:2c02b2e821f2 59 for( ;
johnb 0:2c02b2e821f2 60 outLoop > 0;
johnb 0:2c02b2e821f2 61 outLoop--,outData++ )
johnb 0:2c02b2e821f2 62 {
johnb 0:2c02b2e821f2 63 pc.printf("0x%02X ",*outData);
johnb 0:2c02b2e821f2 64 }
johnb 0:2c02b2e821f2 65 pc.printf("\r\n");
johnb 0:2c02b2e821f2 66 }
johnb 0:2c02b2e821f2 67 };
johnb 0:2c02b2e821f2 68
johnb 0:2c02b2e821f2 69 int main() {
johnb 0:2c02b2e821f2 70 /* This example will use the blocking API for simplicity */
johnb 0:2c02b2e821f2 71 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:2c02b2e821f2 72
johnb 0:2c02b2e821f2 73 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:2c02b2e821f2 74
johnb 0:2c02b2e821f2 75 /* This is the object that will be receiving the RX call-backs */
johnb 0:2c02b2e821f2 76 FrameDumper decoder( &xbeeDevice );
johnb 0:2c02b2e821f2 77
johnb 0:2c02b2e821f2 78 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:2c02b2e821f2 79 This step may not be needed in the case that the XBee has already been configured
johnb 0:2c02b2e821f2 80 to use Mode 2 and the setting has been stored in NV */
johnb 0:2c02b2e821f2 81 status = xbeeDevice.setUpApi();
johnb 0:2c02b2e821f2 82
johnb 0:2c02b2e821f2 83 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:2c02b2e821f2 84 {
johnb 0:2c02b2e821f2 85 /* Set up a peer-to-peer network using the specified PAN and channel */
johnb 0:2c02b2e821f2 86 if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
johnb 0:2c02b2e821f2 87 {
johnb 0:2c02b2e821f2 88 while( 1 )
johnb 0:2c02b2e821f2 89 {
johnb 0:2c02b2e821f2 90 /* Wait while frames are received */
johnb 0:2c02b2e821f2 91 }
johnb 0:2c02b2e821f2 92 }
johnb 0:2c02b2e821f2 93 else
johnb 0:2c02b2e821f2 94 {
johnb 0:2c02b2e821f2 95 pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
johnb 0:2c02b2e821f2 96 }
johnb 0:2c02b2e821f2 97 }
johnb 0:2c02b2e821f2 98 else
johnb 0:2c02b2e821f2 99 {
johnb 0:2c02b2e821f2 100 pc.printf("setUpApi failed with status %d\r\n",status);
johnb 0:2c02b2e821f2 101 }
johnb 0:2c02b2e821f2 102 }