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@35:052c1ba06ce7, 2017-09-26 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Sep 26 11:45:04 2017 +0100
- Revision:
- 35:052c1ba06ce7
- Parent:
- 33:12f0df4d51d7
- Child:
- 37:3a31525e2971
Get rid of \r\n by adding newlines to mbed_app.json
We can do it in the `mbed_app.json`, rather than in every single
`printf()` line.
.
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 | 32:bca3f5f442b3 | 20 | #define WIFI_ESP8266 1 |
mbed_official | 32:bca3f5f442b3 | 21 | #define WIFI_IDW01M1 2 |
mbed_official | 32:bca3f5f442b3 | 22 | |
mbed_official | 0:857719181846 | 23 | #if TARGET_UBLOX_EVK_ODIN_W2 |
mbed_official | 1:aea78e21a7da | 24 | #include "OdinWiFiInterface.h" |
mbed_official | 1:aea78e21a7da | 25 | OdinWiFiInterface wifi; |
mbed_official | 10:5b5beb106156 | 26 | |
mbed_official | 21:b2f2f6a840b4 | 27 | #elif TARGET_REALTEK_RTL8195AM |
mbed_official | 21:b2f2f6a840b4 | 28 | #include "RTWInterface.h" |
mbed_official | 21:b2f2f6a840b4 | 29 | RTWInterface wifi; |
mbed_official | 21:b2f2f6a840b4 | 30 | |
mbed_official | 33:12f0df4d51d7 | 31 | #else // External WiFi modules |
mbed_official | 21:b2f2f6a840b4 | 32 | |
mbed_official | 32:bca3f5f442b3 | 33 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266 |
mbed_official | 1:aea78e21a7da | 34 | #include "ESP8266Interface.h" |
mbed_official | 10:5b5beb106156 | 35 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 32:bca3f5f442b3 | 36 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW01M1 |
mbed_official | 32:bca3f5f442b3 | 37 | #include "SpwfSAInterface.h" |
mbed_official | 32:bca3f5f442b3 | 38 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
mbed_official | 32:bca3f5f442b3 | 39 | #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW01M1 |
mbed_official | 10:5b5beb106156 | 40 | |
mbed_official | 33:12f0df4d51d7 | 41 | #endif |
mbed_official | 0:857719181846 | 42 | |
mbed_official | 0:857719181846 | 43 | const char *sec2str(nsapi_security_t sec) |
mbed_official | 0:857719181846 | 44 | { |
mbed_official | 0:857719181846 | 45 | switch (sec) { |
mbed_official | 0:857719181846 | 46 | case NSAPI_SECURITY_NONE: |
mbed_official | 0:857719181846 | 47 | return "None"; |
mbed_official | 0:857719181846 | 48 | case NSAPI_SECURITY_WEP: |
mbed_official | 0:857719181846 | 49 | return "WEP"; |
mbed_official | 0:857719181846 | 50 | case NSAPI_SECURITY_WPA: |
mbed_official | 0:857719181846 | 51 | return "WPA"; |
mbed_official | 0:857719181846 | 52 | case NSAPI_SECURITY_WPA2: |
mbed_official | 0:857719181846 | 53 | return "WPA2"; |
mbed_official | 0:857719181846 | 54 | case NSAPI_SECURITY_WPA_WPA2: |
mbed_official | 0:857719181846 | 55 | return "WPA/WPA2"; |
mbed_official | 0:857719181846 | 56 | case NSAPI_SECURITY_UNKNOWN: |
mbed_official | 0:857719181846 | 57 | default: |
mbed_official | 0:857719181846 | 58 | return "Unknown"; |
mbed_official | 0:857719181846 | 59 | } |
mbed_official | 0:857719181846 | 60 | } |
mbed_official | 0:857719181846 | 61 | |
mbed_official | 0:857719181846 | 62 | void scan_demo(WiFiInterface *wifi) |
mbed_official | 0:857719181846 | 63 | { |
mbed_official | 0:857719181846 | 64 | WiFiAccessPoint *ap; |
mbed_official | 0:857719181846 | 65 | |
mbed_official | 35:052c1ba06ce7 | 66 | printf("Scan:\n"); |
mbed_official | 0:857719181846 | 67 | |
mbed_official | 0:857719181846 | 68 | int count = wifi->scan(NULL,0); |
mbed_official | 0:857719181846 | 69 | |
mbed_official | 0:857719181846 | 70 | /* Limit number of network arbitrary to 15 */ |
mbed_official | 0:857719181846 | 71 | count = count < 15 ? count : 15; |
mbed_official | 0:857719181846 | 72 | |
mbed_official | 0:857719181846 | 73 | ap = new WiFiAccessPoint[count]; |
mbed_official | 0:857719181846 | 74 | count = wifi->scan(ap, count); |
mbed_official | 0:857719181846 | 75 | for (int i = 0; i < count; i++) |
mbed_official | 0:857719181846 | 76 | { |
mbed_official | 35:052c1ba06ce7 | 77 | 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 | 78 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
mbed_official | 0:857719181846 | 79 | 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 | 80 | } |
mbed_official | 35:052c1ba06ce7 | 81 | printf("%d networks available.\n", count); |
mbed_official | 0:857719181846 | 82 | |
mbed_official | 0:857719181846 | 83 | delete[] ap; |
mbed_official | 0:857719181846 | 84 | } |
mbed_official | 0:857719181846 | 85 | |
mbed_official | 0:857719181846 | 86 | void http_demo(NetworkInterface *net) |
mbed_official | 0:857719181846 | 87 | { |
mbed_official | 0:857719181846 | 88 | TCPSocket socket; |
mbed_official | 24:323569a565ec | 89 | nsapi_error_t response; |
mbed_official | 0:857719181846 | 90 | |
mbed_official | 35:052c1ba06ce7 | 91 | printf("Sending HTTP request to www.arm.com...\n"); |
mbed_official | 0:857719181846 | 92 | |
mbed_official | 0:857719181846 | 93 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
mbed_official | 0:857719181846 | 94 | socket.open(net); |
mbed_official | 24:323569a565ec | 95 | response = socket.connect("www.arm.com", 80); |
mbed_official | 24:323569a565ec | 96 | if(0 != response) { |
mbed_official | 35:052c1ba06ce7 | 97 | printf("Error connecting: %d\n", response); |
mbed_official | 24:323569a565ec | 98 | socket.close(); |
mbed_official | 24:323569a565ec | 99 | return; |
mbed_official | 24:323569a565ec | 100 | } |
mbed_official | 0:857719181846 | 101 | |
mbed_official | 0:857719181846 | 102 | // Send a simple http request |
mbed_official | 0:857719181846 | 103 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
mbed_official | 33:12f0df4d51d7 | 104 | nsapi_size_t size = strlen(sbuffer); |
mbed_official | 24:323569a565ec | 105 | response = 0; |
mbed_official | 24:323569a565ec | 106 | while(size) |
mbed_official | 24:323569a565ec | 107 | { |
mbed_official | 24:323569a565ec | 108 | response = socket.send(sbuffer+response, size); |
mbed_official | 24:323569a565ec | 109 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 110 | printf("Error sending data: %d\n", response); |
mbed_official | 24:323569a565ec | 111 | socket.close(); |
mbed_official | 24:323569a565ec | 112 | return; |
mbed_official | 24:323569a565ec | 113 | } else { |
mbed_official | 24:323569a565ec | 114 | size -= response; |
mbed_official | 24:323569a565ec | 115 | // Check if entire message was sent or not |
mbed_official | 35:052c1ba06ce7 | 116 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mbed_official | 24:323569a565ec | 117 | } |
mbed_official | 24:323569a565ec | 118 | } |
mbed_official | 0:857719181846 | 119 | |
mbed_official | 0:857719181846 | 120 | // Recieve a simple http response and print out the response line |
mbed_official | 0:857719181846 | 121 | char rbuffer[64]; |
mbed_official | 24:323569a565ec | 122 | response = socket.recv(rbuffer, sizeof rbuffer); |
mbed_official | 24:323569a565ec | 123 | if (response < 0) { |
mbed_official | 35:052c1ba06ce7 | 124 | printf("Error receiving data: %d\n", response); |
mbed_official | 24:323569a565ec | 125 | } else { |
mbed_official | 35:052c1ba06ce7 | 126 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
mbed_official | 24:323569a565ec | 127 | } |
mbed_official | 0:857719181846 | 128 | |
mbed_official | 0:857719181846 | 129 | // Close the socket to return its memory and bring down the network interface |
mbed_official | 0:857719181846 | 130 | socket.close(); |
mbed_official | 0:857719181846 | 131 | } |
mbed_official | 0:857719181846 | 132 | |
mbed_official | 0:857719181846 | 133 | int main() |
mbed_official | 0:857719181846 | 134 | { |
mbed_official | 35:052c1ba06ce7 | 135 | printf("WiFi example\n\n"); |
mbed_official | 0:857719181846 | 136 | |
mbed_official | 0:857719181846 | 137 | scan_demo(&wifi); |
mbed_official | 0:857719181846 | 138 | |
mbed_official | 35:052c1ba06ce7 | 139 | printf("\nConnecting...\n"); |
mbed_official | 0:857719181846 | 140 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
mbed_official | 0:857719181846 | 141 | if (ret != 0) { |
mbed_official | 35:052c1ba06ce7 | 142 | printf("\nConnection error\n"); |
mbed_official | 0:857719181846 | 143 | return -1; |
mbed_official | 0:857719181846 | 144 | } |
mbed_official | 0:857719181846 | 145 | |
mbed_official | 35:052c1ba06ce7 | 146 | printf("Success\n\n"); |
mbed_official | 35:052c1ba06ce7 | 147 | printf("MAC: %s\n", wifi.get_mac_address()); |
mbed_official | 35:052c1ba06ce7 | 148 | printf("IP: %s\n", wifi.get_ip_address()); |
mbed_official | 35:052c1ba06ce7 | 149 | printf("Netmask: %s\n", wifi.get_netmask()); |
mbed_official | 35:052c1ba06ce7 | 150 | printf("Gateway: %s\n", wifi.get_gateway()); |
mbed_official | 35:052c1ba06ce7 | 151 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
mbed_official | 0:857719181846 | 152 | |
mbed_official | 0:857719181846 | 153 | http_demo(&wifi); |
mbed_official | 0:857719181846 | 154 | |
mbed_official | 0:857719181846 | 155 | wifi.disconnect(); |
mbed_official | 0:857719181846 | 156 | |
mbed_official | 35:052c1ba06ce7 | 157 | printf("\nDone\n"); |
mbed_official | 0:857719181846 | 158 | } |