ESP8266 / Mbed OS mbed-os-example-esp8266
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 
00020 
00021 #include "ESP8266Interface.h"
00022 ESP8266Interface wifi(D1, D0);
00023 
00024 const char *sec2str(nsapi_security_t sec)
00025 {
00026     switch (sec) {
00027         case NSAPI_SECURITY_NONE:
00028             return "None";
00029         case NSAPI_SECURITY_WEP:
00030             return "WEP";
00031         case NSAPI_SECURITY_WPA:
00032             return "WPA";
00033         case NSAPI_SECURITY_WPA2:
00034             return "WPA2";
00035         case NSAPI_SECURITY_WPA_WPA2:
00036             return "WPA/WPA2";
00037         case NSAPI_SECURITY_UNKNOWN:
00038         default:
00039             return "Unknown";
00040     }
00041 }
00042 
00043 void scan_demo(WiFiInterface *wifi)
00044 {
00045     WiFiAccessPoint *ap;
00046 
00047     printf("Scan:\r\n");
00048 
00049     int count = wifi->scan(NULL,0);
00050 
00051     /* Limit number of network arbitrary to 15 */
00052     count = count < 15 ? count : 15;
00053 
00054     ap = new WiFiAccessPoint[count];
00055     count = wifi->scan(ap, count);
00056     for (int i = 0; i < count; i++)
00057     {
00058         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
00059                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00060                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00061     }
00062     printf("%d networks available.\r\n", count);
00063 
00064     delete[] ap;
00065 }
00066 
00067 void http_demo(NetworkInterface *net)
00068 {
00069     TCPSocket socket;
00070 
00071     printf("Sending HTTP request to www.arm.com...\r\n");
00072 
00073     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00074     socket.open(net);
00075     socket.connect("www.arm.com", 80);
00076 
00077     // Send a simple http request
00078     char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
00079     int scount = socket.send(sbuffer, sizeof sbuffer);
00080     printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00081 
00082     // Recieve a simple http response and print out the response line
00083     char rbuffer[64];
00084     int rcount = socket.recv(rbuffer, sizeof rbuffer);
00085     printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00086 
00087     // Close the socket to return its memory and bring down the network interface
00088     socket.close();
00089 }
00090 
00091 int main()
00092 {
00093     printf("WiFi example\r\n\r\n");
00094 
00095     // Scan for available access points 
00096     scan_demo(&wifi);
00097 
00098     printf("\r\nConnecting...\r\n");
00099     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00100     if (ret != 0) {
00101         printf("\r\nConnection error\r\n");
00102         return -1;
00103     }
00104 
00105     printf("Success\r\n\r\n");
00106     printf("MAC: %s\r\n", wifi.get_mac_address());
00107     printf("IP: %s\r\n", wifi.get_ip_address());
00108     printf("Netmask: %s\r\n", wifi.get_netmask());
00109     printf("Gateway: %s\r\n", wifi.get_gateway());
00110     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00111 
00112     http_demo(&wifi);
00113 
00114     wifi.disconnect();
00115 
00116     printf("\r\nDone\r\n");
00117 }