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

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

Committer:
chuanga
Date:
Fri Feb 22 13:13:10 2019 +0800
Revision:
0:3c5e2cc8251b
adding wifi example program for wise-1530 wifi module

Who changed what in which revision?

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