A quick example of a simple WiFi application using the WiFi and network-socket APIs that is provided as a part of mbed-os.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 
00019 WiFiInterface *wifi;
00020 
00021 const char *sec2str(nsapi_security_t sec)
00022 {
00023     switch (sec) {
00024         case NSAPI_SECURITY_NONE:
00025             return "None";
00026         case NSAPI_SECURITY_WEP:
00027             return "WEP";
00028         case NSAPI_SECURITY_WPA:
00029             return "WPA";
00030         case NSAPI_SECURITY_WPA2:
00031             return "WPA2";
00032         case NSAPI_SECURITY_WPA_WPA2:
00033             return "WPA/WPA2";
00034         case NSAPI_SECURITY_UNKNOWN:
00035         default:
00036             return "Unknown";
00037     }
00038 }
00039 
00040 int scan_demo(WiFiInterface *wifi)
00041 {
00042     WiFiAccessPoint *ap;
00043 
00044     printf("Scan:\n");
00045 
00046     int count = wifi->scan(NULL,0);
00047 
00048     if (count <= 0) {
00049         printf("scan() failed with return value: %d\n", count);
00050         return 0;
00051     }
00052 
00053     /* Limit number of network arbitrary to 15 */
00054     count = count < 15 ? count : 15;
00055 
00056     ap = new WiFiAccessPoint[count];
00057     count = wifi->scan(ap, count);
00058 
00059     if (count <= 0) {
00060         printf("scan() failed with return value: %d\n", count);
00061         return 0;
00062     }
00063 
00064     for (int i = 0; i < count; i++) {
00065         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00066                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00067                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00068     }
00069     printf("%d networks available.\n", count);
00070 
00071     delete[] ap;
00072     return count;
00073 }
00074 
00075 int main()
00076 {
00077     printf("WiFi example\n");
00078 
00079 #ifdef MBED_MAJOR_VERSION
00080     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00081 #endif
00082 
00083     wifi = WiFiInterface::get_default_instance();
00084     if (!wifi) {
00085         printf("ERROR: No WiFiInterface found.\n");
00086         return -1;
00087     }
00088 
00089     int count = scan_demo(wifi);
00090     if (count == 0) {
00091         printf("No WIFI APs found - can't continue further.\n");
00092         return -1;
00093     }
00094 
00095     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00096     int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00097     if (ret != 0) {
00098         printf("\nConnection error: %d\n", ret);
00099         return -1;
00100     }
00101 
00102     printf("Success\n\n");
00103     printf("MAC: %s\n", wifi->get_mac_address());
00104     printf("IP: %s\n", wifi->get_ip_address());
00105     printf("Netmask: %s\n", wifi->get_netmask());
00106     printf("Gateway: %s\n", wifi->get_gateway());
00107     printf("RSSI: %d\n\n", wifi->get_rssi());
00108 
00109     wifi->disconnect();
00110 
00111     printf("\nDone\n");
00112 }