API for communicating with XBee devices.

Dependencies:   CircularBuffer FixedLengthList

Dependents:   XBeeApiTest XBeeApiSimpleATCmdsExample XBeeApiBroadcastExample XBeeApiBroadcastExampleRTOS ... more

Overview

XBeeApi is intended to be a library for providing a high-level API interface to the XBee - for example getChannel() and setChannel(2) methods rather than needing to send( "ATCH" ) and send( "ATCH 2" ) - and then de-code the responses.

See the notebook page here for a description of how the API works & some details on the various classes.

Features:

  • Support for transmission & reception of data packets
  • Support for reading & changing settings
  • Support for "Remote AT" interface to access settings & I/O channels on remote XBees
  • XBeeApi should work if you're using mbed-rtos, though it is not currently threadsafe. Take a look at the XBeeApiBroadcastExampleRTOS example if you're including mbed-rtos.

Example Programs

There are also example programs available:

Transmit

Import programXBeeApiSimpleBroadcastExample

Simple example of how to use XBeeApi - set up the XBee, configure P2P networking then transmit a frame.

Import programXBeeApiBroadcastExample

Example for XBeeAPI; a little more involved than XBeeApiSimpleBroadcastExample with report on failure to set up the XBee and on the transmit status of the message.

Import programXBeeApiBroadcastExampleRTOS

Example of using the XBeeApi library to broadcast a message, based on XBeeApiBroadcastExample. This example shows how to use the library when using mbed-rtos. Before compiling you must open "XbeeApi\Config\XBeeApiCfg.hpp" and change the '#if 0' to '#if 1' on the line above the comment reading "Use RTOS features to make XBeeApi threadsafe"

Settings/Status

Import programXBeeApiSimpleATCmdsExample

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

Import programXBeeApiRemoteATCmdsExample

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

Receive

Import programXBeeApiSimpleReceiveExample

Simple example of using XBeeApi to receive data packets via wireless

Import programXBeeApiReceiveCallbackExample

Example of using the XBeeApi library to receive a message via a callback method

Import programXBeeApiReceiveCallbackExampleRTOS

Example of using the XBeeApi library to receive a message via a callback method. This example shows how to use the library when using mbed-rtos. See the comment at the top of main.cpp

Remote I/O

Import programXBeeApiRemoteIOExample

Example of using the XBeeApi library to read inputs on a remote XBee

If you have 2 mbed connected XBees available then you can use XBeeApiSimpleReceiveExample and XBeeApiSimpleBroadcastExample as a pair.

Note that this is still a work in progress! XBeeApiTodoList tracks some of the functionality still to be added.

Files at this revision

API Documentation at this revision

Comitter:
johnb
Date:
Sat Jan 18 19:44:01 2014 +0000
Parent:
0:9f44c388ca00
Child:
2:7196461129e9
Commit message:
Work-in-progress commit : Can get XBee into AT command mode, configure API mode 2, exit command code and successfully perform API command.

Changed in this revision

XBeeApiCmdAt.cpp Show annotated file Show diff for this revision Revisions of this file
XBeeDevice.cpp Show annotated file Show diff for this revision Revisions of this file
XBeeDevice.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/XBeeApiCmdAt.cpp	Sat Jan 18 18:12:12 2014 +0000
+++ b/XBeeApiCmdAt.cpp	Sat Jan 18 19:44:01 2014 +0000
@@ -18,11 +18,14 @@
 
 #include "XBeeApiCmdAt.hpp"
 
-const uint8_t cmd_vr[] = { 77, 'V', 'R' }; 
+#define VR_CMD_LEN (3U)
+
+const uint8_t cmd_vr[ VR_CMD_LEN ] = { 'V', 'V', 'R' }; 
 
 XBeeApiCmdAt::XBeeApiCmdAt( void ) : XBeeApiCmd( )
 {
+     m_cmdId = XBEE_CMD_AT_CMD;
      m_data = cmd_vr;
-     m_dataLen = sizeof( cmd_vr );
+     m_dataLen = VR_CMD_LEN ;
 }
 
--- a/XBeeDevice.cpp	Sat Jan 18 18:12:12 2014 +0000
+++ b/XBeeDevice.cpp	Sat Jan 18 19:44:01 2014 +0000
@@ -46,6 +46,8 @@
 {
     uint8_t sum = 0U;
     uint16_t len;
+    uint16_t i;
+    const uint8_t* cmdData;
  
 #if defined  XBEEAPI_CONFIG_USING_RTOS
     m_ifMutex.lock();
@@ -58,10 +60,22 @@
     xbeeWrite((uint8_t)(len & 0xFF));
 
     xbeeWrite((uint8_t)p_cmd->getCmdId());
+    sum += p_cmd->getCmdId();
+
+    cmdData = p_cmd->getDataPtr();
+
+    for( i = 0;
+         i < (len-1);
+         i++ )
+    {
+        xbeeWrite(cmdData[i]);
+        sum += cmdData[i];
+    }
     
     /* Checksum is 0xFF - summation of bytes (excluding delimiter and length) */
     xbeeWrite( (uint8_t)0xFFU - sum );
-    /* @TODO */
+    
+    fflush( m_if );
     
 #if defined  XBEEAPI_CONFIG_USING_RTOS
     m_ifMutex.unlock();
@@ -76,43 +90,85 @@
          (p_byte == XBEE_SB_XON ) || 
          (p_byte == XBEE_SB_XOFF))) 
     {
+        #if 0
+        m_if.printf("%02x ",XBEE_SB_ESCAPE);
+        m_if.printf("%02x ",p_byte ^ 0x20);
+        #else
         m_if.putc(XBEE_SB_ESCAPE);
         m_if.putc(p_byte ^ 0x20);
+        #endif
     } else {
+        #if 0
+        m_if.printf("%02x ",p_byte);
+        #else
         m_if.putc(p_byte);
+        #endif
     }
 }
 
-bool XBeeDevice::setUpApi( void )
+#define IS_OK( _b ) (( _b[ 0 ] == 'O' ) && ( _b[ 1 ] == 'K' ) && ( _b[ 2 ] == '\r' ))
+#define OK_LEN (3U)
+
+XBeeDevice::XBeeDeviceReturn_t XBeeDevice::SendCmd( const char* const p_dat, size_t p_len, int p_wait_ms )
 {
-    bool ret_val = false;
+    XBeeDeviceReturn_t ret_val;
+
+    for( size_t i = 0;
+         i < p_len;
+         i++ ) {
+        m_if.putc(p_dat[i]);
+    }
     
-    m_if.printf("+++");
     fflush( m_if );
-
-    wait( 5 );
-    
-    if( m_rxBuff.getSize() == 3 )
+            
+    wait_ms( p_wait_ms );
+            
+    /* Check the response for the OK indicator */
+    if( m_rxBuff.getSize() == OK_LEN )
     {
-        uint8_t ok_buff[4];
-        m_rxBuff.read( ok_buff, 3 );
-        ok_buff[3] = 0;
-        
-        if(( ok_buff[ 0 ] == 'O' ) &&
-           ( ok_buff[ 1 ] == 'K' ) &&
-           ( ok_buff[ 2 ] == '\r' ))
-           
+        uint8_t ok_buff[OK_LEN];
+        m_rxBuff.read( ok_buff, OK_LEN );
+                
+        if( IS_OK( ok_buff ))
+        {
+            ret_val = XBEEDEVICE_OK;
+        } 
+        else 
         {
-            ret_val = true;
-            m_if.printf("ATVR\r");
-            fflush( m_if );
+            ret_val = XBEEDEVICE_UNEXPECTED_DATA;                    
         }
     }
+    else
+    {
+        ret_val = XBEEDEVICE_UNEXPECTED_LENGTH;
+    }
+    return ret_val;
+}
+
+XBeeDevice::XBeeDeviceReturn_t XBeeDevice::setUpApi( void )
+{
+    XBeeDeviceReturn_t ret_val;
+    
+    /* Request to enter command mode */
+    ret_val = SendCmd("+++", 3, 5000);
+
+    /* Everything OK with last request? */
+    if( ret_val == XBEEDEVICE_OK )
+    {
+        /* API mode 2 please! */
+        ret_val = SendCmd("ATAP 2\r",7);
+    }
+
+    /* Everything OK with last request? */
+    if( ret_val == XBEEDEVICE_OK )
+    {
+        /* Exit command mode, back to API mode */
+        ret_val = SendCmd("ATCN\r",5);
+    }
     
     return ret_val;
 }
 
-
 #if defined XBEEAPI_CONFIG_ENABLE_DEVELOPER
 
 void XBeeDevice::dumpRxBuffer( Stream* p_buf, const bool p_hexView )
--- a/XBeeDevice.hpp	Sat Jan 18 18:12:12 2014 +0000
+++ b/XBeeDevice.hpp	Sat Jan 18 19:44:01 2014 +0000
@@ -73,6 +73,13 @@
      CircularBuffer<XBEEAPI_CONFIG_RX_BUFFER_SIZE> m_rxBuff;
      
    public:
+     typedef enum {
+         XBEEDEVICE_OK                = 0,
+         XBEEDEVICE_TIMEOUT           = 1,
+         XBEEDEVICE_UNEXPECTED_LENGTH = 2,
+         XBEEDEVICE_UNEXPECTED_DATA   = 3
+     } XBeeDeviceReturn_t;
+   
      /**
          @param p_tx Serial interface TX pin
          @param p_rx Serial interface RX pin
@@ -83,7 +90,9 @@
      
      void SendCmd( const XBeeApiCmd* const p_cmd );
      
-     bool setUpApi( void );
+     XBeeDeviceReturn_t setUpApi( void );
+     
+     XBeeDeviceReturn_t SendCmd( const char* const p_dat, size_t p_len, int p_wait_ms = 1000 );
      
 #if defined XBEEAPI_CONFIG_ENABLE_DEVELOPER
      void dumpRxBuffer( Stream* p_buf, const bool p_hexView );