Simple example of using XBeeApi to receive data packets via wireless

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Tue Jul 08 20:26:40 2014 +0000
Revision:
2:34b3b5b1f01d
Parent:
1:36ac23927246
Set the XBee's 16-bit address

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 /* TODO: You may need to change these based on the device/connections that you're using */
johnb 0:1c4d3c2753d4 33 #define XBEE_TX_PIN PTA2
johnb 0:1c4d3c2753d4 34 #define XBEE_RX_PIN PTA1
johnb 0:1c4d3c2753d4 35
johnb 2:34b3b5b1f01d 36 /* Network address for our XBee */
johnb 2:34b3b5b1f01d 37 const uint16_t myNetworkAddress = 0x1234;
johnb 2:34b3b5b1f01d 38
johnb 0:1c4d3c2753d4 39 /* ID for the Personal Area Network we're going to join */
johnb 0:1c4d3c2753d4 40 const XBeeApiCmdAt::panId_t myPANId = 1000;
johnb 0:1c4d3c2753d4 41
johnb 0:1c4d3c2753d4 42 /* Network channel to use */
johnb 0:1c4d3c2753d4 43 const XBeeApiCmdAt::channel_t myChannelId = 14;
johnb 0:1c4d3c2753d4 44
johnb 0:1c4d3c2753d4 45 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:1c4d3c2753d4 46
johnb 0:1c4d3c2753d4 47 /** Circular buffer to receive the data frames from the XBee. Any frames
johnb 0:1c4d3c2753d4 48 that won't fit in the buffer when they are received will be lost */
johnb 0:1c4d3c2753d4 49 XBeeApiRxFrameCircularBuffer rxBuffer( 10, &xbeeDevice );
johnb 0:1c4d3c2753d4 50
johnb 0:1c4d3c2753d4 51 void dumpFrame( const XBeeApiRxFrame* const p_frame )
johnb 0:1c4d3c2753d4 52 {
johnb 0:1c4d3c2753d4 53 const uint8_t* outData;
johnb 0:1c4d3c2753d4 54 uint16_t outLoop;
johnb 0:1c4d3c2753d4 55 pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() );
johnb 0:1c4d3c2753d4 56 p_frame->getDataPtr( 0, &outData, &outLoop );
johnb 0:1c4d3c2753d4 57 pc.printf("Data [%d]: ",outLoop);
johnb 0:1c4d3c2753d4 58 for( ;
johnb 0:1c4d3c2753d4 59 outLoop > 0;
johnb 0:1c4d3c2753d4 60 outLoop--,outData++ )
johnb 0:1c4d3c2753d4 61 {
johnb 0:1c4d3c2753d4 62 pc.printf("0x%02X ",*outData);
johnb 0:1c4d3c2753d4 63 }
johnb 0:1c4d3c2753d4 64 pc.printf("\r\n");
johnb 0:1c4d3c2753d4 65 }
johnb 0:1c4d3c2753d4 66
johnb 0:1c4d3c2753d4 67 int main() {
johnb 0:1c4d3c2753d4 68 /* This example will use the blocking API for simplicity */
johnb 0:1c4d3c2753d4 69 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:1c4d3c2753d4 70
johnb 0:1c4d3c2753d4 71 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:1c4d3c2753d4 72
johnb 0:1c4d3c2753d4 73 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:1c4d3c2753d4 74 This step may not be needed in the case that the XBee has already been configured
johnb 0:1c4d3c2753d4 75 to use Mode 2 and the setting has been stored in NV */
johnb 0:1c4d3c2753d4 76 status = xbeeDevice.setUpApi();
johnb 0:1c4d3c2753d4 77
johnb 0:1c4d3c2753d4 78 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:1c4d3c2753d4 79 {
johnb 2:34b3b5b1f01d 80 /* Set the 16-bit source address of this XBee */
johnb 2:34b3b5b1f01d 81 atIf.setSourceAddress( myNetworkAddress );
johnb 2:34b3b5b1f01d 82
johnb 0:1c4d3c2753d4 83 /* Set up a peer-to-peer network using the specified PAN and channel */
johnb 0:1c4d3c2753d4 84 if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
johnb 0:1c4d3c2753d4 85 {
johnb 2:34b3b5b1f01d 86 const XBeeApiRxFrame* frameP;
johnb 2:34b3b5b1f01d 87 while( 1 )
johnb 0:1c4d3c2753d4 88 {
johnb 2:34b3b5b1f01d 89 /* Wait for a while, report how many frames are in the buffer then clear
johnb 2:34b3b5b1f01d 90 clear the buffer out for the next loop around */
johnb 2:34b3b5b1f01d 91 wait(10);
johnb 2:34b3b5b1f01d 92
johnb 2:34b3b5b1f01d 93 pc.printf("Received frames: %d\r\n",rxBuffer.getFrameCount() );
johnb 2:34b3b5b1f01d 94
johnb 2:34b3b5b1f01d 95 /* Work through all of the frames in the buffer */
johnb 2:34b3b5b1f01d 96 while( NULL != ( frameP = rxBuffer.getTailPtr()))
johnb 2:34b3b5b1f01d 97 {
johnb 2:34b3b5b1f01d 98 /* Dump out some information from the frame */
johnb 2:34b3b5b1f01d 99 dumpFrame( frameP );
johnb 2:34b3b5b1f01d 100 /* Remove the frame from the buffer */
johnb 2:34b3b5b1f01d 101 rxBuffer.pop();
johnb 2:34b3b5b1f01d 102 }
johnb 2:34b3b5b1f01d 103 }
johnb 0:1c4d3c2753d4 104 }
johnb 2:34b3b5b1f01d 105 else
johnb 2:34b3b5b1f01d 106 {
johnb 2:34b3b5b1f01d 107 pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
johnb 2:34b3b5b1f01d 108 }
johnb 0:1c4d3c2753d4 109 }
johnb 0:1c4d3c2753d4 110 else
johnb 0:1c4d3c2753d4 111 {
johnb 0:1c4d3c2753d4 112 pc.printf("setUpApi failed with status %d\r\n",status);
johnb 0:1c4d3c2753d4 113 }
johnb 0:1c4d3c2753d4 114 }