Test / example for the XBeeApi library

Dependencies:   XBeeApi mbed

Example program employing the XBeeApi library to configure and communicate with an XBee. See the notebook page here

Committer:
johnb
Date:
Fri Jan 31 01:18:47 2014 +0000
Revision:
1:3de4723efe9e
Parent:
0:132f26d3350b
Quick test of XBeeApi - needs additional structure/function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnb 0:132f26d3350b 1 #include "mbed.h"
johnb 0:132f26d3350b 2 #include "XBeeDevice.hpp"
johnb 0:132f26d3350b 3 #include "XBeeApiCmdAt.hpp"
johnb 0:132f26d3350b 4
johnb 1:3de4723efe9e 5 /* Use the blocking API ... */
johnb 1:3de4723efe9e 6 #define USE_BLOCKING
johnb 1:3de4723efe9e 7
johnb 0:132f26d3350b 8 Serial pc(USBTX, USBRX); // tx, rx
johnb 0:132f26d3350b 9
johnb 1:3de4723efe9e 10 XBeeDevice xbeeDevice( PTA2, PTA1, NC, NC );
johnb 0:132f26d3350b 11
johnb 0:132f26d3350b 12 int main() {
johnb 1:3de4723efe9e 13 #if defined USE_BLOCKING
johnb 1:3de4723efe9e 14 XBeeApiCmdAtBlocking atIf;
johnb 1:3de4723efe9e 15 #else
johnb 1:3de4723efe9e 16 XBeeApiCmdAt atIf;
johnb 1:3de4723efe9e 17 #endif
johnb 0:132f26d3350b 18 XBeeDevice::XBeeDeviceReturn_t status;
johnb 0:132f26d3350b 19
johnb 1:3de4723efe9e 20 if( ! ( xbeeDevice.registerDecoder( &atIf ) ))
johnb 1:3de4723efe9e 21 {
johnb 1:3de4723efe9e 22 /* TODO: Oh dear */
johnb 1:3de4723efe9e 23 }
johnb 1:3de4723efe9e 24
johnb 0:132f26d3350b 25 /* Get API mode 2 set up */
johnb 0:132f26d3350b 26 status = xbeeDevice.setUpApi();
johnb 0:132f26d3350b 27
johnb 0:132f26d3350b 28 if( status != XBeeDevice::XBEEDEVICE_OK )
johnb 0:132f26d3350b 29 {
johnb 0:132f26d3350b 30 pc.printf("\r\n[%4d]: XBeeStatus: %d\r\n",__LINE__,status);
johnb 0:132f26d3350b 31 }
johnb 1:3de4723efe9e 32 #if !defined USE_BLOCKING
johnb 0:132f26d3350b 33 else
johnb 0:132f26d3350b 34 {
johnb 1:3de4723efe9e 35 /* If we got the API set up should be OK to send an API command
johnb 1:3de4723efe9e 36 Request the Firmware and Hardware versions from the XBee */
johnb 1:3de4723efe9e 37 atIf.requestFirmwareVersion();
johnb 1:3de4723efe9e 38 atIf.requestHardwareVersion();
johnb 1:3de4723efe9e 39 atIf.requestChannel();
johnb 0:132f26d3350b 40 }
johnb 1:3de4723efe9e 41 #endif
johnb 0:132f26d3350b 42
johnb 0:132f26d3350b 43 while( 1 ) {
johnb 1:3de4723efe9e 44 uint16_t hwVer, fwVer;
johnb 1:3de4723efe9e 45 uint8_t channel;
johnb 1:3de4723efe9e 46
johnb 0:132f26d3350b 47 wait(1);
johnb 1:3de4723efe9e 48
johnb 1:3de4723efe9e 49 /* If we've now retrieved the HW and FW versions, proudly tell the
johnb 1:3de4723efe9e 50 serial port */
johnb 1:3de4723efe9e 51
johnb 1:3de4723efe9e 52 if( atIf.getHardwareVersion( &hwVer ) )
johnb 1:3de4723efe9e 53 {
johnb 1:3de4723efe9e 54 printf("HW VER: 0x%04x\r\n", hwVer);
johnb 1:3de4723efe9e 55 }
johnb 1:3de4723efe9e 56 if( atIf.getFirmwareVersion( &fwVer ) )
johnb 1:3de4723efe9e 57 {
johnb 1:3de4723efe9e 58 printf("FW VER: 0x%04x\r\n", fwVer);
johnb 1:3de4723efe9e 59 }
johnb 1:3de4723efe9e 60 if( atIf.getChannel( &channel ) )
johnb 1:3de4723efe9e 61 {
johnb 1:3de4723efe9e 62 printf("Channel: 0x%02x\r\n", channel);
johnb 1:3de4723efe9e 63 if( channel != 0x0d )
johnb 1:3de4723efe9e 64 {
johnb 1:3de4723efe9e 65 atIf.setChannel( 0x0d );
johnb 1:3de4723efe9e 66 }
johnb 1:3de4723efe9e 67 }
johnb 1:3de4723efe9e 68
johnb 1:3de4723efe9e 69 /* Any spare (i.e. unprocessed) bytes in the rx buffer to be
johnb 1:3de4723efe9e 70 worried about? */
johnb 0:132f26d3350b 71 xbeeDevice.dumpRxBuffer( &pc, true );
johnb 0:132f26d3350b 72 }
johnb 1:3de4723efe9e 73
johnb 1:3de4723efe9e 74 #if 0
johnb 0:132f26d3350b 75 pc.printf("\r\n[%4d]: Done\r\n",__LINE__);
johnb 1:3de4723efe9e 76 #endif
johnb 0:132f26d3350b 77 }