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.

Dependencies:   XBeeApi XBeeApiSimpleBroadcastExample mbed

Fork of XBeeApiSimpleBroadcastExample by John Bailey

Revision:
4:4c6644fd040b
Parent:
3:8fea787c2199
Child:
5:481ec3d3737f
diff -r 8fea787c2199 -r 4c6644fd040b main.cpp
--- a/main.cpp	Fri Mar 28 22:31:04 2014 +0000
+++ b/main.cpp	Fri Jun 27 19:18:39 2014 +0000
@@ -1,8 +1,9 @@
 /**
    @file
    @brief Example of using the XBeeApi library to broadcast a message
-          This example has a minimum of error checking in order
-          to keep the code compact
+          This example has more error checking than
+          XBeeApiSimpleBroadcast example and should improve the changes
+          of detecting and diagnosing a problem.
 
    @author John Bailey 
 
@@ -33,6 +34,10 @@
 #define XBEE_TX_PIN PTA2
 #define XBEE_RX_PIN PTA1
 
+#define WAIT_FOR_TX_TIME 500
+
+Serial pc(USBTX, USBRX); // tx, rx
+
 /* Network address for our XBee */
 const uint16_t myNetworkAddress = 0x1234;
 
@@ -57,8 +62,9 @@
 
     XBeeDevice::XBeeDeviceReturn_t status;
 
-    /* This is the frame we're going to transmit */
-    XBeeApiTxFrame frame( &xbeeDevice );
+    /* This is the frame we're going to transmit - using the extended version so that
+       stats are available */
+    XBeeApiTxFrameEx frame( &xbeeDevice );
         
     /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
        This step may not be needed in the case that the XBee has already been configured
@@ -76,6 +82,40 @@
         frame.setDataPtr( tx_data, sizeof( tx_data ) );
         frame.setDestAddr( frameDestinationAddress );
         
-        xbeeDevice.SendFrame( &frame );    
+        xbeeDevice.SendFrame( &frame );
+        
+        /* Arbitary wait for the TX to occurr.  We could poll getMostRecentStatus() 
+           instead of just waiting */
+        wait_ms( WAIT_FOR_TX_TIME );
+
+        pc.printf( "\r\nTX Status: ");
+        switch( frame.getMostRecentStatus() )
+        {
+            case XBeeApiTxFrame::XBEE_API_TX_STATUS_LAST:
+                /* Didn't receive a response from the XBee indicating
+                   the transmit status */    
+                pc.printf("No TX confirmation received from XBee");
+                break;
+            case XBeeApiTxFrame::XBEE_API_TX_STATUS_OK:
+                /* Frame was transmitted OK and at least one other network
+                   node acknowledged it */
+                pc.printf("OK");
+                break;
+            case XBeeApiTxFrame::XBEE_API_TX_STATUS_NO_ACK:
+                /* This means that the message was transmitted by 
+                   the XBee but no other nodes on the network acknowledged it.
+                   Could be other nodes are not on the same PAN, channel, etc) */
+                pc.printf("No acknowledgement received");
+                break;
+            default:
+                /* In this example we don't expect a response other than
+                   those explicitly handled above */
+                pc.printf("Unexpected response received");
+                break;
+        }
+    }
+    else
+    {
+        pc.printf("setUpApi failed with status %d\r\n",status);
     }
 }
\ No newline at end of file