Example of using the XBeeApi library to send AT commands to remote XBee devices in order to read/write settings

Dependencies:   XBeeApi mbed

Committer:
johnb
Date:
Sun Jul 13 15:52:24 2014 +0000
Revision:
0:1b2f833ea9ba
Example of using the XBeeApi library to send AT commands to remote XBee devices in order to read/write settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:1b2f833ea9ba 1 /**
johnb 0:1b2f833ea9ba 2 @file
johnb 0:1b2f833ea9ba 3 @brief Example of using the XBeeApi library to send AT commands
johnb 0:1b2f833ea9ba 4 to remote XBee devices in order to read/write settings
johnb 0:1b2f833ea9ba 5
johnb 0:1b2f833ea9ba 6 @author John Bailey
johnb 0:1b2f833ea9ba 7
johnb 0:1b2f833ea9ba 8 @copyright Copyright 2014 John Bailey
johnb 0:1b2f833ea9ba 9
johnb 0:1b2f833ea9ba 10 @section LICENSE
johnb 0:1b2f833ea9ba 11
johnb 0:1b2f833ea9ba 12 Licensed under the Apache License, Version 2.0 (the "License");
johnb 0:1b2f833ea9ba 13 you may not use this file except in compliance with the License.
johnb 0:1b2f833ea9ba 14 You may obtain a copy of the License at
johnb 0:1b2f833ea9ba 15
johnb 0:1b2f833ea9ba 16 http://www.apache.org/licenses/LICENSE-2.0
johnb 0:1b2f833ea9ba 17
johnb 0:1b2f833ea9ba 18 Unless required by applicable law or agreed to in writing, software
johnb 0:1b2f833ea9ba 19 distributed under the License is distributed on an "AS IS" BASIS,
johnb 0:1b2f833ea9ba 20 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnb 0:1b2f833ea9ba 21 See the License for the specific language governing permissions and
johnb 0:1b2f833ea9ba 22 limitations under the License.
johnb 0:1b2f833ea9ba 23
johnb 0:1b2f833ea9ba 24 */
johnb 0:1b2f833ea9ba 25
johnb 0:1b2f833ea9ba 26 #include "mbed.h"
johnb 0:1b2f833ea9ba 27 #include "xbeeapi.hpp"
johnb 0:1b2f833ea9ba 28
johnb 0:1b2f833ea9ba 29 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:1b2f833ea9ba 30
johnb 0:1b2f833ea9ba 31 /* TODO: You may need to change these based on the device/connections that you're using */
johnb 0:1b2f833ea9ba 32 #define XBEE_TX_PIN PTA2
johnb 0:1b2f833ea9ba 33 #define XBEE_RX_PIN PTA1
johnb 0:1b2f833ea9ba 34
johnb 0:1b2f833ea9ba 35 /* Network address for our XBee */
johnb 0:1b2f833ea9ba 36 const uint16_t myNetworkAddress = 0x1234;
johnb 0:1b2f833ea9ba 37
johnb 0:1b2f833ea9ba 38 /* Network address for the remote XBee */
johnb 0:1b2f833ea9ba 39 const uint16_t remoteNetworkAddress = 0x4321;
johnb 0:1b2f833ea9ba 40
johnb 0:1b2f833ea9ba 41 /* ID for the Personal Area Network we're going to join */
johnb 0:1b2f833ea9ba 42 const XBeeApiCmdAt::panId_t myPANId = 1000;
johnb 0:1b2f833ea9ba 43
johnb 0:1b2f833ea9ba 44 /* Network channel to use */
johnb 0:1b2f833ea9ba 45 const XBeeApiCmdAt::channel_t myChannelId = 14;
johnb 0:1b2f833ea9ba 46
johnb 0:1b2f833ea9ba 47 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
johnb 0:1b2f833ea9ba 48
johnb 0:1b2f833ea9ba 49 int main() {
johnb 0:1b2f833ea9ba 50 /* This example will use the blocking API for simplicity */
johnb 0:1b2f833ea9ba 51 XBeeApiCmdAtBlocking atIf( &xbeeDevice );
johnb 0:1b2f833ea9ba 52
johnb 0:1b2f833ea9ba 53 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:1b2f833ea9ba 54
johnb 0:1b2f833ea9ba 55 /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
johnb 0:1b2f833ea9ba 56 This step may not be needed in the case that the XBee has already been configured
johnb 0:1b2f833ea9ba 57 to use Mode 2 and the setting has been stored in NV */
johnb 0:1b2f833ea9ba 58 status = xbeeDevice.setUpApi();
johnb 0:1b2f833ea9ba 59
johnb 0:1b2f833ea9ba 60 if( status == XBeeDevice::XBEEDEVICE_OK )
johnb 0:1b2f833ea9ba 61 {
johnb 0:1b2f833ea9ba 62 /* Set the 16-bit source address of this XBee */
johnb 0:1b2f833ea9ba 63 atIf.setSourceAddress( myNetworkAddress );
johnb 0:1b2f833ea9ba 64
johnb 0:1b2f833ea9ba 65 /* Set up a peer-to-peer network using the specified PAN and channel */
johnb 0:1b2f833ea9ba 66 if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
johnb 0:1b2f833ea9ba 67 {
johnb 0:1b2f833ea9ba 68 uint64_t sn;
johnb 0:1b2f833ea9ba 69 XBeeApiCmdAt::panId_t panId;
johnb 0:1b2f833ea9ba 70 uint8_t chan;
johnb 0:1b2f833ea9ba 71
johnb 0:1b2f833ea9ba 72 /* Set up a remote XBee device using the specified 16-bit address (64-bit address not used here) */
johnb 0:1b2f833ea9ba 73 XBeeDeviceRemoteAt remoteXBee( &xbeeDevice, remoteNetworkAddress, 0 );
johnb 0:1b2f833ea9ba 74
johnb 0:1b2f833ea9ba 75 /* Get some settings for the remote XBee */
johnb 0:1b2f833ea9ba 76 remoteXBee.requestPanId();
johnb 0:1b2f833ea9ba 77 remoteXBee.requestSerialNumber();
johnb 0:1b2f833ea9ba 78 remoteXBee.requestChannel();
johnb 0:1b2f833ea9ba 79
johnb 0:1b2f833ea9ba 80 /* Get some seetings for the local XBee */
johnb 0:1b2f833ea9ba 81 atIf.requestPanId();
johnb 0:1b2f833ea9ba 82 atIf.requestSerialNumber();
johnb 0:1b2f833ea9ba 83 atIf.requestChannel();
johnb 0:1b2f833ea9ba 84
johnb 0:1b2f833ea9ba 85 /* TODO: Add example of changing settings */
johnb 0:1b2f833ea9ba 86
johnb 0:1b2f833ea9ba 87 wait_ms( 1000 );
johnb 0:1b2f833ea9ba 88
johnb 0:1b2f833ea9ba 89 if( remoteXBee.getSerialNumber( &sn ) )
johnb 0:1b2f833ea9ba 90 {
johnb 0:1b2f833ea9ba 91 pc.printf("Remote serial number is %llu (decimal)\r\n",sn);
johnb 0:1b2f833ea9ba 92 }
johnb 0:1b2f833ea9ba 93 else
johnb 0:1b2f833ea9ba 94 {
johnb 0:1b2f833ea9ba 95 pc.printf("Didn't manage to retrieve the remote serial number\r\n");
johnb 0:1b2f833ea9ba 96 }
johnb 0:1b2f833ea9ba 97 if( remoteXBee.getPanId( &panId ) )
johnb 0:1b2f833ea9ba 98 {
johnb 0:1b2f833ea9ba 99 pc.printf("Remote PAN ID is %d\r\n",panId);
johnb 0:1b2f833ea9ba 100 }
johnb 0:1b2f833ea9ba 101 else
johnb 0:1b2f833ea9ba 102 {
johnb 0:1b2f833ea9ba 103 pc.printf("Didn't manage to retrieve the remote PAN Id\r\n");
johnb 0:1b2f833ea9ba 104 }
johnb 0:1b2f833ea9ba 105
johnb 0:1b2f833ea9ba 106 if( remoteXBee.getChannel( &chan ) )
johnb 0:1b2f833ea9ba 107 {
johnb 0:1b2f833ea9ba 108 pc.printf("Local channel is %d\r\n",chan);
johnb 0:1b2f833ea9ba 109 }
johnb 0:1b2f833ea9ba 110 else
johnb 0:1b2f833ea9ba 111 {
johnb 0:1b2f833ea9ba 112 pc.printf("Didn't manage to retrieve the local channel\r\n");
johnb 0:1b2f833ea9ba 113 }
johnb 0:1b2f833ea9ba 114 if( atIf.getSerialNumber( &sn ) )
johnb 0:1b2f833ea9ba 115 {
johnb 0:1b2f833ea9ba 116 pc.printf("Local serial number is %llu (decimal)\r\n",sn);
johnb 0:1b2f833ea9ba 117 }
johnb 0:1b2f833ea9ba 118 else
johnb 0:1b2f833ea9ba 119 {
johnb 0:1b2f833ea9ba 120 pc.printf("Didn't manage to retrieve the local serial number\r\n");
johnb 0:1b2f833ea9ba 121 }
johnb 0:1b2f833ea9ba 122 if( atIf.getPanId( &panId ) )
johnb 0:1b2f833ea9ba 123 {
johnb 0:1b2f833ea9ba 124 pc.printf("Remote PAN ID is %d\r\n",panId);
johnb 0:1b2f833ea9ba 125 }
johnb 0:1b2f833ea9ba 126 else
johnb 0:1b2f833ea9ba 127 {
johnb 0:1b2f833ea9ba 128 pc.printf("Didn't manage to retrieve the local PAN Id\r\n");
johnb 0:1b2f833ea9ba 129 }
johnb 0:1b2f833ea9ba 130 if( atIf.getChannel( &chan ) )
johnb 0:1b2f833ea9ba 131 {
johnb 0:1b2f833ea9ba 132 pc.printf("Local channel is %d\r\n",chan);
johnb 0:1b2f833ea9ba 133 }
johnb 0:1b2f833ea9ba 134 else
johnb 0:1b2f833ea9ba 135 {
johnb 0:1b2f833ea9ba 136 pc.printf("Didn't manage to retrieve the local channel\r\n");
johnb 0:1b2f833ea9ba 137 }
johnb 0:1b2f833ea9ba 138 }
johnb 0:1b2f833ea9ba 139 else
johnb 0:1b2f833ea9ba 140 {
johnb 0:1b2f833ea9ba 141 pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
johnb 0:1b2f833ea9ba 142 }
johnb 0:1b2f833ea9ba 143 }
johnb 0:1b2f833ea9ba 144 else
johnb 0:1b2f833ea9ba 145 {
johnb 0:1b2f833ea9ba 146 pc.printf("setUpApi failed with status %d\r\n",status);
johnb 0:1b2f833ea9ba 147 }
johnb 0:1b2f833ea9ba 148 }