![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@5:481ec3d3737f, 2014-07-05 (annotated)
- Committer:
- johnb
- Date:
- Sat Jul 05 16:57:36 2014 +0000
- Revision:
- 5:481ec3d3737f
- Parent:
- 4:4c6644fd040b
Update to report failure of xbeeSetNetworkTypeP2P() call
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:481ec3d3737f | 79 | if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) ) |
johnb | 4:4c6644fd040b | 80 | { |
johnb | 5:481ec3d3737f | 81 | |
johnb | 5:481ec3d3737f | 82 | /* Set the data pointer & destination address in the transmit frame */ |
johnb | 5:481ec3d3737f | 83 | frame.setDataPtr( tx_data, sizeof( tx_data ) ); |
johnb | 5:481ec3d3737f | 84 | frame.setDestAddr( frameDestinationAddress ); |
johnb | 5:481ec3d3737f | 85 | |
johnb | 5:481ec3d3737f | 86 | xbeeDevice.SendFrame( &frame ); |
johnb | 5:481ec3d3737f | 87 | |
johnb | 5:481ec3d3737f | 88 | /* Arbitary wait for the TX to occurr. We could poll getMostRecentStatus() |
johnb | 5:481ec3d3737f | 89 | instead of just waiting */ |
johnb | 5:481ec3d3737f | 90 | wait_ms( WAIT_FOR_TX_TIME ); |
johnb | 5:481ec3d3737f | 91 | |
johnb | 5:481ec3d3737f | 92 | pc.printf( "\r\nTX Status: "); |
johnb | 5:481ec3d3737f | 93 | switch( frame.getMostRecentStatus() ) |
johnb | 5:481ec3d3737f | 94 | { |
johnb | 5:481ec3d3737f | 95 | case XBeeApiTxFrame::XBEE_API_TX_STATUS_LAST: |
johnb | 5:481ec3d3737f | 96 | /* Didn't receive a response from the XBee indicating |
johnb | 5:481ec3d3737f | 97 | the transmit status */ |
johnb | 5:481ec3d3737f | 98 | pc.printf("No TX confirmation received from XBee"); |
johnb | 5:481ec3d3737f | 99 | break; |
johnb | 5:481ec3d3737f | 100 | case XBeeApiTxFrame::XBEE_API_TX_STATUS_OK: |
johnb | 5:481ec3d3737f | 101 | /* Frame was transmitted OK and at least one other network |
johnb | 5:481ec3d3737f | 102 | node acknowledged it */ |
johnb | 5:481ec3d3737f | 103 | pc.printf("OK"); |
johnb | 5:481ec3d3737f | 104 | break; |
johnb | 5:481ec3d3737f | 105 | case XBeeApiTxFrame::XBEE_API_TX_STATUS_NO_ACK: |
johnb | 5:481ec3d3737f | 106 | /* This means that the message was transmitted by |
johnb | 5:481ec3d3737f | 107 | the XBee but no other nodes on the network acknowledged it. |
johnb | 5:481ec3d3737f | 108 | Could be other nodes are not on the same PAN, channel, etc) */ |
johnb | 5:481ec3d3737f | 109 | pc.printf("No acknowledgement received"); |
johnb | 5:481ec3d3737f | 110 | break; |
johnb | 5:481ec3d3737f | 111 | default: |
johnb | 5:481ec3d3737f | 112 | /* In this example we don't expect a response other than |
johnb | 5:481ec3d3737f | 113 | those explicitly handled above */ |
johnb | 5:481ec3d3737f | 114 | pc.printf("Unexpected response received"); |
johnb | 5:481ec3d3737f | 115 | break; |
johnb | 5:481ec3d3737f | 116 | } |
johnb | 5:481ec3d3737f | 117 | } |
johnb | 5:481ec3d3737f | 118 | else |
johnb | 5:481ec3d3737f | 119 | { |
johnb | 5:481ec3d3737f | 120 | pc.printf("xbeeSetNetworkTypeP2P reported failure\r\n"); |
johnb | 4:4c6644fd040b | 121 | } |
johnb | 4:4c6644fd040b | 122 | } |
johnb | 4:4c6644fd040b | 123 | else |
johnb | 4:4c6644fd040b | 124 | { |
johnb | 4:4c6644fd040b | 125 | pc.printf("setUpApi failed with status %d\r\n",status); |
johnb | 0:99422855f301 | 126 | } |
johnb | 0:99422855f301 | 127 | } |