![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
87ty78ot7iyu
main.cpp@91:dab9882e2b49, 2019-01-09 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jan 09 14:30:04 2019 +0000
- Revision:
- 91:dab9882e2b49
- Parent:
- 77:b74ac6641a3e
- Child:
- 100:818629f882ab
Cleanup and simplify the example (#144)
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-wifi
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mbed_official | 71:a0fbcc153b55 | 19 | WiFiInterface *wifi; |
mbed_official | 21:b2f2f6a840b4 | 20 | |
mbed_official | 0:857719181846 | 21 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 22 | { |
mbed_official | 0:857719181846 | 23 | switch (sec) { |
mbed_official | 0:857719181846 | 24 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 25 | return "None"; |
mbed_official | 0:857719181846 | 26 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 27 | return "WEP"; |
mbed_official | 0:857719181846 | 28 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 29 | return "WPA"; |
mbed_official | 0:857719181846 | 30 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 31 | return "WPA2"; |
mbed_official | 0:857719181846 | 32 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 33 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 34 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 35 | default: |
mbed_official | 0:857719181846 | 36 | return "Unknown"; |
mbed_official | 0:857719181846 | 37 | } |
mbed_official | 0:857719181846 | 38 | } |
mbed_official | 0:857719181846 | 39 | |
mbed_official | 37:3a31525e2971 | 40 | int scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 41 | { |
mbed_official | 0:857719181846 | 42 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 43 | |
mbed_official | 35:052c1ba06ce7 | 44 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 45 | |
mbed_official | 0:857719181846 | 46 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 47 | |
mbed_official | 66:2cf02c7d430c | 48 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 49 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 50 | return 0; |
mbed_official | 66:2cf02c7d430c | 51 | } |
mbed_official | 66:2cf02c7d430c | 52 | |
mbed_official | 0:857719181846 | 53 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 54 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 55 | |
mbed_official | 0:857719181846 | 56 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 57 | count = wifi->scan(ap, count); |
mbed_official | 66:2cf02c7d430c | 58 | |
mbed_official | 66:2cf02c7d430c | 59 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 60 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 61 | return 0; |
mbed_official | 66:2cf02c7d430c | 62 | } |
mbed_official | 66:2cf02c7d430c | 63 | |
mbed_official | 66:2cf02c7d430c | 64 | for (int i = 0; i < count; i++) { |
mbed_official | 35:052c1ba06ce7 | 65 | 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 | 66 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 67 | 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 | 68 | } |
mbed_official | 35:052c1ba06ce7 | 69 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 70 | |
mbed_official | 0:857719181846 | 71 | delete[] ap; |
mbed_official | 37:3a31525e2971 | 72 | return count; |
mbed_official | 0:857719181846 | 73 | } |
mbed_official | 0:857719181846 | 74 | |
mbed_official | 0:857719181846 | 75 | int main() |
mbed_official | 0:857719181846 | 76 | { |
mbed_official | 67:ebff4a8d228d | 77 | printf("WiFi example\n"); |
mbed_official | 67:ebff4a8d228d | 78 | |
mbed_official | 67:ebff4a8d228d | 79 | #ifdef MBED_MAJOR_VERSION |
mbed_official | 67:ebff4a8d228d | 80 | printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
mbed_official | 67:ebff4a8d228d | 81 | #endif |
mbed_official | 0:857719181846 | 82 | |
mbed_official | 71:a0fbcc153b55 | 83 | wifi = WiFiInterface::get_default_instance(); |
mbed_official | 72:b4761c52cc91 | 84 | if (!wifi) { |
mbed_official | 72:b4761c52cc91 | 85 | printf("ERROR: No WiFiInterface found.\n"); |
mbed_official | 72:b4761c52cc91 | 86 | return -1; |
mbed_official | 72:b4761c52cc91 | 87 | } |
mbed_official | 71:a0fbcc153b55 | 88 | |
mbed_official | 91:dab9882e2b49 | 89 | int count = scan_demo(wifi); |
mbed_official | 37:3a31525e2971 | 90 | if (count == 0) { |
mbed_official | 91:dab9882e2b49 | 91 | printf("No WIFI APs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 92 | return -1; |
mbed_official | 37:3a31525e2971 | 93 | } |
mbed_official | 0:857719181846 | 94 | |
mbed_official | 37:3a31525e2971 | 95 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 71:a0fbcc153b55 | 96 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 97 | if (ret != 0) { |
mbed_official | 66:2cf02c7d430c | 98 | printf("\nConnection error: %d\n", ret); |
mbed_official | 0:857719181846 | 99 | return -1; |
mbed_official | 0:857719181846 | 100 | } |
mbed_official | 0:857719181846 | 101 | |
mbed_official | 35:052c1ba06ce7 | 102 | printf("Success\n\n"); |
mbed_official | 71:a0fbcc153b55 | 103 | printf("MAC: %s\n", wifi->get_mac_address()); |
mbed_official | 71:a0fbcc153b55 | 104 | printf("IP: %s\n", wifi->get_ip_address()); |
mbed_official | 71:a0fbcc153b55 | 105 | printf("Netmask: %s\n", wifi->get_netmask()); |
mbed_official | 71:a0fbcc153b55 | 106 | printf("Gateway: %s\n", wifi->get_gateway()); |
mbed_official | 71:a0fbcc153b55 | 107 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
mbed_official | 0:857719181846 | 108 | |
mbed_official | 71:a0fbcc153b55 | 109 | wifi->disconnect(); |
mbed_official | 0:857719181846 | 110 | |
mbed_official | 35:052c1ba06ce7 | 111 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 112 | } |