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

Dependencies:   mbed XBee EthernetNetIf

main.cpp

Committer:
okini3939
Date:
2012-03-08
Revision:
0:a566d771935a
Child:
1:27bf8d24244b

File content as of revision 0:a566d771935a:

#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());
        }
    }

}