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.
Diff: main.cpp
- Revision:
- 91:dab9882e2b49
- Parent:
- 77:b74ac6641a3e
--- a/main.cpp Wed Jan 09 09:30:03 2019 +0000 +++ b/main.cpp Wed Jan 09 14:30:04 2019 +0000 @@ -15,7 +15,6 @@ */ #include "mbed.h" -#include "TCPSocket.h" WiFiInterface *wifi; @@ -73,60 +72,8 @@ return count; } -void http_demo(NetworkInterface *net) -{ - TCPSocket socket; - nsapi_error_t response; - - 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 - 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(); - return; - } - - // Send a simple http request - char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"; - nsapi_size_t size = strlen(sbuffer); - - // 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; - } - size -= response; - printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); - } - - // Receieve a simple http response and print out the response line - char rbuffer[64]; - response = socket.recv(rbuffer, sizeof rbuffer); - if (response < 0) { - printf("Error receiving data: %d\n", response); - } else { - printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); - } - - // Close the socket to return its memory and bring down the network interface - socket.close(); -} - int main() { - int count = 0; - printf("WiFi example\n"); #ifdef MBED_MAJOR_VERSION @@ -139,9 +86,9 @@ return -1; } - count = scan_demo(wifi); + int count = scan_demo(wifi); if (count == 0) { - printf("No WIFI APNs found - can't continue further.\n"); + printf("No WIFI APs found - can't continue further.\n"); return -1; } @@ -159,8 +106,6 @@ printf("Gateway: %s\n", wifi->get_gateway()); printf("RSSI: %d\n\n", wifi->get_rssi()); - http_demo(wifi); - wifi->disconnect(); printf("\nDone\n");