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:
33:eccf4725930c
Parent:
29:c6d037cceb02
Child:
40:b96e8cad93d3
--- a/Base/XBeeDevice.cpp	Fri Feb 07 21:17:32 2014 +0000
+++ b/Base/XBeeDevice.cpp	Tue Mar 25 18:38:42 2014 +0000
@@ -39,20 +39,34 @@
 /** ASCII command to the XBee to request that it exit command mode */
 const char exit_cmd_mode_cmd[] = { 'A', 'T', 'C', 'N', '\r' };
 
-XBeeDevice::XBeeDevice( PinName p_tx, PinName p_rx, PinName p_rts, PinName p_cts ): m_model( XBeeDevice::XBEEDEVICE_S1 ), m_if( p_tx, p_rx )
+void XBeeDevice::init()                          
 {
-    m_escape = true;
+    m_model = XBeeDevice::XBEEDEVICE_S1;
     m_inAtCmdMode = false;
     m_rxMsgLastWasEsc = false;
+    m_escape = true;
+}
+
+XBeeDevice::XBeeDevice( PinName p_tx, PinName p_rx, PinName p_rts, PinName p_cts ):  m_serialNeedsDelete( true )
+{    
+    init();
     
+    m_if = new Serial( p_tx, p_rx ), 
+
     /* Can only do flow control on devices which support it */
 #if defined ( DEVICE_SERIAL_FC )
     /* TODO: need rts and cts both set? */
-    m_if.set_flow_control( mbed::SerialBase::Flow.RTSCTS, p_rts, p_cts );
+    m_if->set_flow_control( mbed::SerialBase::Flow.RTSCTS, p_rts, p_cts );
 #endif
 
     /* Attach RX call-back to the serial interface */
-    m_if.attach( this, &XBeeDevice::if_rx, Serial::RxIrq); 
+    m_if->attach( this, &XBeeDevice::if_rx, Serial::RxIrq); 
+}
+
+XBeeDevice::XBeeDevice( Serial* p_serialIf ): m_if( p_serialIf ),
+                                              m_serialNeedsDelete( true )
+{    
+    init();
 }
 
 XBeeDevice::~XBeeDevice( void )
@@ -63,6 +77,10 @@
          ++it ) {
         (*it)->unregisterCallback();
     }
+    if( m_serialNeedsDelete )
+    {
+        delete( m_if );
+    }
 }
 
 XBeeDevice::XBeeDeviceModel_t XBeeDevice::getXBeeModel() const
@@ -78,9 +96,9 @@
 void XBeeDevice::if_rx( void )
 {
     /* Keep going while there are bytes to be read */
-    while(m_if.readable()) {
+    while(m_if->readable()) {
         
-        uint8_t c = m_if.getc();
+        uint8_t c = m_if->getc();
         
         /* Sanity check that if we're starting from an empty buffer the byte that we're
            receiving is a frame delimiter */
@@ -245,7 +263,7 @@
     /* Checksum is 0xFF - summation of bytes (excluding delimiter and length) */
     xbeeWrite( (uint8_t)0xFFU - sum );
     
-    fflush( m_if );
+    fflush( *m_if );
 #if defined XBEE_DEBUG_DEVICE_DUMP_MESSAGE_DECODE
     m_if.printf("\r\n");
 #endif
@@ -266,19 +284,19 @@
          (p_byte == XBEE_SB_XOFF))) 
     {
 #if defined XBEE_DEBUG_DEVICE_DUMP_MESSAGE_DECODE
-        m_if.printf("%02x ",XBEE_SB_ESCAPE);
-        m_if.printf("%02x ",p_byte ^ 0x20);
+        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);
+        m_if->putc(XBEE_SB_ESCAPE);
+        m_if->putc(p_byte ^ 0x20);
 #endif
         c_sum += XBEE_SB_ESCAPE;
         c_sum += p_byte ^ 0x20;
     } else {
 #if defined XBEE_DEBUG_DEVICE_DUMP_MESSAGE_DECODE
-        m_if.printf("%02x ",p_byte);
+        m_if->printf("%02x ",p_byte);
 #else
-        m_if.putc(p_byte);
+        m_if->putc(p_byte);
 #endif
         c_sum += p_byte;
     }
@@ -300,10 +318,10 @@
         for( size_t i = 0;
              i < p_len;
              i++ ) {
-            m_if.putc(p_dat[i]);
+            m_if->putc(p_dat[i]);
         }
         
-        fflush( m_if );
+        fflush( *m_if );
                 
         wait_ms( p_wait_ms );