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

Dependencies:   C12832

Committer:
CJW
Date:
Sat Mar 10 15:00:36 2018 +0000
Revision:
57:921d2524844d
Parent:
56:bea82468efd3
Revert to using the LCD screen

Who changed what in which revision?

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