Simple example of using XBeeApi to receive data packets via wireless

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Wed Apr 02 21:45:08 2014 +0000
Revision:
1:36ac23927246
Parent:
0:1c4d3c2753d4
Child:
2:34b3b5b1f01d
Remove redundant variable

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