Josué Chópite / Mbed OS ESP8266_Prueba

Dependencies:   esp8266-driver mbed-STM32F103C8T6

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 #include "TCPSocket.h"
00004  
00005  
00006 #include "ESP8266Interface.h"
00007 ESP8266Interface wifi(PA_2, PA_3);
00008  
00009 const char *sec2str(nsapi_security_t sec)
00010 {
00011     switch (sec) {
00012         case NSAPI_SECURITY_NONE:
00013             return "None";
00014         case NSAPI_SECURITY_WEP:
00015             return "WEP";
00016         case NSAPI_SECURITY_WPA:
00017             return "WPA";
00018         case NSAPI_SECURITY_WPA2:
00019             return "WPA2";
00020         case NSAPI_SECURITY_WPA_WPA2:
00021             return "WPA/WPA2";
00022         case NSAPI_SECURITY_UNKNOWN:
00023         default:
00024             return "Unknown";
00025     }
00026 }
00027  
00028 void scan_demo(WiFiInterface *wifi)
00029 {
00030     WiFiAccessPoint *ap;
00031  
00032     printf("Scan:\r\n");
00033  
00034     int count = wifi->scan(NULL,0);
00035  
00036     /* Limit number of network arbitrary to 15 */
00037     count = count < 15 ? count : 15;
00038  
00039     ap = new WiFiAccessPoint[count];
00040     count = wifi->scan(ap, count);
00041     for (int i = 0; i < count; i++)
00042     {
00043         printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
00044                sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00045                ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00046     }
00047     printf("%d networks available.\r\n", count);
00048  
00049     delete[] ap;
00050 }
00051  
00052 void http_demo(NetworkInterface *net)
00053 {
00054     TCPSocket socket;
00055  
00056     printf("Sending HTTP request to www.arm.com...\r\n");
00057  
00058     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00059     socket.open(net);
00060     socket.connect("www.arm.com", 80);
00061  
00062     // Send a simple http request
00063     char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
00064     int scount = socket.send(sbuffer, sizeof sbuffer);
00065     printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00066  
00067     // Recieve a simple http response and print out the response line
00068     char rbuffer[64];
00069     int rcount = socket.recv(rbuffer, sizeof rbuffer);
00070     printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00071  
00072     // Close the socket to return its memory and bring down the network interface
00073     socket.close();
00074 }
00075  
00076 int main()
00077 {
00078     printf("WiFi example\r\n\r\n");
00079  
00080     // Scan for available access points 
00081     scan_demo(&wifi);
00082  
00083     printf("\r\nConnecting...\r\n");
00084     int ret = wifi.connect("Flia_Leita", "DinoYNives0102", NSAPI_SECURITY_WPA_WPA2);
00085     if (ret != 0) {
00086         printf("\r\nConnection error\r\n");
00087         return -1;
00088     }
00089  
00090     printf("Success\r\n\r\n");
00091     printf("MAC: %s\r\n", wifi.get_mac_address());
00092     printf("IP: %s\r\n", wifi.get_ip_address());
00093     printf("Netmask: %s\r\n", wifi.get_netmask());
00094     printf("Gateway: %s\r\n", wifi.get_gateway());
00095     printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
00096  
00097     http_demo(&wifi);
00098  
00099     wifi.disconnect();
00100  
00101     printf("\r\nDone\r\n");
00102 }