A quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of mbed-os.
The program brings up the WiFi and the underlying network interface, and uses it to scans available networks, connects to a network, prints interface and connection details and performs simple HTTP operation.
Supported hardware:
- UBLOX Odin board built-in WiFi module
- REALTEK_RTL8195AM built-in WiFi module
- NUCLEO_F401RE with X-NUCLEO-IDW01M1 WiFi expansion board using pins D8 D2
- NUCLEO_F429ZI with ESP8266-01 module using pins D1 D0
- NUCLEO_L476RG with ESP8266-01 module using pins D8 D2
- Other mbed targets with ESP8266 module or X-NUCLEO-IDW01M1 expansion board
Not that the mbed target board the WiFi shield gets connected to shouldn't have any other network interface e.g. Ethernet.
ESP8266 is a fallback option and will be used if the build is for unsupported platform.
Revision 66:2cf02c7d430c, committed 2018-06-06
- Comitter:
- mbed_official
- Date:
- Wed Jun 06 15:00:03 2018 +0100
- Parent:
- 65:c6833441fd15
- Child:
- 67:ebff4a8d228d
- Commit message:
- ONME-3694: Check network interface and socket error codes.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Jun 06 11:00:04 2018 +0100 +++ b/main.cpp Wed Jun 06 15:00:03 2018 +0100 @@ -74,13 +74,23 @@ int count = wifi->scan(NULL,0); + if (count <= 0) { + printf("scan() failed with return value: %d\n", count); + return 0; + } + /* Limit number of network arbitrary to 15 */ count = count < 15 ? count : 15; ap = new WiFiAccessPoint[count]; count = wifi->scan(ap, count); - for (int i = 0; i < count; i++) - { + + if (count <= 0) { + printf("scan() failed with return value: %d\n", count); + return 0; + } + + for (int i = 0; i < count; i++) { printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); @@ -99,8 +109,13 @@ printf("Sending HTTP request to www.arm.com...\n"); // Open a socket on the network interface, and create a TCP connection to www.arm.com - socket.open(net); - response = socket.connect("www.arm.com", 80); + response = socket.open(net); + if(0 != response) { + printf("socket.open() failed: %d\n", response); + return; + } + + response = socket.connect("api.ipify.org", 80); if(0 != response) { printf("Error connecting: %d\n", response); socket.close(); @@ -108,24 +123,22 @@ } // Send a simple http request - char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; + char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"; nsapi_size_t size = strlen(sbuffer); - response = 0; - while(size) - { + + // Loop until whole request send + while(size) { response = socket.send(sbuffer+response, size); if (response < 0) { printf("Error sending data: %d\n", response); socket.close(); return; - } else { - size -= response; - // Check if entire message was sent or not - printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); } + size -= response; + printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); } - // Recieve a simple http response and print out the response line + // Receieve a simple http response and print out the response line char rbuffer[64]; response = socket.recv(rbuffer, sizeof rbuffer); if (response < 0) { @@ -153,7 +166,7 @@ printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); if (ret != 0) { - printf("\nConnection error\n"); + printf("\nConnection error: %d\n", ret); return -1; }