
WiFi example for mbed OS, fork from https://github.com/ARMmbed/mbed-os-example-wifi
This is an example of a Wi-Fi application using the Wi-Fi and network socket APIs that Mbed OS provides.
The program brings up the Wi-Fi and the underlying network interface and uses it to scan available networks, connects to a network, prints interface and connection details and performs an HTTP operation.
For more information about Wi-Fi APIs, please visit the Mbed OS Wi-Fi documentation.
This example has been used on the following platforms
main.cpp@1:67275d54581f, 2018-02-22 (annotated)
- Committer:
- dkato
- Date:
- Thu Feb 22 08:32:38 2018 +0000
- Revision:
- 1:67275d54581f
- Parent:
- 0:0593b97dec0b
- Child:
- 5:01be82512f7a
Update main.cpp / mbed_app.json
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 1 | /* WiFi Example |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 2 | * Copyright (c) 2016 ARM Limited |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 3 | * |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 5 | * you may not use this file except in compliance with the License. |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 6 | * You may obtain a copy of the License at |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 7 | * |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 9 | * |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 10 | * Unless required by applicable law or agreed to in writing, software |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 13 | * See the License for the specific language governing permissions and |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 14 | * limitations under the License. |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 15 | */ |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 16 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 17 | #include "mbed.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 18 | #include "TCPSocket.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 19 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 20 | #define WIFI_ESP8266 1 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 21 | #define WIFI_IDW0XX1 2 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 22 | #define WIFI_ESP32 3 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 23 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 24 | #if TARGET_UBLOX_EVK_ODIN_W2 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 25 | #include "OdinWiFiInterface.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 26 | OdinWiFiInterface wifi; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 27 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 28 | #elif TARGET_REALTEK_RTL8195AM |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 29 | #include "RTWInterface.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 30 | RTWInterface wifi; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 31 | |
dkato | 1:67275d54581f | 32 | #elif TARGET_GR_LYCHEE |
dkato | 1:67275d54581f | 33 | #include "ESP32Interface.h" |
dkato | 1:67275d54581f | 34 | ESP32Interface wifi(P5_3, P3_14, P7_1, P0_1); |
dkato | 1:67275d54581f | 35 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 36 | #else // External WiFi modules |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 37 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 38 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 39 | #include "ESP8266Interface.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 40 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 41 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 42 | #include "SpwfSAInterface.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 43 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 44 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP32 |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 45 | #include "ESP32Interface.h" |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 46 | ESP32Interface wifi(MBED_CONF_APP_WIFI_EN, MBED_CONF_APP_WIFI_IO0, MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 47 | #endif |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 48 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 49 | #endif |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 50 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 51 | const char *sec2str(nsapi_security_t sec) |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 52 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 53 | switch (sec) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 54 | case NSAPI_SECURITY_NONE: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 55 | return "None"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 56 | case NSAPI_SECURITY_WEP: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 57 | return "WEP"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 58 | case NSAPI_SECURITY_WPA: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 59 | return "WPA"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 60 | case NSAPI_SECURITY_WPA2: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 61 | return "WPA2"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 62 | case NSAPI_SECURITY_WPA_WPA2: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 63 | return "WPA/WPA2"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 64 | case NSAPI_SECURITY_UNKNOWN: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 65 | default: |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 66 | return "Unknown"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 67 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 68 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 69 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 70 | int scan_demo(WiFiInterface *wifi) |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 71 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 72 | WiFiAccessPoint *ap; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 73 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 74 | printf("Scan:\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 75 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 76 | int count = wifi->scan(NULL,0); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 77 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 78 | /* Limit number of network arbitrary to 15 */ |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 79 | count = count < 15 ? count : 15; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 80 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 81 | ap = new WiFiAccessPoint[count]; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 82 | count = wifi->scan(ap, count); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 83 | for (int i = 0; i < count; i++) |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 84 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 85 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(), |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 86 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 87 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 88 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 89 | printf("%d networks available.\n", count); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 90 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 91 | delete[] ap; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 92 | return count; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 93 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 94 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 95 | void http_demo(NetworkInterface *net) |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 96 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 97 | TCPSocket socket; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 98 | nsapi_error_t response; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 99 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 100 | printf("Sending HTTP request to www.arm.com...\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 101 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 102 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 103 | socket.open(net); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 104 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 105 | response = socket.connect("www.arm.com", 80); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 106 | if(0 != response) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 107 | printf("Error connecting: %d\n", response); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 108 | socket.close(); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 109 | return; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 110 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 111 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 112 | // Send a simple http request |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 113 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 114 | nsapi_size_t size = strlen(sbuffer); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 115 | response = 0; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 116 | while(size) |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 117 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 118 | response = socket.send(sbuffer+response, size); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 119 | if (response < 0) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 120 | printf("Error sending data: %d\n", response); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 121 | socket.close(); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 122 | return; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 123 | } else { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 124 | size -= response; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 125 | // Check if entire message was sent or not |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 126 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 127 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 128 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 129 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 130 | // Recieve a simple http response and print out the response line |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 131 | char rbuffer[64]; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 132 | response = socket.recv(rbuffer, sizeof rbuffer); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 133 | if (response < 0) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 134 | printf("Error receiving data: %d\n", response); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 135 | } else { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 136 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 137 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 138 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 139 | // Close the socket to return its memory and bring down the network interface |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 140 | socket.close(); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 141 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 142 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 143 | int main() |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 144 | { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 145 | int count = 0; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 146 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 147 | printf("WiFi example\n\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 148 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 149 | count = scan_demo(&wifi); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 150 | if (count == 0) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 151 | printf("No WIFI APNs found - can't continue further.\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 152 | return -1; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 153 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 154 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 155 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 156 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 157 | if (ret != 0) { |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 158 | printf("\nConnection error\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 159 | return -1; |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 160 | } |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 161 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 162 | printf("Success\n\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 163 | printf("MAC: %s\n", wifi.get_mac_address()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 164 | printf("IP: %s\n", wifi.get_ip_address()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 165 | printf("Netmask: %s\n", wifi.get_netmask()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 166 | printf("Gateway: %s\n", wifi.get_gateway()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 167 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 168 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 169 | http_demo(&wifi); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 170 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 171 | wifi.disconnect(); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 172 | |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 173 | printf("\nDone\n"); |
group-Renesas-Electronics-Corp | 0:0593b97dec0b | 174 | } |