XBee Wifi on EA LPC4088 QS bBoard
Dependencies: EthernetNetIf XBee_EA_LPC4088_QS mbed
Fork of XBee_wifi_sample by
main.cpp
- Committer:
- marin8703
- Date:
- 2015-02-10
- Revision:
- 3:91764c4bb3d3
- Parent:
- 1:27bf8d24244b
File content as of revision 3:91764c4bb3d3:
#include "mbed.h" #include "XBeeWiFi.h" #include "EthernetNetIf.h" #include "TCPSocket.h" #define SECURITY SECURITY_WPA2 #define SSID "HomeNet24" #define PASSPHRASE "L57Q1*P3h" #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(p9, p10); XBeeWiFi xbee(P4_22 , P4_23, P4_20, P4_21); // TX, RX, CTS, RTS int led_count = 0; int error_fail = 0; int init_wifi (int timeout) { int i, r; pc.printf("reset\r\n"); r = xbee.reset(); if (r < 0) { pc.printf("error reset %d\r\n", r); return -1; } xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); 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); 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 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; //Host host.setIp(IpAddr(217,140,96,42)); pc.baud(115200); pc.printf("Starting main program...\r\n\n"); // pc.printf("1 cycles of LED1 on/off start...\r\n"); // // led_count = 0; // while(led_count<1) { // myled = 1; // wait(1); // myled = 0; // wait(1); // led_count++; // } // myled = 1; // // pc.printf("1 cycles of LED1 on/off DONE.\r\n\n"); // pc.printf("XBee WiFi test starting...\r\n\n"); pc.printf("**CALLING xbee.begin...\r\n"); xbee.begin(9600); pc.printf("xbee.begin DONE.**\r\n\n"); pc.printf("**CALLING init_wifi...\r\n"); if (init_wifi(10)) { pc.printf("XBee error\r\n"); //return -1; error_fail = 1; } pc.printf("init_wifi DONE.*\r\n\n"); while(error_fail) { myled = 1; wait(1); myled = 0; wait(1); } pc.printf("**Getting IP address**\r\n\n"); 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; } wait(5); ///////////////////////////////// /* // 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(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(p9, p10); //DigitalOut rts(p22); //DigitalOut rts(P4_21); // 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()); // } // } }