Alex Leung
/
HealthTracker
Test version
WIFI.cpp@0:4be500de690c, 2018-03-20 (annotated)
- Committer:
- a2824256
- Date:
- Tue Mar 20 02:09:21 2018 +0000
- Revision:
- 0:4be500de690c
- Child:
- 1:bd9f39f9d91c
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a2824256 | 0:4be500de690c | 1 | #include "mbed.h" |
a2824256 | 0:4be500de690c | 2 | #include "TCPSocket.h" |
a2824256 | 0:4be500de690c | 3 | |
a2824256 | 0:4be500de690c | 4 | #include "ESP8266Interface.h" |
a2824256 | 0:4be500de690c | 5 | #define MBED_CONF_APP_NETWORK_INTERFACE WIFI_ESP8266 |
a2824256 | 0:4be500de690c | 6 | #define MBED_CONF_EASY_CONNECT_WIFI_ESP8266_TX PC_10 |
a2824256 | 0:4be500de690c | 7 | #define MBED_CONF_EASY_CONNECT_WIFI_ESP8266_RX PC_11 |
a2824256 | 0:4be500de690c | 8 | |
a2824256 | 0:4be500de690c | 9 | ESP8266Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_ESP8266_TX, MBED_CONF_EASY_CONNECT_WIFI_ESP8266_RX); |
a2824256 | 0:4be500de690c | 10 | const char *sec2str(nsapi_security_t sec) |
a2824256 | 0:4be500de690c | 11 | { |
a2824256 | 0:4be500de690c | 12 | switch (sec) { |
a2824256 | 0:4be500de690c | 13 | case NSAPI_SECURITY_NONE: |
a2824256 | 0:4be500de690c | 14 | return "None"; |
a2824256 | 0:4be500de690c | 15 | case NSAPI_SECURITY_WEP: |
a2824256 | 0:4be500de690c | 16 | return "WEP"; |
a2824256 | 0:4be500de690c | 17 | case NSAPI_SECURITY_WPA: |
a2824256 | 0:4be500de690c | 18 | return "WPA"; |
a2824256 | 0:4be500de690c | 19 | case NSAPI_SECURITY_WPA2: |
a2824256 | 0:4be500de690c | 20 | return "WPA2"; |
a2824256 | 0:4be500de690c | 21 | case NSAPI_SECURITY_WPA_WPA2: |
a2824256 | 0:4be500de690c | 22 | return "WPA/WPA2"; |
a2824256 | 0:4be500de690c | 23 | case NSAPI_SECURITY_UNKNOWN: |
a2824256 | 0:4be500de690c | 24 | default: |
a2824256 | 0:4be500de690c | 25 | return "Unknown"; |
a2824256 | 0:4be500de690c | 26 | } |
a2824256 | 0:4be500de690c | 27 | } |
a2824256 | 0:4be500de690c | 28 | |
a2824256 | 0:4be500de690c | 29 | void scan_demo(WiFiInterface *wifi) |
a2824256 | 0:4be500de690c | 30 | { |
a2824256 | 0:4be500de690c | 31 | WiFiAccessPoint *ap; |
a2824256 | 0:4be500de690c | 32 | |
a2824256 | 0:4be500de690c | 33 | pc.printf("Scan:\r\n"); |
a2824256 | 0:4be500de690c | 34 | |
a2824256 | 0:4be500de690c | 35 | int count = wifi->scan(NULL,0); |
a2824256 | 0:4be500de690c | 36 | |
a2824256 | 0:4be500de690c | 37 | /* Limit number of network arbitrary to 15 */ |
a2824256 | 0:4be500de690c | 38 | count = count < 15 ? count : 15; |
a2824256 | 0:4be500de690c | 39 | |
a2824256 | 0:4be500de690c | 40 | ap = new WiFiAccessPoint[count]; |
a2824256 | 0:4be500de690c | 41 | count = wifi->scan(ap, count); |
a2824256 | 0:4be500de690c | 42 | for (int i = 0; i < count; i++) |
a2824256 | 0:4be500de690c | 43 | { |
a2824256 | 0:4be500de690c | 44 | pc.printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(), |
a2824256 | 0:4be500de690c | 45 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
a2824256 | 0:4be500de690c | 46 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
a2824256 | 0:4be500de690c | 47 | } |
a2824256 | 0:4be500de690c | 48 | pc.printf("%d networks available.\r\n", count); |
a2824256 | 0:4be500de690c | 49 | |
a2824256 | 0:4be500de690c | 50 | delete[] ap; |
a2824256 | 0:4be500de690c | 51 | } |
a2824256 | 0:4be500de690c | 52 | |
a2824256 | 0:4be500de690c | 53 | void http_demo(NetworkInterface *net) |
a2824256 | 0:4be500de690c | 54 | { |
a2824256 | 0:4be500de690c | 55 | TCPSocket socket; |
a2824256 | 0:4be500de690c | 56 | |
a2824256 | 0:4be500de690c | 57 | pc.printf("Sending HTTP request to 192.168.8.100...\r\n"); |
a2824256 | 0:4be500de690c | 58 | |
a2824256 | 0:4be500de690c | 59 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
a2824256 | 0:4be500de690c | 60 | socket.open(net); |
a2824256 | 0:4be500de690c | 61 | socket.connect("192.168.8.100", 8282); |
a2824256 | 0:4be500de690c | 62 | |
a2824256 | 0:4be500de690c | 63 | // Send a simple http request |
a2824256 | 0:4be500de690c | 64 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: 192.168.8.100\r\n\r\n"; |
a2824256 | 0:4be500de690c | 65 | int scount = socket.send(sbuffer, sizeof sbuffer); |
a2824256 | 0:4be500de690c | 66 | pc.printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
a2824256 | 0:4be500de690c | 67 | |
a2824256 | 0:4be500de690c | 68 | // Recieve a simple http response and print out the response line |
a2824256 | 0:4be500de690c | 69 | char rbuffer[64]; |
a2824256 | 0:4be500de690c | 70 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
a2824256 | 0:4be500de690c | 71 | pc.printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
a2824256 | 0:4be500de690c | 72 | |
a2824256 | 0:4be500de690c | 73 | // Close the socket to return its memory and bring down the network interface |
a2824256 | 0:4be500de690c | 74 | socket.close(); |
a2824256 | 0:4be500de690c | 75 | } |