http://mbed.org/users/okini3939/notebook/xbee-mbed/
Dependencies: mbed XBee EthernetNetIf
main.cpp
00001 #include "mbed.h" 00002 #include "XBeeWiFi.h" 00003 #include "EthernetNetIf.h" 00004 #include "TCPSocket.h" 00005 00006 00007 #define SECURITY SECURITY_WPA2 00008 #define SSID "XBEEWIFI" 00009 #define PASSPHRASE "PASSWORD" 00010 /* 00011 #define SECURITY SECURITY_OPEN 00012 #define SSID "XBEEWIFI" 00013 #define PASSPHRASE "" 00014 */ 00015 #define HTTP_PORT 80 00016 #define HTTP_SRC_PORT 10080 00017 #define HTTP_TIMEOUT 5000 // ms 00018 #define METHOD_GET 0 00019 #define METHOD_POST 1 00020 00021 DigitalOut myled(LED1); 00022 Serial pc(USBTX, USBRX); 00023 00024 XBeeWiFi xbee(p13, p14, p12, P0_22); // TX, RX, CTS, RTS 00025 //XBeeWiFi xbee(p13, p14, p12, p11); // TX, RX, CTS, RTS 00026 00027 int init_wifi (int timeout) { 00028 int i, r; 00029 00030 pc.printf("reset\r\n"); 00031 r = xbee.reset(); 00032 if (r < 0) { 00033 pc.printf("error reset %d\r\n", r); 00034 return -1; 00035 } 00036 00037 xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); 00038 00039 r = xbee.setup(SECURITY, SSID, PASSPHRASE); 00040 if (r < 0) { 00041 pc.printf("error setup %d\r\n", r); 00042 return -1; 00043 } 00044 00045 for (i = 0; i < timeout; i ++) { 00046 wait(1); 00047 r = xbee.getStatus(); 00048 pc.printf("status %02x: ", r); 00049 switch (r) { 00050 case JOINED_AP: 00051 pc.printf("Successfully joined an access point.\r\n"); 00052 return 0; 00053 case INITIALIZATION: 00054 pc.printf("WiFi initialization in progress.\r\n"); 00055 break; 00056 case SSID_NOT_FOUND: 00057 pc.printf("SSID not found.\r\n"); 00058 return -1; 00059 case SSID_NOT_CONFIGURED: 00060 pc.printf("SSID not configured.\r\n"); 00061 return -1; 00062 case JOIN_FAILED: 00063 pc.printf("SSID join failed.\r\n"); 00064 return -1; 00065 case WAITING_IPADDRESS: 00066 pc.printf("Waiting for IP configuration.\r\n"); 00067 break; 00068 case WAITING_SOCKETS: 00069 pc.printf("Listening sockets are being set up.\r\n"); 00070 break; 00071 case SCANNING_SSID: 00072 pc.printf("Currently scanning for SSID.\r\n"); 00073 break; 00074 default: 00075 pc.printf("\r\n"); 00076 break; 00077 } 00078 } 00079 return -1; 00080 } 00081 00082 00083 int httpRequest (int method, Host *host, char *uri, char *head, char *body) { 00084 Timer timeout; 00085 char buf[1500], tmp[40]; 00086 int r, len; 00087 IPv4TransmitRequest httpRequest; 00088 AtCommandRequest atRequest; 00089 AtCommandResponse atResponse; 00090 IPV4RxFrame httpResponse; 00091 IpAddr ipaddr; 00092 00093 // connect 00094 if (host->getIp().isNull()) { 00095 return -1; 00096 } 00097 if (! host->getPort()) { 00098 host->setPort(HTTP_PORT); 00099 } 00100 00101 // create request 00102 if (method == METHOD_POST) { 00103 strcpy(buf, "POST "); 00104 } else { 00105 strcpy(buf, "GET "); 00106 } 00107 strcat(buf, uri); 00108 strcat(buf, " HTTP/1.1\r\nHost: "); 00109 strcat(buf, host->getName()); 00110 strcat(buf, "\r\n"); 00111 strcat(buf, "Connection: close\r\n"); 00112 if (head) { 00113 strcat(buf, head); 00114 } 00115 if (method == METHOD_POST) { 00116 sprintf(tmp, "Content-Length: %d\r\n", strlen(body)); 00117 strcat(buf, tmp); 00118 } 00119 strcat(buf, "\r\n"); 00120 00121 // send HTTP request 00122 len = strlen(buf); 00123 ipaddr = host->getIp(); 00124 httpRequest.setAddress(ipaddr); 00125 httpRequest.setDstPort(host->getPort()); 00126 httpRequest.setSrcPort(HTTP_SRC_PORT); 00127 httpRequest.setProtocol(PROTOCOL_TCP); 00128 httpRequest.setPayload((uint8_t*)buf); 00129 httpRequest.setPayloadLength(len); 00130 httpRequest.setFrameId(xbee.getNextFrameId()); 00131 xbee.send(httpRequest); 00132 r = xbee.getWiResponse(TX_STATUS_RESPONSE, httpRequest.getFrameId()); 00133 pc.printf("wifi TX: %d\r\n", r); 00134 if (r < 0) return -1; 00135 00136 // wait responce 00137 timeout.reset(); 00138 timeout.start(); 00139 while (timeout.read_ms() < HTTP_TIMEOUT) { 00140 // recv HTTP request 00141 r = xbee.getWiResponse(IPv4_RX_FRAME, 0, 3000); 00142 pc.printf("wifi RX: %d\r\n", r); 00143 if (r >= 0) { 00144 timeout.reset(); 00145 xbee.getResponse().getAtCommandResponse(httpResponse); 00146 pc.printf("\r\n--- recv %d ---\r\n", httpResponse.getDataLength()); 00147 strncpy(buf, (char*)httpResponse.getData(), httpResponse.getDataLength()); 00148 buf[httpResponse.getDataLength()] = 0; 00149 pc.printf(buf); 00150 } 00151 } 00152 timeout.stop(); 00153 return 0; 00154 } 00155 00156 int main() { 00157 int i; 00158 IpAddr ipaddr, netmask, gateway, nameserver; 00159 Host host; 00160 00161 xbee.begin(115200); 00162 // xbee.baud(921000); 00163 pc.baud(115200); 00164 pc.printf("XBee WiFi test\r\n"); 00165 00166 if (init_wifi(20)) { 00167 pc.printf("XBee error\r\n"); 00168 return -1; 00169 } 00170 00171 xbee.getAddress(ipaddr, netmask, gateway, nameserver); 00172 pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00173 if (ipaddr == 0) { 00174 pc.printf("not configure\r\n"); 00175 return -1; 00176 } 00177 00178 /* 00179 // DNS lookup 00180 // *** Note: wifi is turned off when XBee send/recv the port 53 udp packet. 00181 // XBee wifi --> request --> DNS server (wifi OK) 00182 // DNS server --> responce --> XBee wifi (wifi turned off) 00183 // why ?? 00184 nameserver = gateway; 00185 // nameserver = IpAddr(12,34,56,78); 00186 pc.printf("resolver %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]); 00187 xbee.setNameserver(nameserver); 00188 i = xbee.getHostByName("mbed.org", ipaddr); 00189 if (i) { 00190 pc.printf("error resolv %d\r\n", i); 00191 } else { 00192 pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00193 } 00194 */ 00195 // HTTP request 00196 wait(1); 00197 host.setIp(IpAddr(217,140,96,42)); 00198 host.setName("mbed.org"); 00199 httpRequest(METHOD_GET, &host, "/", "", ""); 00200 00201 // Serial through 00202 { 00203 Serial xbeeserial(p13, p14); 00204 DigitalOut rts(p22); 00205 00206 xbeeserial.baud(115200); 00207 rts = 0; 00208 pc.printf("Serial through\r\n"); 00209 for (;;) { 00210 if (pc.readable()) xbeeserial.putc(pc.getc()); 00211 if (xbeeserial.readable()) pc.putc(xbeeserial.getc()); 00212 // if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc()); 00213 } 00214 } 00215 00216 }
Generated on Thu Jul 14 2022 06:13:31 by 1.7.2