Simple example of using XBeeApi to receive data packets via wireless

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Wed Apr 02 21:35:26 2014 +0000
Revision:
0:1c4d3c2753d4
Child:
1:36ac23927246
XBeeApi receive data example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:1c4d3c2753d4 1 /**
johnb 0:1c4d3c2753d4 2 @file
johnb 0:1c4d3c2753d4 3 @brief Example of using the XBeeApi library to broadcast a message
johnb 0:1c4d3c2753d4 4 This example has a minimum of error checking in order
johnb 0:1c4d3c2753d4 5 to keep the code compact
johnb 0:1c4d3c2753d4 6
johnb 0:1c4d3c2753d4 7 @author John Bailey
johnb 0:1c4d3c2753d4 8
johnb 0:1c4d3c2753d4 9 @copyright Copyright 2014 John Bailey
johnb 0:1c4d3c2753d4 10
johnb 0:1c4d3c2753d4 11 @section LICENSE
johnb 0:1c4d3c2753d4 12
johnb 0:1c4d3c2753d4 13 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:1c4d3c2753d4 14 you may not use this file except in compliance with the License.
johnb 0:1c4d3c2753d4 15 You may obtain a copy of the License at
johnb 0:1c4d3c2753d4 16
johnb 0:1c4d3c2753d4 17 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:1c4d3c2753d4 18
johnb 0:1c4d3c2753d4 19 Unless required by applicable law or agreed to in writing, software
johnb 0:1c4d3c2753d4 20 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:1c4d3c2753d4 21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:1c4d3c2753d4 22 See the License for the specific language governing permissions and
johnb 0:1c4d3c2753d4 23 limitations under the License.
johnb 0:1c4d3c2753d4 24
johnb 0:1c4d3c2753d4 25 */
johnb 0:1c4d3c2753d4 26
johnb 0:1c4d3c2753d4 27 #include "mbed.h"
johnb 0:1c4d3c2753d4 28 #include "xbeeapi.hpp"
johnb 0:1c4d3c2753d4 29
johnb 0:1c4d3c2753d4 30 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:1c4d3c2753d4 31
johnb 0:1c4d3c2753d4 32 const uint8_t tx_data[] = { 'H', 'E', 'L', 'L', 'O' };
johnb 0:1c4d3c2753d4 33
johnb 0:1c4d3c2753d4 34 /* TODO: You may need to change these based on the device/connections that you're using */
johnb 0:1c4d3c2753d4 35 #define XBEE_TX_PIN PTA2
johnb 0:1c4d3c2753d4 36 #define XBEE_RX_PIN PTA1
johnb 0:1c4d3c2753d4 37
johnb 0:1c4d3c2753d4 38 /* ID for the Personal Area Network we're going to join */
johnb 0:1c4d3c2753d4 39 const XBeeApiCmdAt::panId_t myPANId = 1000;
johnb 0:1c4d3c2753d4 40
johnb 0:1c4d3c2753d4 41 /* Network channel to use */
johnb 0:1c4d3c2753d4 42 const XBeeApiCmdAt::channel_t myChannelId = 14;
johnb 0:1c4d3c2753d4 43
johnb 0:1c4d3c2753d4 44 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:1c4d3c2753d4 45
johnb 0:1c4d3c2753d4 46 /** Circular buffer to receive the data frames from the XBee. Any frames
johnb 0:1c4d3c2753d4 47 that won't fit in the buffer when they are received will be lost */
johnb 0:1c4d3c2753d4 48 XBeeApiRxFrameCircularBuffer rxBuffer( 10, &xbeeDevice );
johnb 0:1c4d3c2753d4 49
johnb 0:1c4d3c2753d4 50 void dumpFrame( const XBeeApiRxFrame* const p_frame )
johnb 0:1c4d3c2753d4 51 {
johnb 0:1c4d3c2753d4 52 const uint8_t* outData;
johnb 0:1c4d3c2753d4 53 uint16_t outLoop;
johnb 0:1c4d3c2753d4 54 pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() );
johnb 0:1c4d3c2753d4 55 p_frame->getDataPtr( 0, &outData, &outLoop );
johnb 0:1c4d3c2753d4 56 pc.printf("Data [%d]: ",outLoop);
johnb 0:1c4d3c2753d4 57 for( ;
johnb 0:1c4d3c2753d4 58 outLoop > 0;
johnb 0:1c4d3c2753d4 59 outLoop--,outData++ )
johnb 0:1c4d3c2753d4 60 {
johnb 0:1c4d3c2753d4 61 pc.printf("0x%02X ",*outData);
johnb 0:1c4d3c2753d4 62 }
johnb 0:1c4d3c2753d4 63 pc.printf("\r\n");
johnb 0:1c4d3c2753d4 64 }
johnb 0:1c4d3c2753d4 65
johnb 0:1c4d3c2753d4 66 int main() {
johnb 0:1c4d3c2753d4 67 /* This example will use the blocking API for simplicity */
johnb 0:1c4d3c2753d4 68 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:1c4d3c2753d4 69
johnb 0:1c4d3c2753d4 70 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:1c4d3c2753d4 71
johnb 0:1c4d3c2753d4 72 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:1c4d3c2753d4 73 This step may not be needed in the case that the XBee has already been configured
johnb 0:1c4d3c2753d4 74 to use Mode 2 and the setting has been stored in NV */
johnb 0:1c4d3c2753d4 75 status = xbeeDevice.setUpApi();
johnb 0:1c4d3c2753d4 76
johnb 0:1c4d3c2753d4 77 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:1c4d3c2753d4 78 {
johnb 0:1c4d3c2753d4 79 /* Set up a peer-to-peer network using the specified PAN and channel */
johnb 0:1c4d3c2753d4 80 if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
johnb 0:1c4d3c2753d4 81 {
johnb 0:1c4d3c2753d4 82 const XBeeApiRxFrame* frameP;
johnb 0:1c4d3c2753d4 83 while( 1 )
johnb 0:1c4d3c2753d4 84 {
johnb 0:1c4d3c2753d4 85 /* Wait for a while, report how many frames are in the buffer then clear
johnb 0:1c4d3c2753d4 86 clear the buffer out for the next loop around */
johnb 0:1c4d3c2753d4 87 wait(10);
johnb 0:1c4d3c2753d4 88
johnb 0:1c4d3c2753d4 89 pc.printf("Received frames: %d\r\n",rxBuffer.getFrameCount() );
johnb 0:1c4d3c2753d4 90
johnb 0:1c4d3c2753d4 91 /* Work through all of the frames in the buffer */
johnb 0:1c4d3c2753d4 92 while( NULL != ( frameP = rxBuffer.getTailPtr()))
johnb 0:1c4d3c2753d4 93 {
johnb 0:1c4d3c2753d4 94 /* Dump out some information from the frame */
johnb 0:1c4d3c2753d4 95 dumpFrame( frameP );
johnb 0:1c4d3c2753d4 96 /* Remove the frame from the buffer */
johnb 0:1c4d3c2753d4 97 rxBuffer.pop();
johnb 0:1c4d3c2753d4 98 }
johnb 0:1c4d3c2753d4 99 }
johnb 0:1c4d3c2753d4 100 }
johnb 0:1c4d3c2753d4 101 else
johnb 0:1c4d3c2753d4 102 {
johnb 0:1c4d3c2753d4 103 pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
johnb 0:1c4d3c2753d4 104 }
johnb 0:1c4d3c2753d4 105 }
johnb 0:1c4d3c2753d4 106 else
johnb 0:1c4d3c2753d4 107 {
johnb 0:1c4d3c2753d4 108 pc.printf("setUpApi failed with status %d\r\n",status);
johnb 0:1c4d3c2753d4 109 }
johnb 0:1c4d3c2753d4 110 }