John Bailey / Mbed 2 deprecated XBeeApiBroadcastExampleRTOS

Dependencies:   XBeeApi mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002    @file
00003    @brief Example of using the XBeeApi library to broadcast a message
00004           This example has more error checking than
00005           XBeeApiSimpleBroadcast example and should improve the changes
00006           of detecting and diagnosing a problem.
00007           
00008           This example shows how to use the library when using mbed-rtos.
00009           Before compiling you must open "XbeeApi\Config\XBeeApiCfg.hpp"
00010           and change the '#if 0' to '#if 1' on the line above the comment
00011           reading "Use RTOS features to make XBeeApi threadsafe"
00012 
00013    @author John Bailey 
00014 
00015    @copyright Copyright 2014 John Bailey
00016 
00017    @section LICENSE
00018    
00019 Licensed under the Apache License, Version 2.0 (the "License");
00020 you may not use this file except in compliance with the License.
00021 You may obtain a copy of the License at
00022 
00023     http://www.apache.org/licenses/LICENSE-2.0
00024 
00025 Unless required by applicable law or agreed to in writing, software
00026 distributed under the License is distributed on an "AS IS" BASIS,
00027 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00028 See the License for the specific language governing permissions and
00029 limitations under the License.
00030 
00031 */
00032 
00033 #include "mbed.h"   
00034 #include "xbeeapi.hpp"
00035  
00036 const uint8_t tx_data[] = { 'H', 'E', 'L', 'L', 'O' };
00037 
00038 /* TODO: You may need to change these based on the device/connections that you're using */ 
00039 #define XBEE_TX_PIN PTA2
00040 #define XBEE_RX_PIN PTA1
00041 
00042 #define WAIT_FOR_TX_TIME 500
00043 
00044 Serial pc(USBTX, USBRX); // tx, rx
00045 
00046 /* Network address for our XBee */
00047 const uint16_t myNetworkAddress = 0x1234;
00048 
00049 /* ID for the Personal Area Network we're going to join */
00050 const XBeeApiCmdAt::panId_t myPANId = 1000;
00051 
00052 /* Network channel to use */
00053 const XBeeApiCmdAt::channel_t myChannelId = 14;
00054 
00055 /* This example sends a frame to a specific destination address - we could set
00056    the frame to be broadcast by calling the setDestAddrBroadcast() method
00057    instead of setDestAddr() in the function below.
00058    By default 16-bit addressing mode is used.  setDestAddrType can be used
00059    to select between 16-bit and 64-bit addressing modes */
00060 const uint16_t frameDestinationAddress = 0x01;
00061 
00062 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
00063    
00064 int main() {
00065     /* This example will use the blocking API for simplicity */   
00066     XBeeApiCmdAtBlocking atIf( &xbeeDevice );
00067 
00068     XBeeDevice::XBeeDeviceReturn_t status;
00069 
00070     /* This is the frame we're going to transmit - using the extended version so that
00071        stats are available */
00072     XBeeApiTxFrameEx frame( &xbeeDevice );
00073         
00074     if( !xbeeDevice.setupDispatchTask() )
00075     {
00076         pc.printf("setupDispatchTask() failed - XBeeDevice class won't receive any data\r\n");
00077     }
00078         
00079     /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
00080        This step may not be needed in the case that the XBee has already been configured
00081        to use Mode 2 and the setting has been stored in NV */
00082     status = xbeeDevice.setUpApi();
00083 
00084     if( status == XBeeDevice::XBEEDEVICE_OK )
00085     {
00086 
00087         /* Set the 16-bit source address of this XBee */
00088         atIf.setSourceAddress( myNetworkAddress );
00089         if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
00090         {
00091             /* Set the data pointer & destination address in the transmit frame */
00092             frame.setDataPtr( tx_data, sizeof( tx_data ) );
00093             frame.setDestAddr( frameDestinationAddress );
00094             
00095             xbeeDevice.SendFrame( &frame );
00096             
00097             /* Arbitary wait for the TX to occurr.  We could poll getMostRecentStatus() 
00098                instead of just waiting */
00099             wait_ms( WAIT_FOR_TX_TIME );
00100     
00101             pc.printf( "\r\nTX Status: ");
00102             switch( frame.getMostRecentStatus() )
00103             {
00104                 case XBeeApiTxFrame::XBEE_API_TX_STATUS_LAST:
00105                     /* Didn't receive a response from the XBee indicating
00106                        the transmit status */    
00107                     pc.printf("No TX confirmation received from XBee");
00108                     break;
00109                 case XBeeApiTxFrame::XBEE_API_TX_STATUS_OK:
00110                     /* Frame was transmitted OK and at least one other network
00111                        node acknowledged it */
00112                     pc.printf("OK");
00113                     break;
00114                 case XBeeApiTxFrame::XBEE_API_TX_STATUS_NO_ACK:
00115                     /* This means that the message was transmitted by 
00116                        the XBee but no other nodes on the network acknowledged it.
00117                        Could be other nodes are not on the same PAN, channel, etc) */
00118                     pc.printf("No acknowledgement received");
00119                     break;
00120                 default:
00121                     /* In this example we don't expect a response other than
00122                        those explicitly handled above */
00123                     pc.printf("Unexpected response received");
00124                     break;
00125             }
00126         }
00127         else
00128         {
00129             pc.printf("xbeeSetNetworkTypeP2P reported failure\r\n");
00130         }
00131     }
00132     else
00133     {
00134         pc.printf("setUpApi failed with status %d\r\n",status);
00135     }
00136 }