87ty78ot7iyu

Dependencies:   HCSR04

Committer:
mbed_official
Date:
Tue Jul 24 15:15:02 2018 +0100
Revision:
71:a0fbcc153b55
Parent:
67:ebff4a8d228d
Child:
72:b4761c52cc91
Update application to support default interfaces from Mbed OS 5.9.x

.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi

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"
mbed_official 0:857719181846 19
mbed_official 71:a0fbcc153b55 20 #define internal 1
mbed_official 71:a0fbcc153b55 21 #define WIFI_ESP8266 2
mbed_official 71:a0fbcc153b55 22 #define WIFI_IDW0XX1 3
mbed_official 71:a0fbcc153b55 23 #define WIFI_ISM43362 4
mbed_official 32:bca3f5f442b3 24
mbed_official 71:a0fbcc153b55 25 WiFiInterface *wifi;
mbed_official 21:b2f2f6a840b4 26
mbed_official 32:bca3f5f442b3 27 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
mbed_official 71:a0fbcc153b55 28
mbed_official 1:aea78e21a7da 29 #include "ESP8266Interface.h"
mbed_official 71:a0fbcc153b55 30
mbed_official 71:a0fbcc153b55 31 WiFiInterface *WiFiInterface::get_default_instance() {
mbed_official 71:a0fbcc153b55 32 static ESP8266Interface esp(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 71:a0fbcc153b55 33 return &esp;
mbed_official 71:a0fbcc153b55 34 }
mbed_official 63:99e063d738ee 35
mbed_official 63:99e063d738ee 36 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_ISM43362
mbed_official 71:a0fbcc153b55 37
mbed_official 63:99e063d738ee 38 #include "ISM43362Interface.h"
mbed_official 71:a0fbcc153b55 39
mbed_official 71:a0fbcc153b55 40 WiFiInterface *WiFiInterface::get_default_instance() {
mbed_official 71:a0fbcc153b55 41 static ISM43362Interface ism;
mbed_official 71:a0fbcc153b55 42 return &ism;
mbed_official 71:a0fbcc153b55 43 }
mbed_official 63:99e063d738ee 44
mbed_official 44:63be19b7a3db 45 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 71:a0fbcc153b55 46
mbed_official 32:bca3f5f442b3 47 #include "SpwfSAInterface.h"
mbed_official 63:99e063d738ee 48
mbed_official 71:a0fbcc153b55 49 WiFiInterface *WiFiInterface::get_default_instance() {
mbed_official 71:a0fbcc153b55 50 static SpwfSAInterface spwf(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 71:a0fbcc153b55 51 return &spwf;
mbed_official 71:a0fbcc153b55 52 }
mbed_official 10:5b5beb106156 53
mbed_official 33:12f0df4d51d7 54 #endif
mbed_official 0:857719181846 55
mbed_official 71:a0fbcc153b55 56
mbed_official 0:857719181846 57 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 58 {
mbed_official 0:857719181846 59 switch (sec) {
mbed_official 0:857719181846 60 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 61 return "None";
mbed_official 0:857719181846 62 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 63 return "WEP";
mbed_official 0:857719181846 64 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 65 return "WPA";
mbed_official 0:857719181846 66 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 67 return "WPA2";
mbed_official 0:857719181846 68 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 69 return "WPA/WPA2";
mbed_official 0:857719181846 70 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 71 default:
mbed_official 0:857719181846 72 return "Unknown";
mbed_official 0:857719181846 73 }
mbed_official 0:857719181846 74 }
mbed_official 0:857719181846 75
mbed_official 37:3a31525e2971 76 int scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 77 {
mbed_official 0:857719181846 78 WiFiAccessPoint *ap;
mbed_official 0:857719181846 79
mbed_official 35:052c1ba06ce7 80 printf("Scan:\n");
mbed_official 0:857719181846 81
mbed_official 0:857719181846 82 int count = wifi->scan(NULL,0);
mbed_official 0:857719181846 83
mbed_official 66:2cf02c7d430c 84 if (count <= 0) {
mbed_official 66:2cf02c7d430c 85 printf("scan() failed with return value: %d\n", count);
mbed_official 66:2cf02c7d430c 86 return 0;
mbed_official 66:2cf02c7d430c 87 }
mbed_official 66:2cf02c7d430c 88
mbed_official 0:857719181846 89 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 90 count = count < 15 ? count : 15;
mbed_official 0:857719181846 91
mbed_official 0:857719181846 92 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 93 count = wifi->scan(ap, count);
mbed_official 66:2cf02c7d430c 94
mbed_official 66:2cf02c7d430c 95 if (count <= 0) {
mbed_official 66:2cf02c7d430c 96 printf("scan() failed with return value: %d\n", count);
mbed_official 66:2cf02c7d430c 97 return 0;
mbed_official 66:2cf02c7d430c 98 }
mbed_official 66:2cf02c7d430c 99
mbed_official 66:2cf02c7d430c 100 for (int i = 0; i < count; i++) {
mbed_official 35:052c1ba06ce7 101 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
mbed_official 0:857719181846 102 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
mbed_official 0:857719181846 103 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 104 }
mbed_official 35:052c1ba06ce7 105 printf("%d networks available.\n", count);
mbed_official 0:857719181846 106
mbed_official 0:857719181846 107 delete[] ap;
mbed_official 37:3a31525e2971 108 return count;
mbed_official 0:857719181846 109 }
mbed_official 0:857719181846 110
mbed_official 0:857719181846 111 void http_demo(NetworkInterface *net)
mbed_official 0:857719181846 112 {
mbed_official 0:857719181846 113 TCPSocket socket;
mbed_official 24:323569a565ec 114 nsapi_error_t response;
mbed_official 0:857719181846 115
mbed_official 35:052c1ba06ce7 116 printf("Sending HTTP request to www.arm.com...\n");
mbed_official 0:857719181846 117
mbed_official 0:857719181846 118 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mbed_official 66:2cf02c7d430c 119 response = socket.open(net);
mbed_official 66:2cf02c7d430c 120 if(0 != response) {
mbed_official 66:2cf02c7d430c 121 printf("socket.open() failed: %d\n", response);
mbed_official 66:2cf02c7d430c 122 return;
mbed_official 66:2cf02c7d430c 123 }
mbed_official 66:2cf02c7d430c 124
mbed_official 66:2cf02c7d430c 125 response = socket.connect("api.ipify.org", 80);
mbed_official 24:323569a565ec 126 if(0 != response) {
mbed_official 35:052c1ba06ce7 127 printf("Error connecting: %d\n", response);
mbed_official 24:323569a565ec 128 socket.close();
mbed_official 24:323569a565ec 129 return;
mbed_official 24:323569a565ec 130 }
mbed_official 0:857719181846 131
mbed_official 0:857719181846 132 // Send a simple http request
mbed_official 66:2cf02c7d430c 133 char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
mbed_official 33:12f0df4d51d7 134 nsapi_size_t size = strlen(sbuffer);
mbed_official 66:2cf02c7d430c 135
mbed_official 66:2cf02c7d430c 136 // Loop until whole request send
mbed_official 66:2cf02c7d430c 137 while(size) {
mbed_official 24:323569a565ec 138 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 139 if (response < 0) {
mbed_official 35:052c1ba06ce7 140 printf("Error sending data: %d\n", response);
mbed_official 24:323569a565ec 141 socket.close();
mbed_official 24:323569a565ec 142 return;
mbed_official 24:323569a565ec 143 }
mbed_official 66:2cf02c7d430c 144 size -= response;
mbed_official 66:2cf02c7d430c 145 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 146 }
mbed_official 0:857719181846 147
mbed_official 66:2cf02c7d430c 148 // Receieve a simple http response and print out the response line
mbed_official 0:857719181846 149 char rbuffer[64];
mbed_official 24:323569a565ec 150 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 151 if (response < 0) {
mbed_official 35:052c1ba06ce7 152 printf("Error receiving data: %d\n", response);
mbed_official 24:323569a565ec 153 } else {
mbed_official 35:052c1ba06ce7 154 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 155 }
mbed_official 0:857719181846 156
mbed_official 0:857719181846 157 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 158 socket.close();
mbed_official 0:857719181846 159 }
mbed_official 0:857719181846 160
mbed_official 0:857719181846 161 int main()
mbed_official 0:857719181846 162 {
mbed_official 37:3a31525e2971 163 int count = 0;
mbed_official 37:3a31525e2971 164
mbed_official 67:ebff4a8d228d 165 printf("WiFi example\n");
mbed_official 67:ebff4a8d228d 166
mbed_official 67:ebff4a8d228d 167 #ifdef MBED_MAJOR_VERSION
mbed_official 67:ebff4a8d228d 168 printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
mbed_official 67:ebff4a8d228d 169 #endif
mbed_official 0:857719181846 170
mbed_official 71:a0fbcc153b55 171 wifi = WiFiInterface::get_default_instance();
mbed_official 71:a0fbcc153b55 172
mbed_official 71:a0fbcc153b55 173 count = scan_demo(wifi);
mbed_official 37:3a31525e2971 174 if (count == 0) {
mbed_official 37:3a31525e2971 175 printf("No WIFI APNs found - can't continue further.\n");
mbed_official 37:3a31525e2971 176 return -1;
mbed_official 37:3a31525e2971 177 }
mbed_official 0:857719181846 178
mbed_official 37:3a31525e2971 179 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 71:a0fbcc153b55 180 int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 181 if (ret != 0) {
mbed_official 66:2cf02c7d430c 182 printf("\nConnection error: %d\n", ret);
mbed_official 0:857719181846 183 return -1;
mbed_official 0:857719181846 184 }
mbed_official 0:857719181846 185
mbed_official 35:052c1ba06ce7 186 printf("Success\n\n");
mbed_official 71:a0fbcc153b55 187 printf("MAC: %s\n", wifi->get_mac_address());
mbed_official 71:a0fbcc153b55 188 printf("IP: %s\n", wifi->get_ip_address());
mbed_official 71:a0fbcc153b55 189 printf("Netmask: %s\n", wifi->get_netmask());
mbed_official 71:a0fbcc153b55 190 printf("Gateway: %s\n", wifi->get_gateway());
mbed_official 71:a0fbcc153b55 191 printf("RSSI: %d\n\n", wifi->get_rssi());
mbed_official 0:857719181846 192
mbed_official 71:a0fbcc153b55 193 http_demo(wifi);
mbed_official 71:a0fbcc153b55 194
mbed_official 71:a0fbcc153b55 195 wifi->disconnect();
mbed_official 0:857719181846 196
mbed_official 71:a0fbcc153b55 197 #if MBED_CONF_APP_WIFI_SHIELD != internal
mbed_official 71:a0fbcc153b55 198 delete wifi;
mbed_official 71:a0fbcc153b55 199 #endif
mbed_official 0:857719181846 200
mbed_official 35:052c1ba06ce7 201 printf("\nDone\n");
mbed_official 0:857719181846 202 }