http://mbed.org/users/okini3939/notebook/xbee-mbed/
Dependencies: mbed XBee EthernetNetIf
main.cpp@0:a566d771935a, 2012-03-08 (annotated)
- Committer:
- okini3939
- Date:
- Thu Mar 08 17:45:00 2012 +0000
- Revision:
- 0:a566d771935a
- Child:
- 1:27bf8d24244b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:a566d771935a | 1 | #include "mbed.h" |
okini3939 | 0:a566d771935a | 2 | #include "XBeeWiFi.h" |
okini3939 | 0:a566d771935a | 3 | |
okini3939 | 0:a566d771935a | 4 | /* |
okini3939 | 0:a566d771935a | 5 | #define SECURITY SECURITY_WPA2 |
okini3939 | 0:a566d771935a | 6 | #define SSID "XBEEWIFI" |
okini3939 | 0:a566d771935a | 7 | #define PASSPHRASE "PASSWORD" |
okini3939 | 0:a566d771935a | 8 | */ |
okini3939 | 0:a566d771935a | 9 | #define SECURITY SECURITY_OPEN |
okini3939 | 0:a566d771935a | 10 | #define SSID "XBEEWIFI" |
okini3939 | 0:a566d771935a | 11 | #define PASSPHRASE "" |
okini3939 | 0:a566d771935a | 12 | |
okini3939 | 0:a566d771935a | 13 | DigitalOut myled(LED1); |
okini3939 | 0:a566d771935a | 14 | Serial pc(USBTX, USBRX); |
okini3939 | 0:a566d771935a | 15 | |
okini3939 | 0:a566d771935a | 16 | XBeeWiFi xbee(p13, p14, p21); |
okini3939 | 0:a566d771935a | 17 | |
okini3939 | 0:a566d771935a | 18 | |
okini3939 | 0:a566d771935a | 19 | int init_wifi (int timeout) { |
okini3939 | 0:a566d771935a | 20 | int i, r; |
okini3939 | 0:a566d771935a | 21 | |
okini3939 | 0:a566d771935a | 22 | pc.printf("reset %d\r\n"); |
okini3939 | 0:a566d771935a | 23 | r = xbee.reset(); |
okini3939 | 0:a566d771935a | 24 | if (r) { |
okini3939 | 0:a566d771935a | 25 | pc.printf("error %x\r\n", r); |
okini3939 | 0:a566d771935a | 26 | return -1; |
okini3939 | 0:a566d771935a | 27 | } |
okini3939 | 0:a566d771935a | 28 | xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); |
okini3939 | 0:a566d771935a | 29 | |
okini3939 | 0:a566d771935a | 30 | xbee.setup(SECURITY, SSID, PASSPHRASE); |
okini3939 | 0:a566d771935a | 31 | |
okini3939 | 0:a566d771935a | 32 | for (i = 0; i < timeout; i ++) { |
okini3939 | 0:a566d771935a | 33 | wait(1); |
okini3939 | 0:a566d771935a | 34 | r = xbee.getStatus(); |
okini3939 | 0:a566d771935a | 35 | pc.printf("status %02x: ", r); |
okini3939 | 0:a566d771935a | 36 | switch (r) { |
okini3939 | 0:a566d771935a | 37 | case JOINED_AP: |
okini3939 | 0:a566d771935a | 38 | pc.printf("Successfully joined an access point.\r\n"); |
okini3939 | 0:a566d771935a | 39 | return 0; |
okini3939 | 0:a566d771935a | 40 | case INITIALIZATION: |
okini3939 | 0:a566d771935a | 41 | pc.printf("WiFi initialization in progress.\r\n"); |
okini3939 | 0:a566d771935a | 42 | break; |
okini3939 | 0:a566d771935a | 43 | case SSID_NOT_FOUND: |
okini3939 | 0:a566d771935a | 44 | pc.printf("SSID not found.\r\n"); |
okini3939 | 0:a566d771935a | 45 | return -1; |
okini3939 | 0:a566d771935a | 46 | case SSID_NOT_CONFIGURED: |
okini3939 | 0:a566d771935a | 47 | pc.printf("SSID not configured.\r\n"); |
okini3939 | 0:a566d771935a | 48 | return -1; |
okini3939 | 0:a566d771935a | 49 | case JOIN_FAILED: |
okini3939 | 0:a566d771935a | 50 | pc.printf("SSID join failed.\r\n"); |
okini3939 | 0:a566d771935a | 51 | return -1; |
okini3939 | 0:a566d771935a | 52 | case WAITING_IPADDRESS: |
okini3939 | 0:a566d771935a | 53 | pc.printf("Waiting for IP configuration.\r\n"); |
okini3939 | 0:a566d771935a | 54 | break; |
okini3939 | 0:a566d771935a | 55 | case WAITING_SOCKETS: |
okini3939 | 0:a566d771935a | 56 | pc.printf("Listening sockets are being set up.\r\n"); |
okini3939 | 0:a566d771935a | 57 | break; |
okini3939 | 0:a566d771935a | 58 | case SCANNING_SSID: |
okini3939 | 0:a566d771935a | 59 | pc.printf("Currently scanning for SSID.\r\n"); |
okini3939 | 0:a566d771935a | 60 | break; |
okini3939 | 0:a566d771935a | 61 | default: |
okini3939 | 0:a566d771935a | 62 | pc.printf("\r\n"); |
okini3939 | 0:a566d771935a | 63 | break; |
okini3939 | 0:a566d771935a | 64 | } |
okini3939 | 0:a566d771935a | 65 | } |
okini3939 | 0:a566d771935a | 66 | return -1; |
okini3939 | 0:a566d771935a | 67 | } |
okini3939 | 0:a566d771935a | 68 | |
okini3939 | 0:a566d771935a | 69 | |
okini3939 | 0:a566d771935a | 70 | int main() { |
okini3939 | 0:a566d771935a | 71 | int i; |
okini3939 | 0:a566d771935a | 72 | IpAddr ipaddr, netmask, gateway, nameserver; |
okini3939 | 0:a566d771935a | 73 | |
okini3939 | 0:a566d771935a | 74 | xbee.begin(115200); |
okini3939 | 0:a566d771935a | 75 | pc.baud(115200); |
okini3939 | 0:a566d771935a | 76 | pc.printf("XBee WiFi test\r\n"); |
okini3939 | 0:a566d771935a | 77 | |
okini3939 | 0:a566d771935a | 78 | if (init_wifi(10)) { |
okini3939 | 0:a566d771935a | 79 | pc.printf("XBee error\r\n"); |
okini3939 | 0:a566d771935a | 80 | return -1; |
okini3939 | 0:a566d771935a | 81 | } |
okini3939 | 0:a566d771935a | 82 | |
okini3939 | 0:a566d771935a | 83 | xbee.getAddress(ipaddr, netmask, gateway, nameserver); |
okini3939 | 0:a566d771935a | 84 | pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
okini3939 | 0:a566d771935a | 85 | |
okini3939 | 0:a566d771935a | 86 | nameserver = gateway; |
okini3939 | 0:a566d771935a | 87 | // nameserver = IpAddr(202,232,2,35); |
okini3939 | 0:a566d771935a | 88 | xbee.setNameserver(nameserver); |
okini3939 | 0:a566d771935a | 89 | |
okini3939 | 0:a566d771935a | 90 | i = xbee.getHostByName("mbed.org", ipaddr); |
okini3939 | 0:a566d771935a | 91 | if (i) { |
okini3939 | 0:a566d771935a | 92 | pc.printf("error resolv %d\r\n", i); |
okini3939 | 0:a566d771935a | 93 | } else { |
okini3939 | 0:a566d771935a | 94 | pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
okini3939 | 0:a566d771935a | 95 | } |
okini3939 | 0:a566d771935a | 96 | |
okini3939 | 0:a566d771935a | 97 | { |
okini3939 | 0:a566d771935a | 98 | Serial xbeeserial(p13, p14); |
okini3939 | 0:a566d771935a | 99 | xbeeserial.baud(115200); |
okini3939 | 0:a566d771935a | 100 | pc.printf("Serial through\r\n"); |
okini3939 | 0:a566d771935a | 101 | for (;;) { |
okini3939 | 0:a566d771935a | 102 | if (pc.readable()) xbeeserial.putc(pc.getc()); |
okini3939 | 0:a566d771935a | 103 | // if (xbeeserial.readable()) pc.putc(xbeeserial.getc()); |
okini3939 | 0:a566d771935a | 104 | if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc()); |
okini3939 | 0:a566d771935a | 105 | } |
okini3939 | 0:a566d771935a | 106 | } |
okini3939 | 0:a566d771935a | 107 | |
okini3939 | 0:a566d771935a | 108 | } |