http://mbed.org/users/okini3939/notebook/xbee-mbed/
Dependencies: mbed XBee EthernetNetIf
main.cpp@2:8aee4ad2fc1e, 2013-12-18 (annotated)
- Committer:
- okini3939
- Date:
- Wed Dec 18 02:25:14 2013 +0000
- Revision:
- 2:8aee4ad2fc1e
- Parent:
- 1:27bf8d24244b
add XBee_conf.h
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 | 1:27bf8d24244b | 3 | #include "EthernetNetIf.h" |
okini3939 | 1:27bf8d24244b | 4 | #include "TCPSocket.h" |
okini3939 | 0:a566d771935a | 5 | |
okini3939 | 1:27bf8d24244b | 6 | |
okini3939 | 0:a566d771935a | 7 | #define SECURITY SECURITY_WPA2 |
okini3939 | 0:a566d771935a | 8 | #define SSID "XBEEWIFI" |
okini3939 | 0:a566d771935a | 9 | #define PASSPHRASE "PASSWORD" |
okini3939 | 1:27bf8d24244b | 10 | /* |
okini3939 | 0:a566d771935a | 11 | #define SECURITY SECURITY_OPEN |
okini3939 | 0:a566d771935a | 12 | #define SSID "XBEEWIFI" |
okini3939 | 0:a566d771935a | 13 | #define PASSPHRASE "" |
okini3939 | 1:27bf8d24244b | 14 | */ |
okini3939 | 1:27bf8d24244b | 15 | #define HTTP_PORT 80 |
okini3939 | 1:27bf8d24244b | 16 | #define HTTP_SRC_PORT 10080 |
okini3939 | 1:27bf8d24244b | 17 | #define HTTP_TIMEOUT 5000 // ms |
okini3939 | 1:27bf8d24244b | 18 | #define METHOD_GET 0 |
okini3939 | 1:27bf8d24244b | 19 | #define METHOD_POST 1 |
okini3939 | 0:a566d771935a | 20 | |
okini3939 | 0:a566d771935a | 21 | DigitalOut myled(LED1); |
okini3939 | 0:a566d771935a | 22 | Serial pc(USBTX, USBRX); |
okini3939 | 0:a566d771935a | 23 | |
okini3939 | 1:27bf8d24244b | 24 | XBeeWiFi xbee(p13, p14, p12, P0_22); // TX, RX, CTS, RTS |
okini3939 | 1:27bf8d24244b | 25 | //XBeeWiFi xbee(p13, p14, p12, p11); // TX, RX, CTS, RTS |
okini3939 | 0:a566d771935a | 26 | |
okini3939 | 0:a566d771935a | 27 | int init_wifi (int timeout) { |
okini3939 | 0:a566d771935a | 28 | int i, r; |
okini3939 | 0:a566d771935a | 29 | |
okini3939 | 1:27bf8d24244b | 30 | pc.printf("reset\r\n"); |
okini3939 | 0:a566d771935a | 31 | r = xbee.reset(); |
okini3939 | 1:27bf8d24244b | 32 | if (r < 0) { |
okini3939 | 1:27bf8d24244b | 33 | pc.printf("error reset %d\r\n", r); |
okini3939 | 0:a566d771935a | 34 | return -1; |
okini3939 | 0:a566d771935a | 35 | } |
okini3939 | 1:27bf8d24244b | 36 | |
okini3939 | 0:a566d771935a | 37 | xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); |
okini3939 | 0:a566d771935a | 38 | |
okini3939 | 1:27bf8d24244b | 39 | r = xbee.setup(SECURITY, SSID, PASSPHRASE); |
okini3939 | 1:27bf8d24244b | 40 | if (r < 0) { |
okini3939 | 1:27bf8d24244b | 41 | pc.printf("error setup %d\r\n", r); |
okini3939 | 1:27bf8d24244b | 42 | return -1; |
okini3939 | 1:27bf8d24244b | 43 | } |
okini3939 | 0:a566d771935a | 44 | |
okini3939 | 0:a566d771935a | 45 | for (i = 0; i < timeout; i ++) { |
okini3939 | 0:a566d771935a | 46 | wait(1); |
okini3939 | 0:a566d771935a | 47 | r = xbee.getStatus(); |
okini3939 | 0:a566d771935a | 48 | pc.printf("status %02x: ", r); |
okini3939 | 0:a566d771935a | 49 | switch (r) { |
okini3939 | 0:a566d771935a | 50 | case JOINED_AP: |
okini3939 | 0:a566d771935a | 51 | pc.printf("Successfully joined an access point.\r\n"); |
okini3939 | 0:a566d771935a | 52 | return 0; |
okini3939 | 0:a566d771935a | 53 | case INITIALIZATION: |
okini3939 | 0:a566d771935a | 54 | pc.printf("WiFi initialization in progress.\r\n"); |
okini3939 | 0:a566d771935a | 55 | break; |
okini3939 | 0:a566d771935a | 56 | case SSID_NOT_FOUND: |
okini3939 | 0:a566d771935a | 57 | pc.printf("SSID not found.\r\n"); |
okini3939 | 0:a566d771935a | 58 | return -1; |
okini3939 | 0:a566d771935a | 59 | case SSID_NOT_CONFIGURED: |
okini3939 | 0:a566d771935a | 60 | pc.printf("SSID not configured.\r\n"); |
okini3939 | 0:a566d771935a | 61 | return -1; |
okini3939 | 0:a566d771935a | 62 | case JOIN_FAILED: |
okini3939 | 0:a566d771935a | 63 | pc.printf("SSID join failed.\r\n"); |
okini3939 | 0:a566d771935a | 64 | return -1; |
okini3939 | 0:a566d771935a | 65 | case WAITING_IPADDRESS: |
okini3939 | 0:a566d771935a | 66 | pc.printf("Waiting for IP configuration.\r\n"); |
okini3939 | 0:a566d771935a | 67 | break; |
okini3939 | 0:a566d771935a | 68 | case WAITING_SOCKETS: |
okini3939 | 0:a566d771935a | 69 | pc.printf("Listening sockets are being set up.\r\n"); |
okini3939 | 0:a566d771935a | 70 | break; |
okini3939 | 0:a566d771935a | 71 | case SCANNING_SSID: |
okini3939 | 0:a566d771935a | 72 | pc.printf("Currently scanning for SSID.\r\n"); |
okini3939 | 0:a566d771935a | 73 | break; |
okini3939 | 0:a566d771935a | 74 | default: |
okini3939 | 0:a566d771935a | 75 | pc.printf("\r\n"); |
okini3939 | 0:a566d771935a | 76 | break; |
okini3939 | 0:a566d771935a | 77 | } |
okini3939 | 0:a566d771935a | 78 | } |
okini3939 | 0:a566d771935a | 79 | return -1; |
okini3939 | 0:a566d771935a | 80 | } |
okini3939 | 0:a566d771935a | 81 | |
okini3939 | 0:a566d771935a | 82 | |
okini3939 | 1:27bf8d24244b | 83 | int httpRequest (int method, Host *host, char *uri, char *head, char *body) { |
okini3939 | 1:27bf8d24244b | 84 | Timer timeout; |
okini3939 | 1:27bf8d24244b | 85 | char buf[1500], tmp[40]; |
okini3939 | 1:27bf8d24244b | 86 | int r, len; |
okini3939 | 1:27bf8d24244b | 87 | IPv4TransmitRequest httpRequest; |
okini3939 | 1:27bf8d24244b | 88 | AtCommandRequest atRequest; |
okini3939 | 1:27bf8d24244b | 89 | AtCommandResponse atResponse; |
okini3939 | 1:27bf8d24244b | 90 | IPV4RxFrame httpResponse; |
okini3939 | 1:27bf8d24244b | 91 | IpAddr ipaddr; |
okini3939 | 1:27bf8d24244b | 92 | |
okini3939 | 1:27bf8d24244b | 93 | // connect |
okini3939 | 1:27bf8d24244b | 94 | if (host->getIp().isNull()) { |
okini3939 | 1:27bf8d24244b | 95 | return -1; |
okini3939 | 1:27bf8d24244b | 96 | } |
okini3939 | 1:27bf8d24244b | 97 | if (! host->getPort()) { |
okini3939 | 1:27bf8d24244b | 98 | host->setPort(HTTP_PORT); |
okini3939 | 1:27bf8d24244b | 99 | } |
okini3939 | 1:27bf8d24244b | 100 | |
okini3939 | 1:27bf8d24244b | 101 | // create request |
okini3939 | 1:27bf8d24244b | 102 | if (method == METHOD_POST) { |
okini3939 | 1:27bf8d24244b | 103 | strcpy(buf, "POST "); |
okini3939 | 1:27bf8d24244b | 104 | } else { |
okini3939 | 1:27bf8d24244b | 105 | strcpy(buf, "GET "); |
okini3939 | 1:27bf8d24244b | 106 | } |
okini3939 | 1:27bf8d24244b | 107 | strcat(buf, uri); |
okini3939 | 1:27bf8d24244b | 108 | strcat(buf, " HTTP/1.1\r\nHost: "); |
okini3939 | 1:27bf8d24244b | 109 | strcat(buf, host->getName()); |
okini3939 | 1:27bf8d24244b | 110 | strcat(buf, "\r\n"); |
okini3939 | 1:27bf8d24244b | 111 | strcat(buf, "Connection: close\r\n"); |
okini3939 | 1:27bf8d24244b | 112 | if (head) { |
okini3939 | 1:27bf8d24244b | 113 | strcat(buf, head); |
okini3939 | 1:27bf8d24244b | 114 | } |
okini3939 | 1:27bf8d24244b | 115 | if (method == METHOD_POST) { |
okini3939 | 1:27bf8d24244b | 116 | sprintf(tmp, "Content-Length: %d\r\n", strlen(body)); |
okini3939 | 1:27bf8d24244b | 117 | strcat(buf, tmp); |
okini3939 | 1:27bf8d24244b | 118 | } |
okini3939 | 1:27bf8d24244b | 119 | strcat(buf, "\r\n"); |
okini3939 | 1:27bf8d24244b | 120 | |
okini3939 | 1:27bf8d24244b | 121 | // send HTTP request |
okini3939 | 1:27bf8d24244b | 122 | len = strlen(buf); |
okini3939 | 1:27bf8d24244b | 123 | ipaddr = host->getIp(); |
okini3939 | 1:27bf8d24244b | 124 | httpRequest.setAddress(ipaddr); |
okini3939 | 1:27bf8d24244b | 125 | httpRequest.setDstPort(host->getPort()); |
okini3939 | 1:27bf8d24244b | 126 | httpRequest.setSrcPort(HTTP_SRC_PORT); |
okini3939 | 1:27bf8d24244b | 127 | httpRequest.setProtocol(PROTOCOL_TCP); |
okini3939 | 1:27bf8d24244b | 128 | httpRequest.setPayload((uint8_t*)buf); |
okini3939 | 1:27bf8d24244b | 129 | httpRequest.setPayloadLength(len); |
okini3939 | 1:27bf8d24244b | 130 | httpRequest.setFrameId(xbee.getNextFrameId()); |
okini3939 | 1:27bf8d24244b | 131 | xbee.send(httpRequest); |
okini3939 | 1:27bf8d24244b | 132 | r = xbee.getWiResponse(TX_STATUS_RESPONSE, httpRequest.getFrameId()); |
okini3939 | 1:27bf8d24244b | 133 | pc.printf("wifi TX: %d\r\n", r); |
okini3939 | 1:27bf8d24244b | 134 | if (r < 0) return -1; |
okini3939 | 1:27bf8d24244b | 135 | |
okini3939 | 1:27bf8d24244b | 136 | // wait responce |
okini3939 | 1:27bf8d24244b | 137 | timeout.reset(); |
okini3939 | 1:27bf8d24244b | 138 | timeout.start(); |
okini3939 | 1:27bf8d24244b | 139 | while (timeout.read_ms() < HTTP_TIMEOUT) { |
okini3939 | 1:27bf8d24244b | 140 | // recv HTTP request |
okini3939 | 1:27bf8d24244b | 141 | r = xbee.getWiResponse(IPv4_RX_FRAME, 0, 3000); |
okini3939 | 1:27bf8d24244b | 142 | pc.printf("wifi RX: %d\r\n", r); |
okini3939 | 1:27bf8d24244b | 143 | if (r >= 0) { |
okini3939 | 1:27bf8d24244b | 144 | timeout.reset(); |
okini3939 | 1:27bf8d24244b | 145 | xbee.getResponse().getAtCommandResponse(httpResponse); |
okini3939 | 1:27bf8d24244b | 146 | pc.printf("\r\n--- recv %d ---\r\n", httpResponse.getDataLength()); |
okini3939 | 1:27bf8d24244b | 147 | strncpy(buf, (char*)httpResponse.getData(), httpResponse.getDataLength()); |
okini3939 | 1:27bf8d24244b | 148 | buf[httpResponse.getDataLength()] = 0; |
okini3939 | 1:27bf8d24244b | 149 | pc.printf(buf); |
okini3939 | 1:27bf8d24244b | 150 | } |
okini3939 | 1:27bf8d24244b | 151 | } |
okini3939 | 1:27bf8d24244b | 152 | timeout.stop(); |
okini3939 | 1:27bf8d24244b | 153 | return 0; |
okini3939 | 1:27bf8d24244b | 154 | } |
okini3939 | 1:27bf8d24244b | 155 | |
okini3939 | 0:a566d771935a | 156 | int main() { |
okini3939 | 0:a566d771935a | 157 | int i; |
okini3939 | 0:a566d771935a | 158 | IpAddr ipaddr, netmask, gateway, nameserver; |
okini3939 | 1:27bf8d24244b | 159 | Host host; |
okini3939 | 0:a566d771935a | 160 | |
okini3939 | 0:a566d771935a | 161 | xbee.begin(115200); |
okini3939 | 1:27bf8d24244b | 162 | // xbee.baud(921000); |
okini3939 | 0:a566d771935a | 163 | pc.baud(115200); |
okini3939 | 0:a566d771935a | 164 | pc.printf("XBee WiFi test\r\n"); |
okini3939 | 0:a566d771935a | 165 | |
okini3939 | 1:27bf8d24244b | 166 | if (init_wifi(20)) { |
okini3939 | 0:a566d771935a | 167 | pc.printf("XBee error\r\n"); |
okini3939 | 0:a566d771935a | 168 | return -1; |
okini3939 | 0:a566d771935a | 169 | } |
okini3939 | 0:a566d771935a | 170 | |
okini3939 | 0:a566d771935a | 171 | xbee.getAddress(ipaddr, netmask, gateway, nameserver); |
okini3939 | 0:a566d771935a | 172 | pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
okini3939 | 1:27bf8d24244b | 173 | if (ipaddr == 0) { |
okini3939 | 1:27bf8d24244b | 174 | pc.printf("not configure\r\n"); |
okini3939 | 1:27bf8d24244b | 175 | return -1; |
okini3939 | 1:27bf8d24244b | 176 | } |
okini3939 | 0:a566d771935a | 177 | |
okini3939 | 1:27bf8d24244b | 178 | /* |
okini3939 | 1:27bf8d24244b | 179 | // DNS lookup |
okini3939 | 1:27bf8d24244b | 180 | // *** Note: wifi is turned off when XBee send/recv the port 53 udp packet. |
okini3939 | 1:27bf8d24244b | 181 | // XBee wifi --> request --> DNS server (wifi OK) |
okini3939 | 1:27bf8d24244b | 182 | // DNS server --> responce --> XBee wifi (wifi turned off) |
okini3939 | 1:27bf8d24244b | 183 | // why ?? |
okini3939 | 0:a566d771935a | 184 | nameserver = gateway; |
okini3939 | 1:27bf8d24244b | 185 | // nameserver = IpAddr(12,34,56,78); |
okini3939 | 1:27bf8d24244b | 186 | pc.printf("resolver %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]); |
okini3939 | 0:a566d771935a | 187 | xbee.setNameserver(nameserver); |
okini3939 | 0:a566d771935a | 188 | i = xbee.getHostByName("mbed.org", ipaddr); |
okini3939 | 0:a566d771935a | 189 | if (i) { |
okini3939 | 0:a566d771935a | 190 | pc.printf("error resolv %d\r\n", i); |
okini3939 | 0:a566d771935a | 191 | } else { |
okini3939 | 0:a566d771935a | 192 | pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
okini3939 | 0:a566d771935a | 193 | } |
okini3939 | 1:27bf8d24244b | 194 | */ |
okini3939 | 1:27bf8d24244b | 195 | // HTTP request |
okini3939 | 1:27bf8d24244b | 196 | wait(1); |
okini3939 | 1:27bf8d24244b | 197 | host.setIp(IpAddr(217,140,96,42)); |
okini3939 | 1:27bf8d24244b | 198 | host.setName("mbed.org"); |
okini3939 | 1:27bf8d24244b | 199 | httpRequest(METHOD_GET, &host, "/", "", ""); |
okini3939 | 0:a566d771935a | 200 | |
okini3939 | 1:27bf8d24244b | 201 | // Serial through |
okini3939 | 0:a566d771935a | 202 | { |
okini3939 | 0:a566d771935a | 203 | Serial xbeeserial(p13, p14); |
okini3939 | 1:27bf8d24244b | 204 | DigitalOut rts(p22); |
okini3939 | 1:27bf8d24244b | 205 | |
okini3939 | 0:a566d771935a | 206 | xbeeserial.baud(115200); |
okini3939 | 1:27bf8d24244b | 207 | rts = 0; |
okini3939 | 0:a566d771935a | 208 | pc.printf("Serial through\r\n"); |
okini3939 | 0:a566d771935a | 209 | for (;;) { |
okini3939 | 0:a566d771935a | 210 | if (pc.readable()) xbeeserial.putc(pc.getc()); |
okini3939 | 1:27bf8d24244b | 211 | if (xbeeserial.readable()) pc.putc(xbeeserial.getc()); |
okini3939 | 1:27bf8d24244b | 212 | // if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc()); |
okini3939 | 0:a566d771935a | 213 | } |
okini3939 | 0:a566d771935a | 214 | } |
okini3939 | 0:a566d771935a | 215 | |
okini3939 | 0:a566d771935a | 216 | } |