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