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.

Revision:
52:0950b05d5270
Parent:
51:a7d0d2ef9261
Child:
53:7b65422d7a32
diff -r a7d0d2ef9261 -r 0950b05d5270 Utility/XBeeApiCmdAt.cpp
--- a/Utility/XBeeApiCmdAt.cpp	Mon Jul 28 10:24:16 2014 +0000
+++ b/Utility/XBeeApiCmdAt.cpp	Mon Jul 28 12:48:33 2014 +0000
@@ -252,11 +252,19 @@
     m_resetCount( 0 ),
     m_sampleCount( 0 )
 {
-    for( uint8_t i = 0;
+    uint8_t i;
+    for( i = 0;
          i < XBEE_API_DIO_CHANNEL_COUNT;
          i++ )
     {
         m_have_d[ i ] = false;
+        m_ioDigitalUpdatedTime[ i ] = 0;
+    }
+    for( i = 0;
+         i < XBEE_API_ADC_CHANNEL_COUNT;
+         i++ )
+    {
+        m_ioAnalogueUpdatedTime[ i ] = 0;
     }
 }
 
@@ -392,6 +400,65 @@
     return ret_val;
 }
 
+#define DIO_CHANNEL_MASK (0x01FFU)
+#define ADC_CHANNEL_MASK (0x7E00U)
+
+bool XBeeApiCmdAt::processIOFrame( const uint8_t* const p_data, size_t p_len, const size_t p_start )
+{
+#if 0
+    /* This is the number of sample sets that are contained in the packet, set using the IT AT command */
+    uint8_t sampleCount = p_data[ p_start ];
+#endif
+    
+    uint16_t channelMask = (((uint16_t)p_data[ p_start + 1 ]) << 8) |
+                           ((uint16_t)p_data[ p_start + 2 ]);
+    const uint8_t* dataPtr = &( p_data[ p_start + 3 ] );
+    const uint16_t dioMask = channelMask & DIO_CHANNEL_MASK;
+    uint16_t adcMask = (channelMask & ADC_CHANNEL_MASK) >> 9U;
+    uint8_t adc = 0;
+
+    time_t seconds = time( NULL );
+
+    if( dioMask )
+    {
+        uint16_t dioData = (((uint16_t)dataPtr[ 0 ]) << 8U) |
+                           (uint16_t)dataPtr[ 1 ];
+        dataPtr += 2;
+        
+        for( unsigned i = 0;
+             i < XBEE_API_DIO_CHANNEL_COUNT;
+             i++ )
+        {
+            if(( dioMask >> i ) & 0x01 )
+            {
+                m_ioDigitalState[i] = ( dioData >> i ) & 0x01;
+                m_ioDigitalUpdatedTime[i] = seconds;
+            }
+        }
+    }
+    
+    while( adcMask )
+    {
+        if( adcMask & 0x01 )
+        {
+            uint16_t adcData = (((uint16_t)dataPtr[ 0 ]) << 8U) |
+                               (uint16_t)dataPtr[ 1 ];
+            dataPtr += 2;
+            
+            m_ioAnalogueVal[adc] = adcData;
+            m_ioAnalogueUpdatedTime[adc] = seconds;
+        }
+        adcMask = adcMask >> 1U;
+        adc++;
+    }    
+    
+    return true;                       
+}
+
+#define XBEE_IO_PACKET_ADDRESS (XBEE_CMD_POSN_ID_SPECIFIC_DATA)
+#define XBEE_IO_PACKET_64BIT_DATA_START (XBEE_IO_PACKET_ADDRESS + sizeof( uint64_t) + 1U + 1U)
+#define XBEE_IO_PACKET_16BIT_DATA_START (XBEE_IO_PACKET_ADDRESS + sizeof( uint16_t) + 1U + 1U)
+
 bool XBeeApiCmdAt::decodeCallback( const uint8_t* const p_data, size_t p_len )
 {
     bool ret_val = false;
@@ -399,7 +466,39 @@
     if( XBEE_CMD_AT_RESPONSE == p_data[ XBEE_CMD_POSN_API_ID ] ) 
     {
         ret_val = processResponseFrame( p_data, p_len );
+    } 
+    else if( XBEE_CMD_RX_16B_IO == p_data[ XBEE_CMD_POSN_API_ID ] )
+    {
+        uint16_t src16BitAddr = (((uint16_t)p_data[ XBEE_IO_PACKET_ADDRESS ]) << 8U) | 
+                                p_data[ XBEE_IO_PACKET_ADDRESS + 1 ];
+        if( m_have_sourceAddress &&
+           ( src16BitAddr == m_sourceAddress ))
+        {
+            ret_val = processIOFrame( p_data, p_len, XBEE_IO_PACKET_16BIT_DATA_START );
+        }
     }
+    else if( XBEE_CMD_RX_64B_IO == p_data[ XBEE_CMD_POSN_API_ID ] )
+    {
+        uint32_t srcAddrHigh = (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS ]) << 24U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 1 ]) << 16U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 2 ]) << 8U) |
+                               (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 3 ]));
+        uint32_t srcAddrLow =  (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 4 ]) << 24U) |
+                                (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 5 ]) << 16U) |
+                                (((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 6 ]) << 8U) |
+                                ((uint64_t)p_data[ XBEE_IO_PACKET_ADDRESS + 7 ]);
+        if( m_have_snLow &&
+            m_have_snHigh &&
+           ( srcAddrHigh == m_snHigh ) &&
+           ( srcAddrLow  == m_snLow ))
+        {
+            ret_val = processIOFrame( p_data, p_len, XBEE_IO_PACKET_64BIT_DATA_START );
+        }
+    }
+    else
+    {
+    }
+
     return ret_val;
 }
 
@@ -504,6 +603,27 @@
     return ret_val;
 }
 
+time_t XBeeApiCmdAt::getDigitalState( const uint8_t p_chanNo, bool& p_state )
+{
+    time_t ret_val = 0;
+    if( p_chanNo < XBEE_API_DIO_CHANNEL_COUNT )
+    {
+        p_state = m_ioDigitalState[ p_chanNo ];
+        ret_val = m_ioDigitalUpdatedTime[ p_chanNo ];
+    }    
+    return ret_val;
+}
+
+time_t XBeeApiCmdAt::getAnalogueValue( const uint8_t p_chanNo, uint16_t& p_val )
+{
+    time_t ret_val = 0;
+    if( p_chanNo < XBEE_API_ADC_CHANNEL_COUNT )
+    {
+        p_val = m_ioAnalogueVal[ p_chanNo ];
+        ret_val = m_ioAnalogueUpdatedTime[ p_chanNo ];
+    }        
+    return ret_val;
+}
 
 #define MAKE_GET(_name, _mnemonic, _type ) \
 bool XBeeApiCmdAt::get ## _name( _type* const p_param ) \