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

Dependencies:   mbed XBee EthernetNetIf

Revision:
1:27bf8d24244b
Parent:
0:a566d771935a
--- a/main.cpp	Thu Mar 08 17:45:00 2012 +0000
+++ b/main.cpp	Tue Mar 13 09:14:31 2012 +0000
@@ -1,33 +1,46 @@
 #include "mbed.h"
 #include "XBeeWiFi.h"
+#include "EthernetNetIf.h"
+#include "TCPSocket.h"
 
-/*
+
 #define SECURITY SECURITY_WPA2
 #define SSID "XBEEWIFI"
 #define PASSPHRASE "PASSWORD"
-*/
+/*
 #define SECURITY SECURITY_OPEN
 #define SSID "XBEEWIFI"
 #define PASSPHRASE ""
+*/
+#define HTTP_PORT 80
+#define HTTP_SRC_PORT 10080
+#define HTTP_TIMEOUT 5000 // ms
+#define METHOD_GET 0
+#define METHOD_POST 1
 
 DigitalOut myled(LED1);
 Serial pc(USBTX, USBRX);
 
-XBeeWiFi xbee(p13, p14, p21);
-
+XBeeWiFi xbee(p13, p14, p12, P0_22); // TX, RX, CTS, RTS
+//XBeeWiFi xbee(p13, p14, p12, p11); // TX, RX, CTS, RTS
 
 int init_wifi (int timeout) {
     int i, r;
 
-    pc.printf("reset %d\r\n");
+    pc.printf("reset\r\n");
     r = xbee.reset();
-    if (r) {
-        pc.printf("error %x\r\n", r);
+    if (r < 0) {
+        pc.printf("error reset %d\r\n", r);
         return -1;
     }
+
     xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000);    
 
-    xbee.setup(SECURITY, SSID, PASSPHRASE);
+    r = xbee.setup(SECURITY, SSID, PASSPHRASE);
+    if (r < 0) {
+        pc.printf("error setup %d\r\n", r);
+        return -1;
+    }
 
     for (i = 0; i < timeout; i ++) {
         wait(1);
@@ -67,41 +80,136 @@
 }
 
 
+int httpRequest (int method, Host *host, char *uri, char *head, char *body) {
+    Timer timeout;
+    char buf[1500], tmp[40];
+    int r, len;
+    IPv4TransmitRequest httpRequest;
+    AtCommandRequest atRequest;
+    AtCommandResponse atResponse;
+    IPV4RxFrame httpResponse;
+    IpAddr ipaddr;
+
+    // connect
+    if (host->getIp().isNull()) {
+        return -1;
+    }
+    if (! host->getPort()) {
+        host->setPort(HTTP_PORT);
+    }
+
+    // create request
+    if (method == METHOD_POST) {
+        strcpy(buf, "POST ");
+    } else {
+        strcpy(buf, "GET ");
+    }
+    strcat(buf, uri);
+    strcat(buf, " HTTP/1.1\r\nHost: ");
+    strcat(buf, host->getName());
+    strcat(buf, "\r\n");
+    strcat(buf, "Connection: close\r\n");
+    if (head) {
+        strcat(buf, head);
+    }
+    if (method == METHOD_POST) {
+        sprintf(tmp, "Content-Length: %d\r\n", strlen(body));
+        strcat(buf, tmp);
+    }
+    strcat(buf, "\r\n");
+
+    // send HTTP request
+    len = strlen(buf);
+    ipaddr = host->getIp();
+    httpRequest.setAddress(ipaddr);
+    httpRequest.setDstPort(host->getPort());
+    httpRequest.setSrcPort(HTTP_SRC_PORT);
+    httpRequest.setProtocol(PROTOCOL_TCP);
+    httpRequest.setPayload((uint8_t*)buf);
+    httpRequest.setPayloadLength(len);
+    httpRequest.setFrameId(xbee.getNextFrameId());
+    xbee.send(httpRequest);
+    r = xbee.getWiResponse(TX_STATUS_RESPONSE, httpRequest.getFrameId());
+    pc.printf("wifi TX: %d\r\n", r);
+    if (r < 0) return -1;
+
+    // wait responce
+    timeout.reset();
+    timeout.start();
+    while (timeout.read_ms() < HTTP_TIMEOUT) {
+        // recv HTTP request
+        r = xbee.getWiResponse(IPv4_RX_FRAME, 0, 3000);
+        pc.printf("wifi RX: %d\r\n", r);
+        if (r >= 0) {
+            timeout.reset();
+            xbee.getResponse().getAtCommandResponse(httpResponse);
+            pc.printf("\r\n--- recv %d ---\r\n", httpResponse.getDataLength());
+            strncpy(buf, (char*)httpResponse.getData(), httpResponse.getDataLength());
+            buf[httpResponse.getDataLength()] = 0;
+            pc.printf(buf);
+        }
+    }
+    timeout.stop();
+    return 0;
+}
+
 int main() {
     int i;
     IpAddr ipaddr, netmask, gateway, nameserver;
+    Host host;
 
     xbee.begin(115200);
+//    xbee.baud(921000);
     pc.baud(115200);
     pc.printf("XBee WiFi test\r\n");
 
-    if (init_wifi(10)) {
+    if (init_wifi(20)) {
         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]);
+    if (ipaddr == 0) {
+        pc.printf("not configure\r\n");
+        return -1;
+    }
 
+/*
+    // DNS lookup 
+    // *** Note: wifi is turned off when XBee send/recv the port 53 udp packet.
+    //           XBee wifi --> request --> DNS server (wifi OK)
+    //           DNS server --> responce --> XBee wifi (wifi turned off)
+    //           why ??
     nameserver = gateway;
-//    nameserver = IpAddr(202,232,2,35);
+//    nameserver = IpAddr(12,34,56,78);
+    pc.printf("resolver %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]);
     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]);
     }
+*/
+    // HTTP request
+    wait(1);
+    host.setIp(IpAddr(217,140,96,42));
+    host.setName("mbed.org");
+    httpRequest(METHOD_GET, &host, "/", "", "");
 
+    // Serial through
     {
         Serial xbeeserial(p13, p14);
+        DigitalOut rts(p22);
+        
         xbeeserial.baud(115200);
+        rts = 0;
         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());
+            if (xbeeserial.readable()) pc.putc(xbeeserial.getc());
+//            if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc());
         }
     }