Demonstrates how to import classes and libraries into a simple application. The original Mbed OS 5 wifi example has been imported and configured for the ST IDW04A1

Dependencies:   wifi-x-nucleo-idw01m1

Committer:
timo_k2
Date:
Wed May 20 15:57:51 2020 +0000
Revision:
0:ce9de1636bd7
Demonstrates how to import classes and libraries to a simple application. The original mbed OS5 wifi -example library has been imported and configured for the ST IDW04A1 Wifi-module.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:ce9de1636bd7 1 /* WiFi Example
timo_k2 0:ce9de1636bd7 2 * Copyright (c) 2018 ARM Limited
timo_k2 0:ce9de1636bd7 3 *
timo_k2 0:ce9de1636bd7 4 * Licensed under the Apache License, Version 2.0 (the "License");
timo_k2 0:ce9de1636bd7 5 * you may not use this file except in compliance with the License.
timo_k2 0:ce9de1636bd7 6 * You may obtain a copy of the License at
timo_k2 0:ce9de1636bd7 7 *
timo_k2 0:ce9de1636bd7 8 * http://www.apache.org/licenses/LICENSE-2.0
timo_k2 0:ce9de1636bd7 9 *
timo_k2 0:ce9de1636bd7 10 * Unless required by applicable law or agreed to in writing, software
timo_k2 0:ce9de1636bd7 11 * distributed under the License is distributed on an "AS IS" BASIS,
timo_k2 0:ce9de1636bd7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
timo_k2 0:ce9de1636bd7 13 * See the License for the specific language governing permissions and
timo_k2 0:ce9de1636bd7 14 * limitations under the License.
timo_k2 0:ce9de1636bd7 15 */
timo_k2 0:ce9de1636bd7 16
timo_k2 0:ce9de1636bd7 17 #include "mbed.h"
timo_k2 0:ce9de1636bd7 18 #include "TCPSocket.h"
timo_k2 0:ce9de1636bd7 19
timo_k2 0:ce9de1636bd7 20 #define WIFI_IDW0XX1 2
timo_k2 0:ce9de1636bd7 21
timo_k2 0:ce9de1636bd7 22 #if (defined(TARGET_DISCO_L475VG_IOT01A) || defined(TARGET_DISCO_F413ZH))
timo_k2 0:ce9de1636bd7 23 #include "ISM43362Interface.h"
timo_k2 0:ce9de1636bd7 24 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);
timo_k2 0:ce9de1636bd7 25
timo_k2 0:ce9de1636bd7 26 #else // External WiFi modules
timo_k2 0:ce9de1636bd7 27
timo_k2 0:ce9de1636bd7 28 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
timo_k2 0:ce9de1636bd7 29 #include "SpwfSAInterface.h"
timo_k2 0:ce9de1636bd7 30 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
timo_k2 0:ce9de1636bd7 31 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
timo_k2 0:ce9de1636bd7 32
timo_k2 0:ce9de1636bd7 33 #endif
timo_k2 0:ce9de1636bd7 34
timo_k2 0:ce9de1636bd7 35 const char *sec2str(nsapi_security_t sec)
timo_k2 0:ce9de1636bd7 36 {
timo_k2 0:ce9de1636bd7 37 switch (sec) {
timo_k2 0:ce9de1636bd7 38 case NSAPI_SECURITY_NONE:
timo_k2 0:ce9de1636bd7 39 return "None";
timo_k2 0:ce9de1636bd7 40 case NSAPI_SECURITY_WEP:
timo_k2 0:ce9de1636bd7 41 return "WEP";
timo_k2 0:ce9de1636bd7 42 case NSAPI_SECURITY_WPA:
timo_k2 0:ce9de1636bd7 43 return "WPA";
timo_k2 0:ce9de1636bd7 44 case NSAPI_SECURITY_WPA2:
timo_k2 0:ce9de1636bd7 45 return "WPA2";
timo_k2 0:ce9de1636bd7 46 case NSAPI_SECURITY_WPA_WPA2:
timo_k2 0:ce9de1636bd7 47 return "WPA/WPA2";
timo_k2 0:ce9de1636bd7 48 case NSAPI_SECURITY_UNKNOWN:
timo_k2 0:ce9de1636bd7 49 default:
timo_k2 0:ce9de1636bd7 50 return "Unknown";
timo_k2 0:ce9de1636bd7 51 }
timo_k2 0:ce9de1636bd7 52 }
timo_k2 0:ce9de1636bd7 53
timo_k2 0:ce9de1636bd7 54 int scan_demo(WiFiInterface *wifi)
timo_k2 0:ce9de1636bd7 55 {
timo_k2 0:ce9de1636bd7 56 WiFiAccessPoint *ap;
timo_k2 0:ce9de1636bd7 57
timo_k2 0:ce9de1636bd7 58 printf("Scan:\n");
timo_k2 0:ce9de1636bd7 59
timo_k2 0:ce9de1636bd7 60 int count = wifi->scan(NULL,0);
timo_k2 0:ce9de1636bd7 61 printf("%d networks available.\n", count);
timo_k2 0:ce9de1636bd7 62
timo_k2 0:ce9de1636bd7 63 /* Limit number of network arbitrary to 15 */
timo_k2 0:ce9de1636bd7 64 count = count < 15 ? count : 15;
timo_k2 0:ce9de1636bd7 65
timo_k2 0:ce9de1636bd7 66 ap = new WiFiAccessPoint[count];
timo_k2 0:ce9de1636bd7 67 count = wifi->scan(ap, count);
timo_k2 0:ce9de1636bd7 68 for (int i = 0; i < count; i++)
timo_k2 0:ce9de1636bd7 69 {
timo_k2 0:ce9de1636bd7 70 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
timo_k2 0:ce9de1636bd7 71 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
timo_k2 0:ce9de1636bd7 72 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
timo_k2 0:ce9de1636bd7 73 }
timo_k2 0:ce9de1636bd7 74
timo_k2 0:ce9de1636bd7 75 delete[] ap;
timo_k2 0:ce9de1636bd7 76 return count;
timo_k2 0:ce9de1636bd7 77 }
timo_k2 0:ce9de1636bd7 78
timo_k2 0:ce9de1636bd7 79 void http_demo(NetworkInterface *net)
timo_k2 0:ce9de1636bd7 80 {
timo_k2 0:ce9de1636bd7 81 TCPSocket socket;
timo_k2 0:ce9de1636bd7 82 nsapi_error_t response;
timo_k2 0:ce9de1636bd7 83
timo_k2 0:ce9de1636bd7 84 printf("Sending HTTP request to www.arm.com...\n");
timo_k2 0:ce9de1636bd7 85
timo_k2 0:ce9de1636bd7 86 // Open a socket on the network interface, and create a TCP connection to www.arm.com
timo_k2 0:ce9de1636bd7 87 socket.open(net);
timo_k2 0:ce9de1636bd7 88 response = socket.connect("www.arm.com", 80);
timo_k2 0:ce9de1636bd7 89 if(0 != response) {
timo_k2 0:ce9de1636bd7 90 printf("Error connecting: %d\n", response);
timo_k2 0:ce9de1636bd7 91 socket.close();
timo_k2 0:ce9de1636bd7 92 return;
timo_k2 0:ce9de1636bd7 93 }
timo_k2 0:ce9de1636bd7 94
timo_k2 0:ce9de1636bd7 95 // Send a simple http request
timo_k2 0:ce9de1636bd7 96 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
timo_k2 0:ce9de1636bd7 97 nsapi_size_t size = strlen(sbuffer);
timo_k2 0:ce9de1636bd7 98 response = 0;
timo_k2 0:ce9de1636bd7 99 while(size)
timo_k2 0:ce9de1636bd7 100 {
timo_k2 0:ce9de1636bd7 101 response = socket.send(sbuffer+response, size);
timo_k2 0:ce9de1636bd7 102 if (response < 0) {
timo_k2 0:ce9de1636bd7 103 printf("Error sending data: %d\n", response);
timo_k2 0:ce9de1636bd7 104 socket.close();
timo_k2 0:ce9de1636bd7 105 return;
timo_k2 0:ce9de1636bd7 106 } else {
timo_k2 0:ce9de1636bd7 107 size -= response;
timo_k2 0:ce9de1636bd7 108 // Check if entire message was sent or not
timo_k2 0:ce9de1636bd7 109 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
timo_k2 0:ce9de1636bd7 110 }
timo_k2 0:ce9de1636bd7 111 }
timo_k2 0:ce9de1636bd7 112
timo_k2 0:ce9de1636bd7 113 // Recieve a simple http response and print out the response line
timo_k2 0:ce9de1636bd7 114 char rbuffer[64];
timo_k2 0:ce9de1636bd7 115 response = socket.recv(rbuffer, sizeof rbuffer);
timo_k2 0:ce9de1636bd7 116 if (response < 0) {
timo_k2 0:ce9de1636bd7 117 printf("Error receiving data: %d\n", response);
timo_k2 0:ce9de1636bd7 118 } else {
timo_k2 0:ce9de1636bd7 119 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
timo_k2 0:ce9de1636bd7 120 }
timo_k2 0:ce9de1636bd7 121
timo_k2 0:ce9de1636bd7 122 // Close the socket to return its memory and bring down the network interface
timo_k2 0:ce9de1636bd7 123 socket.close();
timo_k2 0:ce9de1636bd7 124 }
timo_k2 0:ce9de1636bd7 125
timo_k2 0:ce9de1636bd7 126 int main()
timo_k2 0:ce9de1636bd7 127 {
timo_k2 0:ce9de1636bd7 128 int count = 0;
timo_k2 0:ce9de1636bd7 129
timo_k2 0:ce9de1636bd7 130 printf("WiFi example\n\n");
timo_k2 0:ce9de1636bd7 131
timo_k2 0:ce9de1636bd7 132 count = scan_demo(&wifi);
timo_k2 0:ce9de1636bd7 133 if (count == 0) {
timo_k2 0:ce9de1636bd7 134 printf("No WIFI APNs found - can't continue further.\n");
timo_k2 0:ce9de1636bd7 135 return -1;
timo_k2 0:ce9de1636bd7 136 }
timo_k2 0:ce9de1636bd7 137
timo_k2 0:ce9de1636bd7 138 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
timo_k2 0:ce9de1636bd7 139 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
timo_k2 0:ce9de1636bd7 140 if (ret != 0) {
timo_k2 0:ce9de1636bd7 141 printf("\nConnection error\n");
timo_k2 0:ce9de1636bd7 142 return -1;
timo_k2 0:ce9de1636bd7 143 }
timo_k2 0:ce9de1636bd7 144
timo_k2 0:ce9de1636bd7 145 printf("Success\n\n");
timo_k2 0:ce9de1636bd7 146 printf("MAC: %s\n", wifi.get_mac_address());
timo_k2 0:ce9de1636bd7 147 printf("IP: %s\n", wifi.get_ip_address());
timo_k2 0:ce9de1636bd7 148 printf("Netmask: %s\n", wifi.get_netmask());
timo_k2 0:ce9de1636bd7 149 printf("Gateway: %s\n", wifi.get_gateway());
timo_k2 0:ce9de1636bd7 150 printf("RSSI: %d\n\n", wifi.get_rssi());
timo_k2 0:ce9de1636bd7 151
timo_k2 0:ce9de1636bd7 152 http_demo(&wifi);
timo_k2 0:ce9de1636bd7 153
timo_k2 0:ce9de1636bd7 154 wifi.disconnect();
timo_k2 0:ce9de1636bd7 155
timo_k2 0:ce9de1636bd7 156 printf("\nDone\n");
timo_k2 0:ce9de1636bd7 157 }