Christopher Foley
/
ESE519_WIFI_CONTROLLER
a
Fork of ESE519_Lab6_part1_skeleton by
wifiGETWrapper.cpp@7:ca226305d28b, 2018-10-24 (annotated)
- Committer:
- hydroguy45
- Date:
- Wed Oct 24 20:54:43 2018 +0000
- Revision:
- 7:ca226305d28b
- Child:
- 9:b8801ba7d7c2
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hydroguy45 | 7:ca226305d28b | 1 | /* WiFi Example |
hydroguy45 | 7:ca226305d28b | 2 | * Copyright (c) 2016 ARM Limited |
hydroguy45 | 7:ca226305d28b | 3 | * |
hydroguy45 | 7:ca226305d28b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hydroguy45 | 7:ca226305d28b | 5 | * you may not use this file except in compliance with the License. |
hydroguy45 | 7:ca226305d28b | 6 | * You may obtain a copy of the License at |
hydroguy45 | 7:ca226305d28b | 7 | * |
hydroguy45 | 7:ca226305d28b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hydroguy45 | 7:ca226305d28b | 9 | * |
hydroguy45 | 7:ca226305d28b | 10 | * Unless required by applicable law or agreed to in writing, software |
hydroguy45 | 7:ca226305d28b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hydroguy45 | 7:ca226305d28b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hydroguy45 | 7:ca226305d28b | 13 | * See the License for the specific language governing permissions and |
hydroguy45 | 7:ca226305d28b | 14 | * limitations under the License. |
hydroguy45 | 7:ca226305d28b | 15 | */ |
hydroguy45 | 7:ca226305d28b | 16 | //#include "EthernetInterface.h" |
hydroguy45 | 7:ca226305d28b | 17 | #include "wifiGETWrapper.h" |
hydroguy45 | 7:ca226305d28b | 18 | #include "mbed.h" |
hydroguy45 | 7:ca226305d28b | 19 | #include "TCPSocket.h" |
hydroguy45 | 7:ca226305d28b | 20 | |
hydroguy45 | 7:ca226305d28b | 21 | #define WIFI_SSID "Hydroguy45" |
hydroguy45 | 7:ca226305d28b | 22 | #define WIFI_PASSWORD "segfault" |
hydroguy45 | 7:ca226305d28b | 23 | #define PORT 8080 |
hydroguy45 | 7:ca226305d28b | 24 | |
hydroguy45 | 7:ca226305d28b | 25 | ESP8266Interface wifi(p13, p14); |
hydroguy45 | 7:ca226305d28b | 26 | /*void startHTTPServer(ESP8266Interface * wifi){ |
hydroguy45 | 7:ca226305d28b | 27 | printf("Starting HTTP server"); |
hydroguy45 | 7:ca226305d28b | 28 | TCPServer server; |
hydroguy45 | 7:ca226305d28b | 29 | server.open(wifi); |
hydroguy45 | 7:ca226305d28b | 30 | server.bind(wifi->get_ip_address(),PORT); |
hydroguy45 | 7:ca226305d28b | 31 | server.listen(); |
hydroguy45 | 7:ca226305d28b | 32 | while(true){ |
hydroguy45 | 7:ca226305d28b | 33 | SocketAddress addr; |
hydroguy45 | 7:ca226305d28b | 34 | TCPSocket client; |
hydroguy45 | 7:ca226305d28b | 35 | printf("\n\n\r***Waiting for a socket connection***\n\n\r"); |
hydroguy45 | 7:ca226305d28b | 36 | int err = server.accept(&client, &addr); |
hydroguy45 | 7:ca226305d28b | 37 | if(err < 0){ |
hydroguy45 | 7:ca226305d28b | 38 | printf("Ran into an error %d", err); |
hydroguy45 | 7:ca226305d28b | 39 | break; |
hydroguy45 | 7:ca226305d28b | 40 | } |
hydroguy45 | 7:ca226305d28b | 41 | printf("----------------\n\rConnection created with %s%d\n\r", addr.get_ip_address(), addr.get_port()); |
hydroguy45 | 7:ca226305d28b | 42 | client.set_blocking(true); |
hydroguy45 | 7:ca226305d28b | 43 | client.set_timeout(1500); |
hydroguy45 | 7:ca226305d28b | 44 | char buff[256]; |
hydroguy45 | 7:ca226305d28b | 45 | while (true){ |
hydroguy45 | 7:ca226305d28b | 46 | int n=client.recv(buff, sizeof(buff)); |
hydroguy45 | 7:ca226305d28b | 47 | if(n<=0) break; |
hydroguy45 | 7:ca226305d28b | 48 | buff[n] = '\0'; |
hydroguy45 | 7:ca226305d28b | 49 | printf("Received from client: %s\n", buff); |
hydroguy45 | 7:ca226305d28b | 50 | client.send(buff, n); |
hydroguy45 | 7:ca226305d28b | 51 | printf("%s\n",buff); |
hydroguy45 | 7:ca226305d28b | 52 | if( n<=0)break; |
hydroguy45 | 7:ca226305d28b | 53 | } |
hydroguy45 | 7:ca226305d28b | 54 | client.close(); |
hydroguy45 | 7:ca226305d28b | 55 | printf("Connection Terminated\n\r--------------------\n\n\r"); |
hydroguy45 | 7:ca226305d28b | 56 | } |
hydroguy45 | 7:ca226305d28b | 57 | server.close(); |
hydroguy45 | 7:ca226305d28b | 58 | |
hydroguy45 | 7:ca226305d28b | 59 | TCPSocketServer server; |
hydroguy45 | 7:ca226305d28b | 60 | |
hydroguy45 | 7:ca226305d28b | 61 | server.listen(); |
hydroguy45 | 7:ca226305d28b | 62 | while(true){ |
hydroguy45 | 7:ca226305d28b | 63 | TCPSocketConnection connection; |
hydroguy45 | 7:ca226305d28b | 64 | server.accept(connection); |
hydroguy45 | 7:ca226305d28b | 65 | connection.set_blocking(false, 1500); |
hydroguy45 | 7:ca226305d28b | 66 | printf("Connection from %s\n", connection.get_address()); |
hydroguy45 | 7:ca226305d28b | 67 | char buff[256]; |
hydroguy45 | 7:ca226305d28b | 68 | while (true){ |
hydroguy45 | 7:ca226305d28b | 69 | int n=client.receive(buff, sizeof(buff)); |
hydroguy45 | 7:ca226305d28b | 70 | if(n<=0) break; |
hydroguy45 | 7:ca226305d28b | 71 | buff[n] = '\0'; |
hydroguy45 | 7:ca226305d28b | 72 | printf("Received from client: %s\n", buff); |
hydroguy45 | 7:ca226305d28b | 73 | client.send_all(buffer, n); |
hydroguy45 | 7:ca226305d28b | 74 | if( n<=0)break; |
hydroguy45 | 7:ca226305d28b | 75 | } |
hydroguy45 | 7:ca226305d28b | 76 | client.close() |
hydroguy45 | 7:ca226305d28b | 77 | } |
hydroguy45 | 7:ca226305d28b | 78 | server.close(); |
hydroguy45 | 7:ca226305d28b | 79 | }*/ |
hydroguy45 | 7:ca226305d28b | 80 | const char *sec2str(nsapi_security_t sec) |
hydroguy45 | 7:ca226305d28b | 81 | { |
hydroguy45 | 7:ca226305d28b | 82 | switch (sec) { |
hydroguy45 | 7:ca226305d28b | 83 | case NSAPI_SECURITY_NONE: |
hydroguy45 | 7:ca226305d28b | 84 | return "None"; |
hydroguy45 | 7:ca226305d28b | 85 | case NSAPI_SECURITY_WEP: |
hydroguy45 | 7:ca226305d28b | 86 | return "WEP"; |
hydroguy45 | 7:ca226305d28b | 87 | case NSAPI_SECURITY_WPA: |
hydroguy45 | 7:ca226305d28b | 88 | return "WPA"; |
hydroguy45 | 7:ca226305d28b | 89 | case NSAPI_SECURITY_WPA2: |
hydroguy45 | 7:ca226305d28b | 90 | return "WPA2"; |
hydroguy45 | 7:ca226305d28b | 91 | case NSAPI_SECURITY_WPA_WPA2: |
hydroguy45 | 7:ca226305d28b | 92 | return "WPA/WPA2"; |
hydroguy45 | 7:ca226305d28b | 93 | case NSAPI_SECURITY_UNKNOWN: |
hydroguy45 | 7:ca226305d28b | 94 | default: |
hydroguy45 | 7:ca226305d28b | 95 | return "Unknown"; |
hydroguy45 | 7:ca226305d28b | 96 | } |
hydroguy45 | 7:ca226305d28b | 97 | } |
hydroguy45 | 7:ca226305d28b | 98 | |
hydroguy45 | 7:ca226305d28b | 99 | void scan_demo(WiFiInterface *wifi) |
hydroguy45 | 7:ca226305d28b | 100 | { |
hydroguy45 | 7:ca226305d28b | 101 | WiFiAccessPoint *ap; |
hydroguy45 | 7:ca226305d28b | 102 | |
hydroguy45 | 7:ca226305d28b | 103 | printf("Scan:\r\n"); |
hydroguy45 | 7:ca226305d28b | 104 | |
hydroguy45 | 7:ca226305d28b | 105 | int count = wifi->scan(NULL,0); |
hydroguy45 | 7:ca226305d28b | 106 | |
hydroguy45 | 7:ca226305d28b | 107 | /* Limit number of network arbitrary to 15 */ |
hydroguy45 | 7:ca226305d28b | 108 | count = count < 15 ? count : 15; |
hydroguy45 | 7:ca226305d28b | 109 | |
hydroguy45 | 7:ca226305d28b | 110 | ap = new WiFiAccessPoint[count]; |
hydroguy45 | 7:ca226305d28b | 111 | count = wifi->scan(ap, count); |
hydroguy45 | 7:ca226305d28b | 112 | for (int i = 0; i < count; i++) |
hydroguy45 | 7:ca226305d28b | 113 | { |
hydroguy45 | 7:ca226305d28b | 114 | printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(), |
hydroguy45 | 7:ca226305d28b | 115 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
hydroguy45 | 7:ca226305d28b | 116 | ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel()); |
hydroguy45 | 7:ca226305d28b | 117 | } |
hydroguy45 | 7:ca226305d28b | 118 | printf("%d networks available.\r\n", count); |
hydroguy45 | 7:ca226305d28b | 119 | |
hydroguy45 | 7:ca226305d28b | 120 | delete[] ap; |
hydroguy45 | 7:ca226305d28b | 121 | } |
hydroguy45 | 7:ca226305d28b | 122 | |
hydroguy45 | 7:ca226305d28b | 123 | void GET(NetworkInterface *net, const char * address, const char * argument) |
hydroguy45 | 7:ca226305d28b | 124 | { |
hydroguy45 | 7:ca226305d28b | 125 | TCPSocket socket; |
hydroguy45 | 7:ca226305d28b | 126 | |
hydroguy45 | 7:ca226305d28b | 127 | printf("Sending HTTP request to %s/?result=%s\r\n", address, argument); |
hydroguy45 | 7:ca226305d28b | 128 | |
hydroguy45 | 7:ca226305d28b | 129 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
hydroguy45 | 7:ca226305d28b | 130 | socket.open(net); |
hydroguy45 | 7:ca226305d28b | 131 | socket.connect(address, 8081); |
hydroguy45 | 7:ca226305d28b | 132 | |
hydroguy45 | 7:ca226305d28b | 133 | // Send a simple http request |
hydroguy45 | 7:ca226305d28b | 134 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: \r\n\r\n"; |
hydroguy45 | 7:ca226305d28b | 135 | strcat(sbuffer, address); |
hydroguy45 | 7:ca226305d28b | 136 | strcat(sbuffer, ":8081/"); |
hydroguy45 | 7:ca226305d28b | 137 | strcat(sbuffer, argument); |
hydroguy45 | 7:ca226305d28b | 138 | int scount = socket.send(sbuffer, sizeof sbuffer); |
hydroguy45 | 7:ca226305d28b | 139 | printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
hydroguy45 | 7:ca226305d28b | 140 | |
hydroguy45 | 7:ca226305d28b | 141 | // Recieve a simple http response and print out the response line |
hydroguy45 | 7:ca226305d28b | 142 | char rbuffer[1024]; |
hydroguy45 | 7:ca226305d28b | 143 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
hydroguy45 | 7:ca226305d28b | 144 | printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
hydroguy45 | 7:ca226305d28b | 145 | |
hydroguy45 | 7:ca226305d28b | 146 | // Close the socket to return its memory and bring down the network interface |
hydroguy45 | 7:ca226305d28b | 147 | socket.close(); |
hydroguy45 | 7:ca226305d28b | 148 | } |
hydroguy45 | 7:ca226305d28b | 149 | int initConnection(const char * SSID, const char * password) |
hydroguy45 | 7:ca226305d28b | 150 | { |
hydroguy45 | 7:ca226305d28b | 151 | printf("WiFi example\r\n\r\n"); |
hydroguy45 | 7:ca226305d28b | 152 | |
hydroguy45 | 7:ca226305d28b | 153 | // Scan for available access points |
hydroguy45 | 7:ca226305d28b | 154 | scan_demo(&wifi); |
hydroguy45 | 7:ca226305d28b | 155 | |
hydroguy45 | 7:ca226305d28b | 156 | printf("\r\nConnecting...\r\n"); |
hydroguy45 | 7:ca226305d28b | 157 | int ret = wifi.connect(SSID, password, NSAPI_SECURITY_WPA_WPA2); |
hydroguy45 | 7:ca226305d28b | 158 | if (ret != 0) { |
hydroguy45 | 7:ca226305d28b | 159 | printf("\r\nConnection error\r\n"); |
hydroguy45 | 7:ca226305d28b | 160 | return -1; |
hydroguy45 | 7:ca226305d28b | 161 | } |
hydroguy45 | 7:ca226305d28b | 162 | |
hydroguy45 | 7:ca226305d28b | 163 | printf("Success\r\n\r\n"); |
hydroguy45 | 7:ca226305d28b | 164 | printf("MAC: %s\r\n", wifi.get_mac_address()); |
hydroguy45 | 7:ca226305d28b | 165 | printf("IP: %s\r\n", wifi.get_ip_address()); |
hydroguy45 | 7:ca226305d28b | 166 | printf("Netmask: %s\r\n", wifi.get_netmask()); |
hydroguy45 | 7:ca226305d28b | 167 | printf("Gateway: %s\r\n", wifi.get_gateway()); |
hydroguy45 | 7:ca226305d28b | 168 | printf("RSSI: %d\r\n\r\n", wifi.get_rssi()); |
hydroguy45 | 7:ca226305d28b | 169 | |
hydroguy45 | 7:ca226305d28b | 170 | GET(&wifi, "192.168.8.1", "Testing|testing|1|2|3"); |
hydroguy45 | 7:ca226305d28b | 171 | } |
hydroguy45 | 7:ca226305d28b | 172 | int sendGET(const char * args){ |
hydroguy45 | 7:ca226305d28b | 173 | GET(&wifi, "192.168.8.1", args); |
hydroguy45 | 7:ca226305d28b | 174 | } |