Simple example of using XBeeApi to send AT-style commands to the XBee

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Mon Mar 31 19:42:44 2014 +0000
Revision:
0:b0f8e1a438e0
Child:
1:0ee7020a1b90
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:b0f8e1a438e0 1 /**
johnb 0:b0f8e1a438e0 2 @file
johnb 0:b0f8e1a438e0 3 @brief Example of using the XBeeApi library to send AT-style
johnb 0:b0f8e1a438e0 4 commands to change settings in the XBee
johnb 0:b0f8e1a438e0 5
johnb 0:b0f8e1a438e0 6 @author John Bailey
johnb 0:b0f8e1a438e0 7
johnb 0:b0f8e1a438e0 8 @copyright Copyright 2014 John Bailey
johnb 0:b0f8e1a438e0 9
johnb 0:b0f8e1a438e0 10 @section LICENSE
johnb 0:b0f8e1a438e0 11
johnb 0:b0f8e1a438e0 12 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:b0f8e1a438e0 13 you may not use this file except in compliance with the License.
johnb 0:b0f8e1a438e0 14 You may obtain a copy of the License at
johnb 0:b0f8e1a438e0 15
johnb 0:b0f8e1a438e0 16 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:b0f8e1a438e0 17
johnb 0:b0f8e1a438e0 18 Unless required by applicable law or agreed to in writing, software
johnb 0:b0f8e1a438e0 19 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:b0f8e1a438e0 20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:b0f8e1a438e0 21 See the License for the specific language governing permissions and
johnb 0:b0f8e1a438e0 22 limitations under the License.
johnb 0:b0f8e1a438e0 23
johnb 0:b0f8e1a438e0 24 */
johnb 0:b0f8e1a438e0 25
johnb 0:b0f8e1a438e0 26 #include "mbed.h"
johnb 0:b0f8e1a438e0 27 #include "xbeeapi.hpp"
johnb 0:b0f8e1a438e0 28
johnb 0:b0f8e1a438e0 29 /* TODO: You may need to change these based on the device/connections that you're using */
johnb 0:b0f8e1a438e0 30 #define XBEE_TX_PIN PTA2
johnb 0:b0f8e1a438e0 31 #define XBEE_RX_PIN PTA1
johnb 0:b0f8e1a438e0 32
johnb 0:b0f8e1a438e0 33 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:b0f8e1a438e0 34
johnb 0:b0f8e1a438e0 35 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:b0f8e1a438e0 36
johnb 0:b0f8e1a438e0 37 int main() {
johnb 0:b0f8e1a438e0 38 /* This example will use the blocking API for simplicity */
johnb 0:b0f8e1a438e0 39 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:b0f8e1a438e0 40
johnb 0:b0f8e1a438e0 41 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:b0f8e1a438e0 42
johnb 0:b0f8e1a438e0 43 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:b0f8e1a438e0 44 This step may not be needed in the case that the XBee has already been configured
johnb 0:b0f8e1a438e0 45 to use Mode 2 and the setting has been stored in NV */
johnb 0:b0f8e1a438e0 46 status = xbeeDevice.setUpApi();
johnb 0:b0f8e1a438e0 47
johnb 0:b0f8e1a438e0 48 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:b0f8e1a438e0 49 {
johnb 0:b0f8e1a438e0 50 uint16_t fwVersion;
johnb 0:b0f8e1a438e0 51 uint8_t chan;
johnb 0:b0f8e1a438e0 52
johnb 0:b0f8e1a438e0 53 /* Ask the XBee for the firmware version it's running.
johnb 0:b0f8e1a438e0 54
johnb 0:b0f8e1a438e0 55 This function is generally used when employing the non-blocking API in order to
johnb 0:b0f8e1a438e0 56 kick off the process of retrieving the data. Once the request has been sent
johnb 0:b0f8e1a438e0 57 the method returns and the response is processed asynchronously. Once received
johnb 0:b0f8e1a438e0 58 the data's available via the getXXX methods.
johnb 0:b0f8e1a438e0 59
johnb 0:b0f8e1a438e0 60 In the case that the blocking API is used the requestXXX methods are optional
johnb 0:b0f8e1a438e0 61 because the getXXX methods will send the request and then block until the response
johnb 0:b0f8e1a438e0 62 is received. The main potential advantage to still (selectively) using the requestXXX
johnb 0:b0f8e1a438e0 63 methods even when employing the blocking API is timing - the getXXX functions do not
johnb 0:b0f8e1a438e0 64 have to communicate with the XBee and can use the previously retrieved data
johnb 0:b0f8e1a438e0 65 meaning that there is no need to block on a communication exchange */
johnb 0:b0f8e1a438e0 66 atIf.requestFirmwareVersion();
johnb 0:b0f8e1a438e0 67
johnb 0:b0f8e1a438e0 68 /* Attempt to get the previously requested firmware version. */
johnb 0:b0f8e1a438e0 69 if( atIf.getFirmwareVersion( &fwVersion ) )
johnb 0:b0f8e1a438e0 70 {
johnb 0:b0f8e1a438e0 71 /* Get the channel number. As noted above, we don't need to use the requestXXX
johnb 0:b0f8e1a438e0 72 in the case that we're using the blocking API */
johnb 0:b0f8e1a438e0 73 if( atIf.getChannel( &chan ) )
johnb 0:b0f8e1a438e0 74 {
johnb 0:b0f8e1a438e0 75 pc.printf("Firmware version: 0x%04x, channel ID 0x%02x",fwVersion,chan);
johnb 0:b0f8e1a438e0 76 }
johnb 0:b0f8e1a438e0 77 else
johnb 0:b0f8e1a438e0 78 {
johnb 0:b0f8e1a438e0 79 pc.printf("Retrieval of channel number was unsuccessful\r\n");
johnb 0:b0f8e1a438e0 80 }
johnb 0:b0f8e1a438e0 81 }
johnb 0:b0f8e1a438e0 82 else
johnb 0:b0f8e1a438e0 83 {
johnb 0:b0f8e1a438e0 84 pc.printf("Retrieval of firmware version was unsuccessful\r\n");
johnb 0:b0f8e1a438e0 85 }
johnb 0:b0f8e1a438e0 86 }
johnb 0:b0f8e1a438e0 87 else
johnb 0:b0f8e1a438e0 88 {
johnb 0:b0f8e1a438e0 89 pc.printf("setUpApi was unsuccessful - return code %d\r\n", status);
johnb 0:b0f8e1a438e0 90 }
johnb 0:b0f8e1a438e0 91 }