Mike van Doorn
/
mbed-os-example-wifi
hoi
main.cpp@77:b74ac6641a3e, 2018-08-31 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 31 12:15:03 2018 +0100
- Revision:
- 77:b74ac6641a3e
- Parent:
- 74:af086eac5e99
- Child:
- 91:dab9882e2b49
Remove external drivers
.
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 | #include "TCPSocket.h" |
mbed_official | 0:857719181846 | 19 | |
mbed_official | 71:a0fbcc153b55 | 20 | WiFiInterface *wifi; |
mbed_official | 21:b2f2f6a840b4 | 21 | |
mbed_official | 0:857719181846 | 22 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 23 | { |
mbed_official | 0:857719181846 | 24 | switch (sec) { |
mbed_official | 0:857719181846 | 25 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 26 | return "None"; |
mbed_official | 0:857719181846 | 27 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 28 | return "WEP"; |
mbed_official | 0:857719181846 | 29 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 30 | return "WPA"; |
mbed_official | 0:857719181846 | 31 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 32 | return "WPA2"; |
mbed_official | 0:857719181846 | 33 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 34 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 35 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 36 | default: |
mbed_official | 0:857719181846 | 37 | return "Unknown"; |
mbed_official | 0:857719181846 | 38 | } |
mbed_official | 0:857719181846 | 39 | } |
mbed_official | 0:857719181846 | 40 | |
mbed_official | 37:3a31525e2971 | 41 | int scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 42 | { |
mbed_official | 0:857719181846 | 43 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 44 | |
mbed_official | 35:052c1ba06ce7 | 45 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 46 | |
mbed_official | 0:857719181846 | 47 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 48 | |
mbed_official | 66:2cf02c7d430c | 49 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 50 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 51 | return 0; |
mbed_official | 66:2cf02c7d430c | 52 | } |
mbed_official | 66:2cf02c7d430c | 53 | |
mbed_official | 0:857719181846 | 54 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 55 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 56 | |
mbed_official | 0:857719181846 | 57 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 58 | count = wifi->scan(ap, count); |
mbed_official | 66:2cf02c7d430c | 59 | |
mbed_official | 66:2cf02c7d430c | 60 | if (count <= 0) { |
mbed_official | 66:2cf02c7d430c | 61 | printf("scan() failed with return value: %d\n", count); |
mbed_official | 66:2cf02c7d430c | 62 | return 0; |
mbed_official | 66:2cf02c7d430c | 63 | } |
mbed_official | 66:2cf02c7d430c | 64 | |
mbed_official | 66:2cf02c7d430c | 65 | for (int i = 0; i < count; i++) { |
mbed_official | 35:052c1ba06ce7 | 66 | 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 | 67 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 68 | 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 | 69 | } |
mbed_official | 35:052c1ba06ce7 | 70 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 71 | |
mbed_official | 0:857719181846 | 72 | delete[] ap; |
mbed_official | 37:3a31525e2971 | 73 | return count; |
mbed_official | 0:857719181846 | 74 | } |
mbed_official | 0:857719181846 | 75 | |
mbed_official | 0:857719181846 | 76 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 77 | { |
mbed_official | 0:857719181846 | 78 | TCPSocket socket; |
mbed_official | 24:323569a565ec | 79 | nsapi_error_t response; |
mbed_official | 0:857719181846 | 80 | |
mbed_official | 35:052c1ba06ce7 | 81 | printf("Sending HTTP request to www.arm.com...\n"); |
mbed_official | 0:857719181846 | 82 | |
mbed_official | 0:857719181846 | 83 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 66:2cf02c7d430c | 84 | response = socket.open(net); |
mbed_official | 66:2cf02c7d430c | 85 | if(0 != response) { |
mbed_official | 66:2cf02c7d430c | 86 | printf("socket.open() failed: %d\n", response); |
mbed_official | 66:2cf02c7d430c | 87 | return; |
mbed_official | 66:2cf02c7d430c | 88 | } |
mbed_official | 66:2cf02c7d430c | 89 | |
mbed_official | 66:2cf02c7d430c | 90 | response = socket.connect("api.ipify.org", 80); |
mbed_official | 24:323569a565ec | 91 | if(0 != response) { |
mbed_official | 35:052c1ba06ce7 | 92 | printf("Error connecting: %d\n", response); |
mbed_official | 24:323569a565ec | 93 | socket.close(); |
mbed_official | 24:323569a565ec | 94 | return; |
mbed_official | 24:323569a565ec | 95 | } |
mbed_official | 0:857719181846 | 96 | |
mbed_official | 0:857719181846 | 97 | // Send a simple http request |
mbed_official | 66:2cf02c7d430c | 98 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n"; |
mbed_official | 33:12f0df4d51d7 | 99 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 66:2cf02c7d430c | 100 | |
mbed_official | 66:2cf02c7d430c | 101 | // Loop until whole request send |
mbed_official | 66:2cf02c7d430c | 102 | while(size) { |
mbed_official | 24:323569a565ec | 103 | response = socket.send(sbuffer+response, size); |
mbed_official | 24:323569a565ec | 104 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 105 | printf("Error sending data: %d\n", response); |
mbed_official | 24:323569a565ec | 106 | socket.close(); |
mbed_official | 24:323569a565ec | 107 | return; |
mbed_official | 24:323569a565ec | 108 | } |
mbed_official | 66:2cf02c7d430c | 109 | size -= response; |
mbed_official | 66:2cf02c7d430c | 110 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 24:323569a565ec | 111 | } |
mbed_official | 0:857719181846 | 112 | |
mbed_official | 66:2cf02c7d430c | 113 | // Receieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 114 | char rbuffer[64]; |
mbed_official | 24:323569a565ec | 115 | response = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 24:323569a565ec | 116 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 117 | printf("Error receiving data: %d\n", response); |
mbed_official | 24:323569a565ec | 118 | } else { |
mbed_official | 35:052c1ba06ce7 | 119 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 24:323569a565ec | 120 | } |
mbed_official | 0:857719181846 | 121 | |
mbed_official | 0:857719181846 | 122 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 123 | socket.close(); |
mbed_official | 0:857719181846 | 124 | } |
mbed_official | 0:857719181846 | 125 | |
mbed_official | 0:857719181846 | 126 | int main() |
mbed_official | 0:857719181846 | 127 | { |
mbed_official | 37:3a31525e2971 | 128 | int count = 0; |
mbed_official | 37:3a31525e2971 | 129 | |
mbed_official | 67:ebff4a8d228d | 130 | printf("WiFi example\n"); |
mbed_official | 67:ebff4a8d228d | 131 | |
mbed_official | 67:ebff4a8d228d | 132 | #ifdef MBED_MAJOR_VERSION |
mbed_official | 67:ebff4a8d228d | 133 | printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
mbed_official | 67:ebff4a8d228d | 134 | #endif |
mbed_official | 0:857719181846 | 135 | |
mbed_official | 71:a0fbcc153b55 | 136 | wifi = WiFiInterface::get_default_instance(); |
mbed_official | 72:b4761c52cc91 | 137 | if (!wifi) { |
mbed_official | 72:b4761c52cc91 | 138 | printf("ERROR: No WiFiInterface found.\n"); |
mbed_official | 72:b4761c52cc91 | 139 | return -1; |
mbed_official | 72:b4761c52cc91 | 140 | } |
mbed_official | 71:a0fbcc153b55 | 141 | |
mbed_official | 71:a0fbcc153b55 | 142 | count = scan_demo(wifi); |
mbed_official | 37:3a31525e2971 | 143 | if (count == 0) { |
mbed_official | 37:3a31525e2971 | 144 | printf("No WIFI APNs found - can't continue further.\n"); |
mbed_official | 37:3a31525e2971 | 145 | return -1; |
mbed_official | 37:3a31525e2971 | 146 | } |
mbed_official | 0:857719181846 | 147 | |
mbed_official | 37:3a31525e2971 | 148 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
mbed_official | 71:a0fbcc153b55 | 149 | int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 150 | if (ret != 0) { |
mbed_official | 66:2cf02c7d430c | 151 | printf("\nConnection error: %d\n", ret); |
mbed_official | 0:857719181846 | 152 | return -1; |
mbed_official | 0:857719181846 | 153 | } |
mbed_official | 0:857719181846 | 154 | |
mbed_official | 35:052c1ba06ce7 | 155 | printf("Success\n\n"); |
mbed_official | 71:a0fbcc153b55 | 156 | printf("MAC: %s\n", wifi->get_mac_address()); |
mbed_official | 71:a0fbcc153b55 | 157 | printf("IP: %s\n", wifi->get_ip_address()); |
mbed_official | 71:a0fbcc153b55 | 158 | printf("Netmask: %s\n", wifi->get_netmask()); |
mbed_official | 71:a0fbcc153b55 | 159 | printf("Gateway: %s\n", wifi->get_gateway()); |
mbed_official | 71:a0fbcc153b55 | 160 | printf("RSSI: %d\n\n", wifi->get_rssi()); |
mbed_official | 0:857719181846 | 161 | |
mbed_official | 71:a0fbcc153b55 | 162 | http_demo(wifi); |
mbed_official | 71:a0fbcc153b55 | 163 | |
mbed_official | 71:a0fbcc153b55 | 164 | wifi->disconnect(); |
mbed_official | 0:857719181846 | 165 | |
mbed_official | 35:052c1ba06ce7 | 166 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 167 | } |