Prueba

Dependencies:   esp8266-driver mbed-STM32F103C8T6

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

Committer:
josuechopite
Date:
Sun Aug 06 16:12:34 2017 +0000
Revision:
43:922bd982dac0
Parent:
29:0b58d21e87d6
Prueba

Who changed what in which revision?

UserRevisionLine numberNew contents of line
josuechopite 43:922bd982dac0 1 #include "stm32f103c8t6.h"
Jonathan Austin 0:2757d7abb7d9 2 #include "mbed.h"
josuechopite 43:922bd982dac0 3 #include "TCPSocket.h"
josuechopite 43:922bd982dac0 4
josuechopite 43:922bd982dac0 5
josuechopite 43:922bd982dac0 6 #include "ESP8266Interface.h"
josuechopite 43:922bd982dac0 7 ESP8266Interface wifi(PA_2, PA_3);
josuechopite 43:922bd982dac0 8
josuechopite 43:922bd982dac0 9 const char *sec2str(nsapi_security_t sec)
josuechopite 43:922bd982dac0 10 {
josuechopite 43:922bd982dac0 11 switch (sec) {
josuechopite 43:922bd982dac0 12 case NSAPI_SECURITY_NONE:
josuechopite 43:922bd982dac0 13 return "None";
josuechopite 43:922bd982dac0 14 case NSAPI_SECURITY_WEP:
josuechopite 43:922bd982dac0 15 return "WEP";
josuechopite 43:922bd982dac0 16 case NSAPI_SECURITY_WPA:
josuechopite 43:922bd982dac0 17 return "WPA";
josuechopite 43:922bd982dac0 18 case NSAPI_SECURITY_WPA2:
josuechopite 43:922bd982dac0 19 return "WPA2";
josuechopite 43:922bd982dac0 20 case NSAPI_SECURITY_WPA_WPA2:
josuechopite 43:922bd982dac0 21 return "WPA/WPA2";
josuechopite 43:922bd982dac0 22 case NSAPI_SECURITY_UNKNOWN:
josuechopite 43:922bd982dac0 23 default:
josuechopite 43:922bd982dac0 24 return "Unknown";
Jonathan Austin 0:2757d7abb7d9 25 }
Jonathan Austin 0:2757d7abb7d9 26 }
josuechopite 43:922bd982dac0 27
josuechopite 43:922bd982dac0 28 void scan_demo(WiFiInterface *wifi)
josuechopite 43:922bd982dac0 29 {
josuechopite 43:922bd982dac0 30 WiFiAccessPoint *ap;
josuechopite 43:922bd982dac0 31
josuechopite 43:922bd982dac0 32 printf("Scan:\r\n");
josuechopite 43:922bd982dac0 33
josuechopite 43:922bd982dac0 34 int count = wifi->scan(NULL,0);
josuechopite 43:922bd982dac0 35
josuechopite 43:922bd982dac0 36 /* Limit number of network arbitrary to 15 */
josuechopite 43:922bd982dac0 37 count = count < 15 ? count : 15;
josuechopite 43:922bd982dac0 38
josuechopite 43:922bd982dac0 39 ap = new WiFiAccessPoint[count];
josuechopite 43:922bd982dac0 40 count = wifi->scan(ap, count);
josuechopite 43:922bd982dac0 41 for (int i = 0; i < count; i++)
josuechopite 43:922bd982dac0 42 {
josuechopite 43:922bd982dac0 43 printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
josuechopite 43:922bd982dac0 44 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
josuechopite 43:922bd982dac0 45 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
josuechopite 43:922bd982dac0 46 }
josuechopite 43:922bd982dac0 47 printf("%d networks available.\r\n", count);
josuechopite 43:922bd982dac0 48
josuechopite 43:922bd982dac0 49 delete[] ap;
josuechopite 43:922bd982dac0 50 }
josuechopite 43:922bd982dac0 51
josuechopite 43:922bd982dac0 52 void http_demo(NetworkInterface *net)
josuechopite 43:922bd982dac0 53 {
josuechopite 43:922bd982dac0 54 TCPSocket socket;
josuechopite 43:922bd982dac0 55
josuechopite 43:922bd982dac0 56 printf("Sending HTTP request to www.arm.com...\r\n");
josuechopite 43:922bd982dac0 57
josuechopite 43:922bd982dac0 58 // Open a socket on the network interface, and create a TCP connection to www.arm.com
josuechopite 43:922bd982dac0 59 socket.open(net);
josuechopite 43:922bd982dac0 60 socket.connect("www.arm.com", 80);
josuechopite 43:922bd982dac0 61
josuechopite 43:922bd982dac0 62 // Send a simple http request
josuechopite 43:922bd982dac0 63 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
josuechopite 43:922bd982dac0 64 int scount = socket.send(sbuffer, sizeof sbuffer);
josuechopite 43:922bd982dac0 65 printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
josuechopite 43:922bd982dac0 66
josuechopite 43:922bd982dac0 67 // Recieve a simple http response and print out the response line
josuechopite 43:922bd982dac0 68 char rbuffer[64];
josuechopite 43:922bd982dac0 69 int rcount = socket.recv(rbuffer, sizeof rbuffer);
josuechopite 43:922bd982dac0 70 printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
josuechopite 43:922bd982dac0 71
josuechopite 43:922bd982dac0 72 // Close the socket to return its memory and bring down the network interface
josuechopite 43:922bd982dac0 73 socket.close();
josuechopite 43:922bd982dac0 74 }
josuechopite 43:922bd982dac0 75
josuechopite 43:922bd982dac0 76 int main()
josuechopite 43:922bd982dac0 77 {
josuechopite 43:922bd982dac0 78 printf("WiFi example\r\n\r\n");
josuechopite 43:922bd982dac0 79
josuechopite 43:922bd982dac0 80 // Scan for available access points
josuechopite 43:922bd982dac0 81 scan_demo(&wifi);
josuechopite 43:922bd982dac0 82
josuechopite 43:922bd982dac0 83 printf("\r\nConnecting...\r\n");
josuechopite 43:922bd982dac0 84 int ret = wifi.connect("Flia_Leita", "DinoYNives0102", NSAPI_SECURITY_WPA_WPA2);
josuechopite 43:922bd982dac0 85 if (ret != 0) {
josuechopite 43:922bd982dac0 86 printf("\r\nConnection error\r\n");
josuechopite 43:922bd982dac0 87 return -1;
josuechopite 43:922bd982dac0 88 }
josuechopite 43:922bd982dac0 89
josuechopite 43:922bd982dac0 90 printf("Success\r\n\r\n");
josuechopite 43:922bd982dac0 91 printf("MAC: %s\r\n", wifi.get_mac_address());
josuechopite 43:922bd982dac0 92 printf("IP: %s\r\n", wifi.get_ip_address());
josuechopite 43:922bd982dac0 93 printf("Netmask: %s\r\n", wifi.get_netmask());
josuechopite 43:922bd982dac0 94 printf("Gateway: %s\r\n", wifi.get_gateway());
josuechopite 43:922bd982dac0 95 printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
josuechopite 43:922bd982dac0 96
josuechopite 43:922bd982dac0 97 http_demo(&wifi);
josuechopite 43:922bd982dac0 98
josuechopite 43:922bd982dac0 99 wifi.disconnect();
josuechopite 43:922bd982dac0 100
josuechopite 43:922bd982dac0 101 printf("\r\nDone\r\n");
josuechopite 43:922bd982dac0 102 }