simple wifi test program. Will connect to an HTTP server for testing.

Fork of mbed-os-example-wifi-adv-wise-1530 by Alan Chuang

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 #include "TCPSocket.h"
00019 
00020 WiFiInterface *wifi;
00021 
00022 const char *sec2str(nsapi_security_t sec)
00023 {
00024     switch (sec) {
00025         case NSAPI_SECURITY_NONE:
00026             return "None";
00027         case NSAPI_SECURITY_WEP:
00028             return "WEP";
00029         case NSAPI_SECURITY_WPA:
00030             return "WPA";
00031         case NSAPI_SECURITY_WPA2:
00032             return "WPA2";
00033         case NSAPI_SECURITY_WPA_WPA2:
00034             return "WPA/WPA2";
00035         case NSAPI_SECURITY_UNKNOWN:
00036         default:
00037             return "Unknown";
00038     }
00039 }
00040 
00041 int scan_demo(WiFiInterface *wifi)
00042 {
00043     WiFiAccessPoint *ap;
00044 
00045     printf("Scan:\n");
00046 
00047     int count = wifi->scan(NULL,0);
00048 
00049     if (count <= 0) {
00050         printf("scan() failed with return value: %d\n", count);
00051         return 0;
00052     }
00053 
00054     /* Limit number of network arbitrary to 15 */
00055     count = count < 15 ? count : 15;
00056 
00057     ap = new WiFiAccessPoint[count];
00058     count = wifi->scan(ap, count);
00059 
00060     if (count <= 0) {
00061         printf("scan() failed with return value: %d\n", count);
00062         return 0;
00063     }
00064 
00065     for (int i = 0; i < count; i++) {
00066         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00067                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00068                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00069     }
00070     printf("%d networks available.\n", count);
00071 
00072     delete[] ap;
00073     return count;
00074 }
00075 
00076 void http_demo(NetworkInterface *net)
00077 {
00078     TCPSocket socket;
00079     nsapi_error_t response;
00080 
00081     printf("Sending HTTP request to www.arm.com...\n");
00082 
00083     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00084     response = socket.open(net);
00085     if(0 != response) {
00086         printf("socket.open() failed: %d\n", response);
00087         return;
00088     }
00089 
00090     response = socket.connect("api.ipify.org", 80);
00091     if(0 != response) {
00092         printf("Error connecting: %d\n", response);
00093         socket.close();
00094         return;
00095     }
00096 
00097     // Send a simple http request
00098     char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
00099     nsapi_size_t size = strlen(sbuffer);
00100 
00101     // Loop until whole request send
00102     while(size) {
00103         response = socket.send(sbuffer+response, size);
00104         if (response < 0) {
00105             printf("Error sending data: %d\n", response);
00106             socket.close();
00107             return;
00108         }
00109         size -= response;
00110         printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00111     }
00112 
00113     // Receieve a simple http response and print out the response line
00114     char rbuffer[64];
00115     response = socket.recv(rbuffer, sizeof rbuffer);
00116     if (response < 0) {
00117         printf("Error receiving data: %d\n", response);
00118     } else {
00119         printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00120     }
00121 
00122     // Close the socket to return its memory and bring down the network interface
00123     socket.close();
00124 }
00125 
00126 int main()
00127 {
00128     int count = 0;
00129 
00130     printf("WiFi example\n");
00131 
00132 #ifdef MBED_MAJOR_VERSION
00133     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00134 #endif
00135 
00136     wifi = WiFiInterface::get_default_instance();
00137     if (!wifi) {
00138         printf("ERROR: No WiFiInterface found.\n");
00139         return -1;
00140     }
00141 
00142     count = scan_demo(wifi);
00143     if (count == 0) {
00144         printf("No WIFI APNs found - can't continue further.\n");
00145         return -1;
00146     }
00147 
00148     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00149     int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00150     if (ret != 0) {
00151         printf("\nConnection error: %d\n", ret);
00152         return -1;
00153     }
00154 
00155     printf("Success\n\n");
00156     printf("MAC: %s\n", wifi->get_mac_address());
00157     printf("IP: %s\n", wifi->get_ip_address());
00158     printf("Netmask: %s\n", wifi->get_netmask());
00159     printf("Gateway: %s\n", wifi->get_gateway());
00160     printf("RSSI: %d\n\n", wifi->get_rssi());
00161 
00162     http_demo(wifi);
00163 
00164     wifi->disconnect();
00165 
00166     printf("\nDone\n");
00167 }