Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-os-example-mbed5-wifi by
main.cpp@1:aea78e21a7da, 2016-10-27 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Oct 27 15:45:05 2016 +0100
- Revision:
- 1:aea78e21a7da
- Parent:
- 0:857719181846
- Child:
- 10:5b5beb106156
Merge pull request #4 from ARMmbed/arduino_ff
Return error for platforms without D0/D1 pins defined
.
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 | 0:857719181846 | 20 | #if TARGET_UBLOX_EVK_ODIN_W2 |
mbed_official | 1:aea78e21a7da | 21 | #include "OdinWiFiInterface.h" |
mbed_official | 1:aea78e21a7da | 22 | OdinWiFiInterface wifi; |
mbed_official | 0:857719181846 | 23 | #else |
mbed_official | 1:aea78e21a7da | 24 | #if !TARGET_FF_ARDUINO |
mbed_official | 1:aea78e21a7da | 25 | #error [NOT_SUPPORTED] Only Arduino form factor devices are supported at this time |
mbed_official | 1:aea78e21a7da | 26 | #endif |
mbed_official | 1:aea78e21a7da | 27 | #include "ESP8266Interface.h" |
mbed_official | 1:aea78e21a7da | 28 | ESP8266Interface wifi(D1, D0); |
mbed_official | 0:857719181846 | 29 | #endif |
mbed_official | 0:857719181846 | 30 | |
mbed_official | 0:857719181846 | 31 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 32 | { |
mbed_official | 0:857719181846 | 33 | switch (sec) { |
mbed_official | 0:857719181846 | 34 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 35 | return "None"; |
mbed_official | 0:857719181846 | 36 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 37 | return "WEP"; |
mbed_official | 0:857719181846 | 38 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 39 | return "WPA"; |
mbed_official | 0:857719181846 | 40 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 41 | return "WPA2"; |
mbed_official | 0:857719181846 | 42 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 43 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 44 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 45 | default: |
mbed_official | 0:857719181846 | 46 | return "Unknown"; |
mbed_official | 0:857719181846 | 47 | } |
mbed_official | 0:857719181846 | 48 | } |
mbed_official | 0:857719181846 | 49 | |
mbed_official | 0:857719181846 | 50 | void scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 51 | { |
mbed_official | 0:857719181846 | 52 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 53 | |
mbed_official | 0:857719181846 | 54 | printf("Scan:\r\n"); |
mbed_official | 0:857719181846 | 55 | |
mbed_official | 0:857719181846 | 56 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 57 | |
mbed_official | 0:857719181846 | 58 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 59 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 60 | |
mbed_official | 0:857719181846 | 61 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 62 | count = wifi->scan(ap, count); |
mbed_official | 0:857719181846 | 63 | for (int i = 0; i < count; i++) |
mbed_official | 0:857719181846 | 64 | { |
mbed_official | 0:857719181846 | 65 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\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 | 0:857719181846 | 69 | printf("%d networks available.\r\n", count); |
mbed_official | 0:857719181846 | 70 | |
mbed_official | 0:857719181846 | 71 | delete[] ap; |
mbed_official | 0:857719181846 | 72 | } |
mbed_official | 0:857719181846 | 73 | |
mbed_official | 0:857719181846 | 74 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 75 | { |
mbed_official | 0:857719181846 | 76 | TCPSocket socket; |
mbed_official | 0:857719181846 | 77 | |
mbed_official | 0:857719181846 | 78 | printf("Sending HTTP request to www.arm.com...\r\n"); |
mbed_official | 0:857719181846 | 79 | |
mbed_official | 0:857719181846 | 80 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 0:857719181846 | 81 | socket.open(net); |
mbed_official | 0:857719181846 | 82 | socket.connect("www.arm.com", 80); |
mbed_official | 0:857719181846 | 83 | |
mbed_official | 0:857719181846 | 84 | // Send a simple http request |
mbed_official | 0:857719181846 | 85 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
mbed_official | 0:857719181846 | 86 | int scount = socket.send(sbuffer, sizeof sbuffer); |
mbed_official | 0:857719181846 | 87 | printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 0:857719181846 | 88 | |
mbed_official | 0:857719181846 | 89 | // Recieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 90 | char rbuffer[64]; |
mbed_official | 0:857719181846 | 91 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 0:857719181846 | 92 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 0:857719181846 | 93 | |
mbed_official | 0:857719181846 | 94 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 95 | socket.close(); |
mbed_official | 0:857719181846 | 96 | } |
mbed_official | 0:857719181846 | 97 | |
mbed_official | 0:857719181846 | 98 | int main() |
mbed_official | 0:857719181846 | 99 | { |
mbed_official | 0:857719181846 | 100 | printf("WiFi example\r\n\r\n"); |
mbed_official | 0:857719181846 | 101 | |
mbed_official | 0:857719181846 | 102 | scan_demo(&wifi); |
mbed_official | 0:857719181846 | 103 | |
mbed_official | 0:857719181846 | 104 | printf("\r\nConnecting...\r\n"); |
mbed_official | 0:857719181846 | 105 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 106 | if (ret != 0) { |
mbed_official | 0:857719181846 | 107 | printf("\r\nConnection error\r\n"); |
mbed_official | 0:857719181846 | 108 | return -1; |
mbed_official | 0:857719181846 | 109 | } |
mbed_official | 0:857719181846 | 110 | |
mbed_official | 0:857719181846 | 111 | printf("Success\r\n\r\n"); |
mbed_official | 0:857719181846 | 112 | printf("MAC: %s\r\n", wifi.get_mac_address()); |
mbed_official | 0:857719181846 | 113 | printf("IP: %s\r\n", wifi.get_ip_address()); |
mbed_official | 0:857719181846 | 114 | printf("Netmask: %s\r\n", wifi.get_netmask()); |
mbed_official | 0:857719181846 | 115 | printf("Gateway: %s\r\n", wifi.get_gateway()); |
mbed_official | 0:857719181846 | 116 | printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 117 | |
mbed_official | 0:857719181846 | 118 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 119 | |
mbed_official | 0:857719181846 | 120 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 121 | |
mbed_official | 0:857719181846 | 122 | printf("\r\nDone\r\n"); |
mbed_official | 0:857719181846 | 123 | } |