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