Sample program for XBee remote API operation. This program get ADC sample data from remote XBee connected over the radio

Dependencies:   TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
todotani
Date:
Sun Dec 12 04:45:05 2010 +0000
Child:
1:3b74123e6d7a
Commit message:

Changed in this revision

TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
XBee.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Sun Dec 12 04:45:05 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#e4cb7ddee0d3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBee.lib	Sun Dec 12 04:45:05 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okini3939/code/XBee/#547cfff7adf7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Dec 12 04:45:05 2010 +0000
@@ -0,0 +1,134 @@
+#include "mbed.h"
+#include "XBee.h"
+#include "TextLCD.h"
+
+TextLCD lcd(p25, p26, p24, p23, p22, p21); // RS, E, DB4, DB5, DB6, DB7
+
+/*-- AT command and parameters --*/
+uint8_t atISCmd[] = {'I', 'S'};      // Forces a read of all enabled digital and analog input lines
+uint8_t atDBCmd[] = {'D', 'B'};      // Received Signal Strength
+uint8_t cmdVal0[] = {0};             // Clear RSSI regisger
+
+/*-- Create instanse of Xbee object --*/
+XBee xbee(p13, p14);
+XBeeAddress64 remoteAddress(0x0013A200, 0x406B7111);    // Specify your XBee address
+
+/*-- Create instanse of Command and Response object --*/
+// Remot ATIS command to read ADC value (ADC3 is enabled by X-CTU tool)
+RemoteAtCommandRequest remoteSampleRequest(remoteAddress, atISCmd);
+// Local ATDB command to read signal strength (RSSI)
+AtCommandRequest atDB(atDBCmd);
+// Local ATDB0 command to clear RSSI
+AtCommandRequest atDB0(atDBCmd, cmdVal0, sizeof(cmdVal0));
+// Create instanse to handle command response
+AtCommandResponse response = AtCommandResponse();
+RemoteAtCommandResponse remoteResp = RemoteAtCommandResponse();
+
+
+/* Receive command response packet
+ * If OK response recieved, return pointer to the Response Data Frame
+ */
+uint8_t* GetResponse() {
+    // Read response
+    if (xbee.readPacket(5000)) {
+        // Got a response! Check if response is AT command respose
+        if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
+            xbee.getResponse().getAtCommandResponse(response);
+            if ( response.getStatus() == AT_OK ) 
+                return response.getValue();
+        } else  if (xbee.getResponse().getApiId() == REMOTE_AT_COMMAND_RESPONSE) {
+            xbee.getResponse().getRemoteAtCommandResponse(remoteResp);
+            if ( remoteResp.getStatus() == AT_OK ) {
+               // Debug print
+                printf("Response Data:");
+                   for (int i = 0; i < remoteResp.getValueLength(); i++)
+                   printf("%02X ", remoteResp.getValue()[i]);
+                printf("\n");
+                
+                return remoteResp.getValue();
+            } else {
+                printf("Remote Command Error:0x%X\n", response.getStatus());
+            }
+        }
+    }
+
+    return 0;
+}
+
+
+/* Get ADC data
+ * Data frame structure of ATIS
+ * Offset
+ *   0   : Number of Samples (Always 1)
+ *   1-2 : Digital Channel Mask
+ *   3   : Analog Channel Mask
+ *   4-5 : Digital Samples (Omit if no DIO enabled)
+ *   6-7 : First ADC Data
+ */
+uint16_t getAnalog(uint8_t *FrameData, int ADC) {
+    // ADC data feild starts 4 bytes offest, if no DIO enabled
+    uint8_t start = 4;
+
+    // Contains Digital channel?
+    if (FrameData[1] > 0 || FrameData[2]) {
+        // make room for digital i/o
+        start+=2;
+    }
+
+    // start depends on how many ADCs before this ADC are enabled
+    for (int i = 0; i < ADC; i++) {
+        // Is Analog channel Enabled ?
+        if ( (FrameData[3] >> i) & 1 ) {
+            start+=2;
+        }
+    }
+
+    return (uint16_t)((FrameData[start] << 8) + FrameData[start + 1]);
+}
+
+
+int main() {
+    unsigned int loop = 0;
+
+    xbee.begin(9600);
+    lcd.printf("RSSI:");
+    lcd.locate(0, 1);
+    lcd.printf("ADC :");
+    printf("\nStart.\n");
+
+    while (true) {
+        uint8_t *responseVal;
+        uint8_t rssiVal = 0;
+        uint16_t adcVal = 0;
+
+        // Send ATDB command (Read RSSI register from local Xbee)
+        xbee.send(atDB);
+        responseVal = GetResponse();
+        if ( responseVal != 0 )
+            rssiVal = responseVal[0];
+        lcd.locate(5, 0);
+        if (rssiVal == 0)
+            lcd.printf("No Signal");
+        else
+            lcd.printf("-%ddBm   ", rssiVal);
+
+        // Clear RSSI register, because Xbee hold RSSI value of last received packet even after radio disconneded
+        xbee.send(atDB0);
+        GetResponse();
+
+        // Read ADC3 value by sending ATIS command
+        xbee.send(remoteSampleRequest);
+        responseVal = GetResponse();
+        if ( responseVal != 0 ) {
+            adcVal = getAnalog(responseVal, 3); // Assume ADC3 is enabled
+        }
+        lcd.locate(5, 1);
+        if (adcVal == 0)
+            lcd.printf("-     ");
+        else
+            lcd.printf("%d", adcVal);
+    
+        printf("Loop:%d\n", loop++);
+        wait(1.0);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Dec 12 04:45:05 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e