Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf XBee_EA_LPC4088_QS mbed
Fork of XBee_wifi_sample by
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 "HomeNet24" 00009 #define PASSPHRASE "L57Q1*P3h" 00010 00011 #define HTTP_PORT 80 00012 #define HTTP_SRC_PORT 10080 00013 #define HTTP_TIMEOUT 5000 // ms 00014 #define METHOD_GET 0 00015 #define METHOD_POST 1 00016 00017 DigitalOut myled(LED1); 00018 Serial pc(p9, p10); 00019 00020 XBeeWiFi xbee(P4_22 , P4_23, P4_20, P4_21); // TX, RX, CTS, RTS 00021 00022 int led_count = 0; 00023 int error_fail = 0; 00024 00025 int init_wifi (int timeout) { 00026 int i, r; 00027 00028 pc.printf("reset\r\n"); 00029 r = xbee.reset(); 00030 if (r < 0) { 00031 pc.printf("error reset %d\r\n", r); 00032 return -1; 00033 } 00034 00035 xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); 00036 00037 r = xbee.setup(SECURITY, SSID, PASSPHRASE); 00038 if (r < 0) { 00039 pc.printf("error setup %d\r\n", r); 00040 return -1; 00041 } 00042 00043 for (i = 0; i < timeout; i ++) { 00044 wait(1); 00045 r = xbee.getStatus(); 00046 pc.printf("status %02x: ", r); 00047 switch (r) { 00048 case JOINED_AP: 00049 pc.printf("Successfully joined an access point.\r\n"); 00050 return 0; 00051 case INITIALIZATION: 00052 pc.printf("WiFi initialization in progress.\r\n"); 00053 break; 00054 case SSID_NOT_FOUND: 00055 pc.printf("SSID not found.\r\n"); 00056 return -1; 00057 case SSID_NOT_CONFIGURED: 00058 pc.printf("SSID not configured.\r\n"); 00059 return -1; 00060 case JOIN_FAILED: 00061 pc.printf("SSID join failed.\r\n"); 00062 return -1; 00063 case WAITING_IPADDRESS: 00064 pc.printf("Waiting for IP configuration.\r\n"); 00065 break; 00066 case WAITING_SOCKETS: 00067 pc.printf("Listening sockets are being set up.\r\n"); 00068 break; 00069 case SCANNING_SSID: 00070 pc.printf("Currently scanning for SSID.\r\n"); 00071 break; 00072 default: 00073 pc.printf("\r\n"); 00074 break; 00075 } 00076 } 00077 return -1; 00078 } 00079 00080 00081 int httpRequest (int method, Host *host, char *uri, char *head, char *body) { 00082 Timer timeout; 00083 char buf[1500], tmp[40]; 00084 int r, len; 00085 IPv4TransmitRequest httpRequest; 00086 AtCommandRequest atRequest; 00087 AtCommandResponse atResponse; 00088 IPV4RxFrame httpResponse; 00089 IpAddr ipaddr; 00090 00091 // connect 00092 if (host->getIp().isNull()) { 00093 return -1; 00094 } 00095 if (! host->getPort()) { 00096 host->setPort(HTTP_PORT); 00097 } 00098 00099 // create request 00100 if (method == METHOD_POST) { 00101 strcpy(buf, "POST "); 00102 } else { 00103 strcpy(buf, "GET "); 00104 } 00105 strcat(buf, uri); 00106 strcat(buf, " HTTP/1.1\r\nHost: "); 00107 strcat(buf, host->getName()); 00108 strcat(buf, "\r\n"); 00109 strcat(buf, "Connection: close\r\n"); 00110 if (head) { 00111 strcat(buf, head); 00112 } 00113 if (method == METHOD_POST) { 00114 sprintf(tmp, "Content-Length: %d\r\n", strlen(body)); 00115 strcat(buf, tmp); 00116 } 00117 strcat(buf, "\r\n"); 00118 00119 // send HTTP request 00120 len = strlen(buf); 00121 ipaddr = host->getIp(); 00122 httpRequest.setAddress(ipaddr); 00123 httpRequest.setDstPort(host->getPort()); 00124 httpRequest.setSrcPort(HTTP_SRC_PORT); 00125 httpRequest.setProtocol(PROTOCOL_TCP); 00126 httpRequest.setPayload((uint8_t*)buf); 00127 httpRequest.setPayloadLength(len); 00128 httpRequest.setFrameId(xbee.getNextFrameId()); 00129 xbee.send(httpRequest); 00130 r = xbee.getWiResponse(TX_STATUS_RESPONSE, httpRequest.getFrameId()); 00131 pc.printf("wifi TX: %d\r\n", r); 00132 if (r < 0) return -1; 00133 00134 // wait responce 00135 timeout.reset(); 00136 timeout.start(); 00137 while (timeout.read_ms() < HTTP_TIMEOUT) { 00138 // recv HTTP request 00139 r = xbee.getWiResponse(IPv4_RX_FRAME, 0, 3000); 00140 pc.printf("wifi RX: %d\r\n", r); 00141 if (r >= 0) { 00142 timeout.reset(); 00143 xbee.getResponse().getAtCommandResponse(httpResponse); 00144 pc.printf("\r\n--- recv %d ---\r\n", httpResponse.getDataLength()); 00145 strncpy(buf, (char*)httpResponse.getData(), httpResponse.getDataLength()); 00146 buf[httpResponse.getDataLength()] = 0; 00147 pc.printf(buf); 00148 } 00149 } 00150 timeout.stop(); 00151 return 0; 00152 } 00153 00154 int main() { 00155 //int i; 00156 IpAddr ipaddr, netmask, gateway, nameserver; 00157 Host host; 00158 //Host host.setIp(IpAddr(217,140,96,42)); 00159 00160 pc.baud(115200); 00161 pc.printf("Starting main program...\r\n\n"); 00162 00163 // pc.printf("1 cycles of LED1 on/off start...\r\n"); 00164 // 00165 // led_count = 0; 00166 // while(led_count<1) { 00167 // myled = 1; 00168 // wait(1); 00169 // myled = 0; 00170 // wait(1); 00171 // led_count++; 00172 // } 00173 // myled = 1; 00174 // 00175 // pc.printf("1 cycles of LED1 on/off DONE.\r\n\n"); 00176 00177 // pc.printf("XBee WiFi test starting...\r\n\n"); 00178 00179 pc.printf("**CALLING xbee.begin...\r\n"); 00180 xbee.begin(9600); 00181 pc.printf("xbee.begin DONE.**\r\n\n"); 00182 pc.printf("**CALLING init_wifi...\r\n"); 00183 if (init_wifi(10)) { 00184 pc.printf("XBee error\r\n"); 00185 //return -1; 00186 error_fail = 1; 00187 } 00188 pc.printf("init_wifi DONE.*\r\n\n"); 00189 00190 while(error_fail) { 00191 myled = 1; 00192 wait(1); 00193 myled = 0; 00194 wait(1); 00195 } 00196 00197 pc.printf("**Getting IP address**\r\n\n"); 00198 xbee.getAddress(ipaddr, netmask, gateway, nameserver); 00199 pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00200 if (ipaddr == 0) { 00201 pc.printf("not configure\r\n"); 00202 return -1; 00203 } 00204 wait(5); 00205 00206 ///////////////////////////////// 00207 /* 00208 // DNS lookup 00209 // *** Note: wifi is turned off when XBee send/recv the port 53 udp packet. 00210 // XBee wifi --> request --> DNS server (wifi OK) 00211 // DNS server --> responce --> XBee wifi (wifi turned off) 00212 // why ?? 00213 nameserver = gateway; 00214 // nameserver = IpAddr(12,34,56,78); 00215 pc.printf("resolver %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]); 00216 xbee.setNameserver(nameserver); 00217 i = xbee.getHostByName("mbed.org", ipaddr); 00218 if (i) { 00219 pc.printf("error resolv %d\r\n", i); 00220 } else { 00221 pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00222 } 00223 */ 00224 /////////////////////////////////////// 00225 // HTTP request 00226 wait(1); 00227 host.setIp(IpAddr(217,140,96,42)); 00228 host.setName("mbed.org"); 00229 httpRequest(METHOD_GET, &host, "/", "", ""); 00230 00231 // Serial through 00232 // { 00233 // Serial xbeeserial(p9, p10); 00234 //DigitalOut rts(p22); 00235 //DigitalOut rts(P4_21); 00236 00237 // xbeeserial.baud(115200); 00238 //rts = 0; 00239 // pc.printf("Serial through\r\n"); 00240 // for (;;) { 00241 // if (pc.readable()) xbeeserial.putc(pc.getc()); 00242 // if (xbeeserial.readable()) pc.putc(xbeeserial.getc()); 00243 // if (xbeeserial.readable()) pc.printf("%02x ", xbeeserial.getc()); 00244 // } 00245 // } 00246 00247 }
Generated on Mon Jul 18 2022 01:10:47 by
1.7.2
