http://mbed.org/users/okini3939/notebook/xbee-mbed/

Dependencies:   mbed XBee EthernetNetIf

Revision:
0:a566d771935a
Child:
1:27bf8d24244b
diff -r 000000000000 -r a566d771935a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 08 17:45:00 2012 +0000
@@ -0,0 +1,108 @@
+#include "mbed.h"
+#include "XBeeWiFi.h"
+
+/*
+#define SECURITY SECURITY_WPA2
+#define SSID "XBEEWIFI"
+#define PASSPHRASE "PASSWORD"
+*/
+#define SECURITY SECURITY_OPEN
+#define SSID "XBEEWIFI"
+#define PASSPHRASE ""
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX);
+
+XBeeWiFi xbee(p13, p14, p21);
+
+
+int init_wifi (int timeout) {
+    int i, r;
+
+    pc.printf("reset %d\r\n");
+    r = xbee.reset();
+    if (r) {
+        pc.printf("error %x\r\n", r);
+        return -1;
+    }
+    xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000);    
+
+    xbee.setup(SECURITY, SSID, PASSPHRASE);
+
+    for (i = 0; i < timeout; i ++) {
+        wait(1);
+        r = xbee.getStatus();
+        pc.printf("status %02x: ", r);    
+        switch (r) {
+        case JOINED_AP:
+            pc.printf("Successfully joined an access point.\r\n");
+            return 0;
+        case INITIALIZATION:
+            pc.printf("WiFi initialization in progress.\r\n");
+            break;
+        case SSID_NOT_FOUND:
+            pc.printf("SSID not found.\r\n");
+            return -1;
+        case SSID_NOT_CONFIGURED:
+            pc.printf("SSID not configured.\r\n");
+            return -1;
+        case JOIN_FAILED:
+            pc.printf("SSID join failed.\r\n");
+            return -1;
+        case WAITING_IPADDRESS:
+            pc.printf("Waiting for IP configuration.\r\n");
+            break;
+        case WAITING_SOCKETS:
+            pc.printf("Listening sockets are being set up.\r\n");
+            break;
+        case SCANNING_SSID:
+            pc.printf("Currently scanning for SSID.\r\n");
+            break;
+        default:
+            pc.printf("\r\n");
+            break;
+        }
+    }
+    return -1;
+}
+
+
+int main() {
+    int i;
+    IpAddr ipaddr, netmask, gateway, nameserver;
+
+    xbee.begin(115200);
+    pc.baud(115200);
+    pc.printf("XBee WiFi test\r\n");
+
+    if (init_wifi(10)) {
+        pc.printf("XBee error\r\n");
+        return -1;
+    }
+
+    xbee.getAddress(ipaddr, netmask, gateway, nameserver);
+    pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
+
+    nameserver = gateway;
+//    nameserver = IpAddr(202,232,2,35);
+    xbee.setNameserver(nameserver);
+
+    i = xbee.getHostByName("mbed.org", ipaddr);
+    if (i) {
+        pc.printf("error resolv %d\r\n", i);
+    } else {
+        pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
+    }
+
+    {
+        Serial xbeeserial(p13, p14);
+        xbeeserial.baud(115200);
+        pc.printf("Serial through\r\n");
+        for (;;) {
+            if (pc.readable()) xbeeserial.putc(pc.getc());
+//            if (xbeeserial.readable()) pc.putc(xbeeserial.getc());
+            if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc());
+        }
+    }
+
+}