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

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Mon Mar 31 19:57:48 2014 +0000
Revision:
1:0ee7020a1b90
Parent:
0:b0f8e1a438e0
Update example to write setting as well as reading

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 1:0ee7020a1b90 33 const uint8_t channelNo = 0xd;
johnb 1:0ee7020a1b90 34
johnb 0:b0f8e1a438e0 35 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:b0f8e1a438e0 36
johnb 0:b0f8e1a438e0 37 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:b0f8e1a438e0 38
johnb 0:b0f8e1a438e0 39 int main() {
johnb 0:b0f8e1a438e0 40 /* This example will use the blocking API for simplicity */
johnb 0:b0f8e1a438e0 41 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:b0f8e1a438e0 42
johnb 0:b0f8e1a438e0 43 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:b0f8e1a438e0 44
johnb 0:b0f8e1a438e0 45 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:b0f8e1a438e0 46 This step may not be needed in the case that the XBee has already been configured
johnb 0:b0f8e1a438e0 47 to use Mode 2 and the setting has been stored in NV */
johnb 0:b0f8e1a438e0 48 status = xbeeDevice.setUpApi();
johnb 0:b0f8e1a438e0 49
johnb 0:b0f8e1a438e0 50 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:b0f8e1a438e0 51 {
johnb 0:b0f8e1a438e0 52 uint16_t fwVersion;
johnb 0:b0f8e1a438e0 53 uint8_t chan;
johnb 1:0ee7020a1b90 54 uint8_t newChan;
johnb 0:b0f8e1a438e0 55
johnb 0:b0f8e1a438e0 56 /* Ask the XBee for the firmware version it's running.
johnb 0:b0f8e1a438e0 57
johnb 0:b0f8e1a438e0 58 This function is generally used when employing the non-blocking API in order to
johnb 0:b0f8e1a438e0 59 kick off the process of retrieving the data. Once the request has been sent
johnb 0:b0f8e1a438e0 60 the method returns and the response is processed asynchronously. Once received
johnb 0:b0f8e1a438e0 61 the data's available via the getXXX methods.
johnb 0:b0f8e1a438e0 62
johnb 0:b0f8e1a438e0 63 In the case that the blocking API is used the requestXXX methods are optional
johnb 0:b0f8e1a438e0 64 because the getXXX methods will send the request and then block until the response
johnb 0:b0f8e1a438e0 65 is received. The main potential advantage to still (selectively) using the requestXXX
johnb 0:b0f8e1a438e0 66 methods even when employing the blocking API is timing - the getXXX functions do not
johnb 0:b0f8e1a438e0 67 have to communicate with the XBee and can use the previously retrieved data
johnb 0:b0f8e1a438e0 68 meaning that there is no need to block on a communication exchange */
johnb 0:b0f8e1a438e0 69 atIf.requestFirmwareVersion();
johnb 0:b0f8e1a438e0 70
johnb 0:b0f8e1a438e0 71 /* Attempt to get the previously requested firmware version. */
johnb 0:b0f8e1a438e0 72 if( atIf.getFirmwareVersion( &fwVersion ) )
johnb 0:b0f8e1a438e0 73 {
johnb 0:b0f8e1a438e0 74 /* Get the channel number. As noted above, we don't need to use the requestXXX
johnb 0:b0f8e1a438e0 75 in the case that we're using the blocking API */
johnb 0:b0f8e1a438e0 76 if( atIf.getChannel( &chan ) )
johnb 0:b0f8e1a438e0 77 {
johnb 1:0ee7020a1b90 78 /* Change the channe ID to something new */
johnb 1:0ee7020a1b90 79 atIf.setChannel( channelNo );
johnb 1:0ee7020a1b90 80
johnb 1:0ee7020a1b90 81 /* Retrieve the channel ID again, after we've changed it */
johnb 1:0ee7020a1b90 82 atIf.getChannel( &newChan );
johnb 1:0ee7020a1b90 83
johnb 1:0ee7020a1b90 84 pc.printf("Firmware version: 0x%04x\r\nOriginal channel ID 0x%02x, new channel ID 0x%02x",fwVersion,chan,newChan);
johnb 0:b0f8e1a438e0 85 }
johnb 0:b0f8e1a438e0 86 else
johnb 0:b0f8e1a438e0 87 {
johnb 0:b0f8e1a438e0 88 pc.printf("Retrieval of channel number was unsuccessful\r\n");
johnb 0:b0f8e1a438e0 89 }
johnb 0:b0f8e1a438e0 90 }
johnb 0:b0f8e1a438e0 91 else
johnb 0:b0f8e1a438e0 92 {
johnb 0:b0f8e1a438e0 93 pc.printf("Retrieval of firmware version was unsuccessful\r\n");
johnb 0:b0f8e1a438e0 94 }
johnb 0:b0f8e1a438e0 95 }
johnb 0:b0f8e1a438e0 96 else
johnb 0:b0f8e1a438e0 97 {
johnb 0:b0f8e1a438e0 98 pc.printf("setUpApi was unsuccessful - return code %d\r\n", status);
johnb 0:b0f8e1a438e0 99 }
johnb 0:b0f8e1a438e0 100 }