Example for XBeeAPI; a little more involved than XBeeApiSimpleBroadcastExample with report on failure to set up the XBee and on the transmit status of the message.

Dependencies:   XBeeApi XBeeApiSimpleBroadcastExample mbed

Fork of XBeeApiSimpleBroadcastExample by John Bailey

Committer:
johnb
Date:
Fri Jun 27 19:18:39 2014 +0000
Revision:
4:4c6644fd040b
Parent:
3:8fea787c2199
Child:
5:481ec3d3737f
Based on XBeeApiSimpleBroadcastExample, a slightly more involved version which reports failure to set up the XBee and on the transmit status of the message.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:99422855f301 1 /**
johnb 0:99422855f301 2 @file
johnb 0:99422855f301 3 @brief Example of using the XBeeApi library to broadcast a message
johnb 4:4c6644fd040b 4 This example has more error checking than
johnb 4:4c6644fd040b 5 XBeeApiSimpleBroadcast example and should improve the changes
johnb 4:4c6644fd040b 6 of detecting and diagnosing a problem.
johnb 0:99422855f301 7
johnb 0:99422855f301 8 @author John Bailey
johnb 0:99422855f301 9
johnb 0:99422855f301 10 @copyright Copyright 2014 John Bailey
johnb 0:99422855f301 11
johnb 0:99422855f301 12 @section LICENSE
johnb 0:99422855f301 13
johnb 0:99422855f301 14 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:99422855f301 15 you may not use this file except in compliance with the License.
johnb 0:99422855f301 16 You may obtain a copy of the License at
johnb 0:99422855f301 17
johnb 0:99422855f301 18 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:99422855f301 19
johnb 0:99422855f301 20 Unless required by applicable law or agreed to in writing, software
johnb 0:99422855f301 21 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:99422855f301 22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:99422855f301 23 See the License for the specific language governing permissions and
johnb 0:99422855f301 24 limitations under the License.
johnb 0:99422855f301 25
johnb 0:99422855f301 26 */
johnb 0:99422855f301 27
johnb 0:99422855f301 28 #include "mbed.h"
johnb 0:99422855f301 29 #include "xbeeapi.hpp"
johnb 0:99422855f301 30
johnb 1:953e5affd67a 31 const uint8_t tx_data[] = { 'H', 'E', 'L', 'L', 'O' };
johnb 0:99422855f301 32
johnb 0:99422855f301 33 /* TODO: You may need to change these based on the device/connections that you're using */
johnb 1:953e5affd67a 34 #define XBEE_TX_PIN PTA2
johnb 1:953e5affd67a 35 #define XBEE_RX_PIN PTA1
johnb 1:953e5affd67a 36
johnb 4:4c6644fd040b 37 #define WAIT_FOR_TX_TIME 500
johnb 4:4c6644fd040b 38
johnb 4:4c6644fd040b 39 Serial pc(USBTX, USBRX); // tx, rx
johnb 4:4c6644fd040b 40
johnb 1:953e5affd67a 41 /* Network address for our XBee */
johnb 1:953e5affd67a 42 const uint16_t myNetworkAddress = 0x1234;
johnb 1:953e5affd67a 43
johnb 1:953e5affd67a 44 /* ID for the Personal Area Network we're going to join */
johnb 1:953e5affd67a 45 const XBeeApiCmdAt::panId_t myPANId = 1000;
johnb 1:953e5affd67a 46
johnb 1:953e5affd67a 47 /* Network channel to use */
johnb 1:953e5affd67a 48 const XBeeApiCmdAt::channel_t myChannelId = 14;
johnb 1:953e5affd67a 49
johnb 1:953e5affd67a 50 /* This example sends a frame to a specific destination address - we could set
johnb 1:953e5affd67a 51 the frame to be broadcast by calling the setDestAddrBroadcast() method
johnb 1:953e5affd67a 52 instead of setDestAddr() in the function below.
johnb 1:953e5affd67a 53 By default 16-bit addressing mode is used. setDestAddrType can be used
johnb 1:953e5affd67a 54 to select between 16-bit and 64-bit addressing modes */
johnb 1:953e5affd67a 55 const uint16_t frameDestinationAddress = 0x01;
johnb 1:953e5affd67a 56
johnb 1:953e5affd67a 57 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:99422855f301 58
johnb 0:99422855f301 59 int main() {
johnb 0:99422855f301 60 /* This example will use the blocking API for simplicity */
johnb 0:99422855f301 61 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:99422855f301 62
johnb 0:99422855f301 63 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:99422855f301 64
johnb 4:4c6644fd040b 65 /* This is the frame we're going to transmit - using the extended version so that
johnb 4:4c6644fd040b 66 stats are available */
johnb 4:4c6644fd040b 67 XBeeApiTxFrameEx frame( &xbeeDevice );
johnb 0:99422855f301 68
johnb 0:99422855f301 69 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:99422855f301 70 This step may not be needed in the case that the XBee has already been configured
johnb 0:99422855f301 71 to use Mode 2 and the setting has been stored in NV */
johnb 0:99422855f301 72 status = xbeeDevice.setUpApi();
johnb 0:99422855f301 73
johnb 3:8fea787c2199 74 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:99422855f301 75 {
johnb 0:99422855f301 76 /* Set the 16-bit source address of this XBee */
johnb 1:953e5affd67a 77 atIf.setSourceAddress( myNetworkAddress );
johnb 0:99422855f301 78 /* Set up a peer-to-peer network using the specified PAN and channel */
johnb 1:953e5affd67a 79 xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId );
johnb 0:99422855f301 80
johnb 0:99422855f301 81 /* Set the data pointer & destination address in the transmit frame */
johnb 0:99422855f301 82 frame.setDataPtr( tx_data, sizeof( tx_data ) );
johnb 1:953e5affd67a 83 frame.setDestAddr( frameDestinationAddress );
johnb 0:99422855f301 84
johnb 4:4c6644fd040b 85 xbeeDevice.SendFrame( &frame );
johnb 4:4c6644fd040b 86
johnb 4:4c6644fd040b 87 /* Arbitary wait for the TX to occurr. We could poll getMostRecentStatus()
johnb 4:4c6644fd040b 88 instead of just waiting */
johnb 4:4c6644fd040b 89 wait_ms( WAIT_FOR_TX_TIME );
johnb 4:4c6644fd040b 90
johnb 4:4c6644fd040b 91 pc.printf( "\r\nTX Status: ");
johnb 4:4c6644fd040b 92 switch( frame.getMostRecentStatus() )
johnb 4:4c6644fd040b 93 {
johnb 4:4c6644fd040b 94 case XBeeApiTxFrame::XBEE_API_TX_STATUS_LAST:
johnb 4:4c6644fd040b 95 /* Didn't receive a response from the XBee indicating
johnb 4:4c6644fd040b 96 the transmit status */
johnb 4:4c6644fd040b 97 pc.printf("No TX confirmation received from XBee");
johnb 4:4c6644fd040b 98 break;
johnb 4:4c6644fd040b 99 case XBeeApiTxFrame::XBEE_API_TX_STATUS_OK:
johnb 4:4c6644fd040b 100 /* Frame was transmitted OK and at least one other network
johnb 4:4c6644fd040b 101 node acknowledged it */
johnb 4:4c6644fd040b 102 pc.printf("OK");
johnb 4:4c6644fd040b 103 break;
johnb 4:4c6644fd040b 104 case XBeeApiTxFrame::XBEE_API_TX_STATUS_NO_ACK:
johnb 4:4c6644fd040b 105 /* This means that the message was transmitted by
johnb 4:4c6644fd040b 106 the XBee but no other nodes on the network acknowledged it.
johnb 4:4c6644fd040b 107 Could be other nodes are not on the same PAN, channel, etc) */
johnb 4:4c6644fd040b 108 pc.printf("No acknowledgement received");
johnb 4:4c6644fd040b 109 break;
johnb 4:4c6644fd040b 110 default:
johnb 4:4c6644fd040b 111 /* In this example we don't expect a response other than
johnb 4:4c6644fd040b 112 those explicitly handled above */
johnb 4:4c6644fd040b 113 pc.printf("Unexpected response received");
johnb 4:4c6644fd040b 114 break;
johnb 4:4c6644fd040b 115 }
johnb 4:4c6644fd040b 116 }
johnb 4:4c6644fd040b 117 else
johnb 4:4c6644fd040b 118 {
johnb 4:4c6644fd040b 119 pc.printf("setUpApi failed with status %d\r\n",status);
johnb 0:99422855f301 120 }
johnb 0:99422855f301 121 }