Farnell-Element14 Bologna IOT Team / Mbed OS mbed-os-example-wifi

Fork of mbed-os-example-wifi by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2018 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 #define WIFI_IDW0XX1    2
00021 
00022 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
00023 #include "ISM43362Interface.h"
00024 ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false);
00025 
00026 #else // External WiFi modules
00027 
00028 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
00029 #include "SpwfSAInterface.h"
00030 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00031 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
00032 
00033 #endif
00034 
00035 const char *sec2str(nsapi_security_t sec)
00036 {
00037     switch (sec) {
00038         case NSAPI_SECURITY_NONE:
00039             return "None";
00040         case NSAPI_SECURITY_WEP:
00041             return "WEP";
00042         case NSAPI_SECURITY_WPA:
00043             return "WPA";
00044         case NSAPI_SECURITY_WPA2:
00045             return "WPA2";
00046         case NSAPI_SECURITY_WPA_WPA2:
00047             return "WPA/WPA2";
00048         case NSAPI_SECURITY_UNKNOWN:
00049         default:
00050             return "Unknown";
00051     }
00052 }
00053 
00054 int scan_demo(WiFiInterface *wifi)
00055 {
00056     WiFiAccessPoint *ap;
00057 
00058     printf("Scan:\n");
00059 
00060     int count = wifi->scan(NULL,0);
00061     printf("%d networks available.\n", count);
00062 
00063     /* Limit number of network arbitrary to 15 */
00064     count = count < 15 ? count : 15;
00065 
00066     ap = new WiFiAccessPoint[count];
00067     count = wifi->scan(ap, count);
00068     for (int i = 0; i < count; i++)
00069     {
00070         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00071                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00072                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00073     }
00074 
00075     delete[] ap;
00076     return count;
00077 }
00078 
00079 void http_demo(NetworkInterface *net)
00080 {
00081     TCPSocket socket;
00082     nsapi_error_t response;
00083 
00084     printf("Sending HTTP request to www.arm.com...\n");
00085 
00086     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00087     socket.open(net);
00088     response = socket.connect("www.arm.com", 80);
00089     if(0 != response) {
00090         printf("Error connecting: %d\n", response);
00091         socket.close();
00092         return;
00093     }
00094 
00095     // Send a simple http request
00096     char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
00097     nsapi_size_t size = strlen(sbuffer);
00098     response = 0;
00099     while(size)
00100     {
00101         response = socket.send(sbuffer+response, size);
00102         if (response < 0) {
00103             printf("Error sending data: %d\n", response);
00104             socket.close();
00105             return;
00106         } else {
00107             size -= response;
00108             // Check if entire message was sent or not
00109             printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00110         }
00111     }
00112 
00113     // Recieve 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 Serial pc(USBTX, USBRX);
00127 
00128 int main()
00129 {
00130     int count = 0;
00131 
00132     pc.baud(115200); // settagggio espliciti porta seriale
00133     
00134     printf("WiFi example\n\n");
00135 
00136     count = scan_demo(&wifi);
00137     if (count == 0) {
00138         printf("No WIFI APNs found - can't continue further.\n");
00139         return -1;
00140     }
00141 
00142     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00143     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00144     if (ret != 0) {
00145         printf("\nConnection error\n");
00146         return -1;
00147     }
00148 
00149     printf("Success\n\n");
00150     printf("MAC: %s\n", wifi.get_mac_address());
00151     printf("IP: %s\n", wifi.get_ip_address());
00152     printf("Netmask: %s\n", wifi.get_netmask());
00153     printf("Gateway: %s\n", wifi.get_gateway());
00154     printf("RSSI: %d\n\n", wifi.get_rssi());
00155 
00156     http_demo(&wifi);
00157 
00158     wifi.disconnect();
00159 
00160     printf("\nDone\n");
00161 }