![](/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@1:953e5affd67a, 2014-03-25 (annotated)
- Committer:
- johnb
- Date:
- Tue Mar 25 20:23:40 2014 +0000
- Revision:
- 1:953e5affd67a
- Parent:
- 0:99422855f301
- Child:
- 2:a553ea23194b
Use some defines/consts to make the code more readable
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 | 0:99422855f301 | 4 | This example has a minimum of error checking in order |
johnb | 0:99422855f301 | 5 | to keep the code compact |
johnb | 0:99422855f301 | 6 | |
johnb | 0:99422855f301 | 7 | @author John Bailey |
johnb | 0:99422855f301 | 8 | |
johnb | 0:99422855f301 | 9 | @copyright Copyright 2014 John Bailey |
johnb | 0:99422855f301 | 10 | |
johnb | 0:99422855f301 | 11 | @section LICENSE |
johnb | 0:99422855f301 | 12 | |
johnb | 0:99422855f301 | 13 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:99422855f301 | 14 | you may not use this file except in compliance with the License. |
johnb | 0:99422855f301 | 15 | You may obtain a copy of the License at |
johnb | 0:99422855f301 | 16 | |
johnb | 0:99422855f301 | 17 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:99422855f301 | 18 | |
johnb | 0:99422855f301 | 19 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:99422855f301 | 20 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:99422855f301 | 21 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:99422855f301 | 22 | See the License for the specific language governing permissions and |
johnb | 0:99422855f301 | 23 | limitations under the License. |
johnb | 0:99422855f301 | 24 | |
johnb | 0:99422855f301 | 25 | */ |
johnb | 0:99422855f301 | 26 | |
johnb | 0:99422855f301 | 27 | #include "mbed.h" |
johnb | 0:99422855f301 | 28 | #include "xbeeapi.hpp" |
johnb | 0:99422855f301 | 29 | |
johnb | 1:953e5affd67a | 30 | const uint8_t tx_data[] = { 'H', 'E', 'L', 'L', 'O' }; |
johnb | 0:99422855f301 | 31 | |
johnb | 0:99422855f301 | 32 | /* TODO: You may need to change these based on the device/connections that you're using */ |
johnb | 1:953e5affd67a | 33 | #define XBEE_TX_PIN PTA2 |
johnb | 1:953e5affd67a | 34 | #define XBEE_RX_PIN PTA1 |
johnb | 1:953e5affd67a | 35 | |
johnb | 1:953e5affd67a | 36 | /* Network address for our XBee */ |
johnb | 1:953e5affd67a | 37 | const uint16_t myNetworkAddress = 0x1234; |
johnb | 1:953e5affd67a | 38 | |
johnb | 1:953e5affd67a | 39 | /* ID for the Personal Area Network we're going to join */ |
johnb | 1:953e5affd67a | 40 | const XBeeApiCmdAt::panId_t myPANId = 1000; |
johnb | 1:953e5affd67a | 41 | |
johnb | 1:953e5affd67a | 42 | /* Network channel to use */ |
johnb | 1:953e5affd67a | 43 | const XBeeApiCmdAt::channel_t myChannelId = 14; |
johnb | 1:953e5affd67a | 44 | |
johnb | 1:953e5affd67a | 45 | /* This example sends a frame to a specific destination address - we could set |
johnb | 1:953e5affd67a | 46 | the frame to be broadcast by calling the setDestAddrBroadcast() method |
johnb | 1:953e5affd67a | 47 | instead of setDestAddr() in the function below. |
johnb | 1:953e5affd67a | 48 | By default 16-bit addressing mode is used. setDestAddrType can be used |
johnb | 1:953e5affd67a | 49 | to select between 16-bit and 64-bit addressing modes */ |
johnb | 1:953e5affd67a | 50 | const uint16_t frameDestinationAddress = 0x01; |
johnb | 1:953e5affd67a | 51 | |
johnb | 1:953e5affd67a | 52 | XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC ); |
johnb | 0:99422855f301 | 53 | |
johnb | 0:99422855f301 | 54 | int main() { |
johnb | 0:99422855f301 | 55 | /* This example will use the blocking API for simplicity */ |
johnb | 0:99422855f301 | 56 | XBeeApiCmdAtBlocking atIf( &xbeeDevice ); |
johnb | 0:99422855f301 | 57 | |
johnb | 0:99422855f301 | 58 | XBeeDevice::XBeeDeviceReturn_t status; |
johnb | 0:99422855f301 | 59 | |
johnb | 0:99422855f301 | 60 | /* This is the frame we're going to transmit */ |
johnb | 0:99422855f301 | 61 | XBeeApiTxFrameEx frame( &xbeeDevice ); |
johnb | 0:99422855f301 | 62 | |
johnb | 0:99422855f301 | 63 | /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions. |
johnb | 0:99422855f301 | 64 | This step may not be needed in the case that the XBee has already been configured |
johnb | 0:99422855f301 | 65 | to use Mode 2 and the setting has been stored in NV */ |
johnb | 0:99422855f301 | 66 | status = xbeeDevice.setUpApi(); |
johnb | 0:99422855f301 | 67 | |
johnb | 0:99422855f301 | 68 | if( status != XBeeDevice::XBEEDEVICE_OK ) |
johnb | 0:99422855f301 | 69 | { |
johnb | 0:99422855f301 | 70 | /* Set the 16-bit source address of this XBee */ |
johnb | 1:953e5affd67a | 71 | atIf.setSourceAddress( myNetworkAddress ); |
johnb | 0:99422855f301 | 72 | /* Set up a peer-to-peer network using the specified PAN and channel */ |
johnb | 1:953e5affd67a | 73 | xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ); |
johnb | 0:99422855f301 | 74 | |
johnb | 0:99422855f301 | 75 | /* Set the data pointer & destination address in the transmit frame */ |
johnb | 0:99422855f301 | 76 | frame.setDataPtr( tx_data, sizeof( tx_data ) ); |
johnb | 1:953e5affd67a | 77 | frame.setDestAddr( frameDestinationAddress ); |
johnb | 0:99422855f301 | 78 | |
johnb | 0:99422855f301 | 79 | xbeeDevice.SendFrame( &frame ); |
johnb | 0:99422855f301 | 80 | } |
johnb | 0:99422855f301 | 81 | } |