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: XBeeWiFi_common XBee_with_SPI 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 "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 #if defined(TARGET_LPC1768) || defined(TARGET_LPC11U24) 00025 XBeeWiFi xbee(p11, p12, p13, p14, p15, p16); // mosi, miso, sclk, ssel, attn, reset 00026 #elif defined(TARGET_LPC1114) 00027 XBeeWiFi xbee(dp2, dp1, dp6, dp9, dp10, dp11); // mosi, miso, sclk, ssel, attn, reset 00028 #endif 00029 00030 int init_wifi (int timeout) { 00031 int i, r; 00032 00033 pc.printf("reset\r\n"); 00034 r = xbee.reset(); 00035 if (r < 0) { 00036 pc.printf("error reset %d\r\n", r); 00037 return -1; 00038 } 00039 00040 xbee.getWiResponse(MODEM_STATUS_RESPONSE, 5000); 00041 00042 r = xbee.setup(SECURITY, SSID, PASSPHRASE); 00043 if (r < 0) { 00044 pc.printf("error setup %d\r\n", r); 00045 return -1; 00046 } 00047 00048 for (i = 0; i < timeout; i ++) { 00049 wait(1); 00050 r = xbee.getStatus(); 00051 pc.printf("status %02x: ", r); 00052 switch (r) { 00053 case JOINED_AP: 00054 pc.printf("Successfully joined an access point.\r\n"); 00055 return 0; 00056 case INITIALIZATION: 00057 pc.printf("WiFi initialization in progress.\r\n"); 00058 break; 00059 case SSID_NOT_FOUND: 00060 pc.printf("SSID not found.\r\n"); 00061 return -1; 00062 case SSID_NOT_CONFIGURED: 00063 pc.printf("SSID not configured.\r\n"); 00064 return -1; 00065 case JOIN_FAILED: 00066 pc.printf("SSID join failed.\r\n"); 00067 return -1; 00068 case WAITING_IPADDRESS: 00069 pc.printf("Waiting for IP configuration.\r\n"); 00070 break; 00071 case WAITING_SOCKETS: 00072 pc.printf("Listening sockets are being set up.\r\n"); 00073 break; 00074 case SCANNING_SSID: 00075 pc.printf("Currently scanning for SSID.\r\n"); 00076 break; 00077 default: 00078 pc.printf("\r\n"); 00079 break; 00080 } 00081 } 00082 return -1; 00083 } 00084 00085 00086 int httpRequest (int method, Host *host, char *uri, char *head, char *body) { 00087 Timer timeout; 00088 char buf[1500], tmp[40]; 00089 int r, len; 00090 IPv4TransmitRequest httpRequest; 00091 AtCommandRequest atRequest; 00092 AtCommandResponse atResponse; 00093 IPV4RxFrame httpResponse; 00094 IpAddr ipaddr; 00095 00096 // connect 00097 if (host->getIp().isNull()) { 00098 return -1; 00099 } 00100 if (! host->getPort()) { 00101 host->setPort(HTTP_PORT); 00102 } 00103 00104 // create request 00105 if (method == METHOD_POST) { 00106 strcpy(buf, "POST "); 00107 } else { 00108 strcpy(buf, "GET "); 00109 } 00110 strcat(buf, uri); 00111 strcat(buf, " HTTP/1.1\r\nHost: "); 00112 strcat(buf, host->getName()); 00113 strcat(buf, "\r\n"); 00114 strcat(buf, "Connection: close\r\n"); 00115 if (head) { 00116 strcat(buf, head); 00117 } 00118 if (method == METHOD_POST) { 00119 sprintf(tmp, "Content-Length: %d\r\n", strlen(body)); 00120 strcat(buf, tmp); 00121 } 00122 strcat(buf, "\r\n"); 00123 00124 // send HTTP request 00125 len = strlen(buf); 00126 ipaddr = host->getIp(); 00127 httpRequest.setAddress(ipaddr); 00128 httpRequest.setDstPort(host->getPort()); 00129 httpRequest.setSrcPort(HTTP_SRC_PORT); 00130 httpRequest.setProtocol(PROTOCOL_TCP); 00131 httpRequest.setPayload((uint8_t*)buf); 00132 httpRequest.setPayloadLength(len); 00133 httpRequest.setFrameId(xbee.getNextFrameId()); 00134 xbee.send(httpRequest); 00135 r = xbee.getWiResponse(TX_STATUS_RESPONSE, httpRequest.getFrameId()); 00136 pc.printf("wifi TX: %d\r\n", r); 00137 if (r < 0) return -1; 00138 00139 // wait responce 00140 timeout.reset(); 00141 timeout.start(); 00142 while (timeout.read_ms() < HTTP_TIMEOUT) { 00143 // recv HTTP request 00144 r = xbee.getWiResponse(IPv4_RX_FRAME, 0, 3000); 00145 pc.printf("wifi RX: %d\r\n", r); 00146 if (r >= 0) { 00147 timeout.reset(); 00148 xbee.getResponse().getAtCommandResponse(httpResponse); 00149 pc.printf("\r\n--- recv %d ---\r\n", httpResponse.getDataLength()); 00150 strncpy(buf, (char*)httpResponse.getData(), httpResponse.getDataLength()); 00151 buf[httpResponse.getDataLength()] = 0; 00152 pc.printf(buf); 00153 } 00154 } 00155 timeout.stop(); 00156 return 0; 00157 } 00158 00159 int main() { 00160 // int i; 00161 IpAddr ipaddr, netmask, gateway, nameserver; 00162 Host host; 00163 00164 // xbee.begin(115200); 00165 // xbee.baud(921000); 00166 pc.baud(9600); 00167 pc.printf("XBee WiFi test\r\n"); 00168 00169 if (init_wifi(20)) { 00170 pc.printf("XBee error\r\n"); 00171 return -1; 00172 } 00173 00174 xbee.getAddress(ipaddr, netmask, gateway, nameserver); 00175 pc.printf("IP address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00176 if (ipaddr.isNull()) { 00177 pc.printf("not configure\r\n"); 00178 return -1; 00179 } 00180 00181 /* 00182 // DNS lookup 00183 // *** Note: wifi is turned off when XBee send/recv the port 53 udp packet. 00184 // XBee wifi --> request --> DNS server (wifi OK) 00185 // DNS server --> responce --> XBee wifi (wifi turned off) 00186 // why ?? 00187 nameserver = gateway; 00188 // nameserver = IpAddr(12,34,56,78); 00189 pc.printf("resolver %d.%d.%d.%d\r\n", nameserver[0], nameserver[1], nameserver[2], nameserver[3]); 00190 xbee.setNameserver(nameserver); 00191 i = xbee.getHostByName("mbed.org", ipaddr); 00192 if (i) { 00193 pc.printf("error resolv %d\r\n", i); 00194 } else { 00195 pc.printf("resolv address %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); 00196 } 00197 */ 00198 // HTTP request 00199 wait(1); 00200 host.setIp(IpAddr(54,243,166,106)); 00201 host.setName("httpbin.org"); 00202 httpRequest(METHOD_GET, &host, "/get", "", ""); 00203 00204 pc.printf("done.\r\n"); 00205 while(1); 00206 00207 }
Generated on Tue Jul 19 2022 06:50:28 by
1.7.2
