|
| 1 | +#include "ESP8266WifiConnect.h" |
| 2 | + |
| 3 | +void ESP8266WifiConnect::handleRoot() |
| 4 | +{ |
| 5 | + if (server.method() == HTTP_GET && !connected) |
| 6 | + { |
| 7 | + String s = "<html><body>Select from the AP listed below<br>"; |
| 8 | + s += apHTMLtable; |
| 9 | + s += "<form method=\"POST\" action=\"/\">"; |
| 10 | + s += "SSID:<br>"; |
| 11 | + s += "<input type=\"text\" length=32 name=\"ssid\"><br>"; |
| 12 | + s += "Password:<br>"; |
| 13 | + s += "<input type=\"password\" length=32 name=\"pass\"><br>"; |
| 14 | + s += "<input type=\"submit\">"; |
| 15 | + s += "</form></body></html>\r\n\r\n"; |
| 16 | + |
| 17 | + server.send(200, "text/html", s.c_str()); |
| 18 | + delay(10); |
| 19 | + Serial.println("Sending 200"); |
| 20 | + } |
| 21 | + else if (server.method() == HTTP_GET && connected) |
| 22 | + { |
| 23 | + String s = "<html><body>Connected to AP "; |
| 24 | + s += ssid; |
| 25 | + s += "</body></html>\r\n\r\n"; |
| 26 | + |
| 27 | + server.send(200, "text/html", s.c_str()); |
| 28 | + delay(10); |
| 29 | + Serial.println("Sending 200"); |
| 30 | + |
| 31 | + finished = true; |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + ssid = server.arg("ssid"); |
| 36 | + ssidLength = ssid.length(); |
| 37 | + password = server.arg("pass"); |
| 38 | + passLength = password.length(); |
| 39 | + Serial.println(ssid); |
| 40 | + Serial.println(password); |
| 41 | + |
| 42 | + String s = "<html><meta http-equiv=\"refresh\" content=\"10;url=http://192.168.4.1/\"/>Trying to connect in 10 sec. to AP "; |
| 43 | + s += ssid; |
| 44 | + s += "</html>\r\n\r\n"; |
| 45 | + |
| 46 | + server.send(200, "text/html", s.c_str()); |
| 47 | + delay(10); |
| 48 | + Serial.println("Sending 200"); |
| 49 | + |
| 50 | + connected = connectAP(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +ESP8266WifiConnect::ESP8266WifiConnect() |
| 55 | +{ |
| 56 | + // Init values |
| 57 | + numRetries = 10; |
| 58 | + ssidLength = 0; |
| 59 | + passLength = 0; |
| 60 | + ssid = ""; |
| 61 | + password = ""; |
| 62 | + apHTMLtable = ""; |
| 63 | + server = ESP8266WebServer(80); |
| 64 | + finished = false; |
| 65 | + connected = false; |
| 66 | +} |
| 67 | + |
| 68 | +void ESP8266WifiConnect::begin() |
| 69 | +{ |
| 70 | + // Open serial port for debug output |
| 71 | + Serial.begin(115200); |
| 72 | + delay(10); |
| 73 | + Serial.println(); |
| 74 | + Serial.println(); |
| 75 | + |
| 76 | + // Read EEPROM data |
| 77 | + EEPROM.begin(512); |
| 78 | + delay(10); |
| 79 | + readEEPROM(); |
| 80 | + |
| 81 | + // Connect to the AP |
| 82 | + int r = 0; |
| 83 | + while (r < numRetries && !connected) |
| 84 | + { |
| 85 | + connected = connectAP(); |
| 86 | + if(connected) |
| 87 | + { |
| 88 | + Serial.println("Connected!"); |
| 89 | + break; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Serial.println("Not connected!"); |
| 94 | + selectAP(); |
| 95 | + } |
| 96 | + |
| 97 | + r++; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +bool ESP8266WifiConnect::connectAP() |
| 102 | +{ |
| 103 | + // Init return value |
| 104 | + bool result = false; |
| 105 | + |
| 106 | + if(ssidLength == 0) |
| 107 | + return result; |
| 108 | + |
| 109 | + // Connect to the AP |
| 110 | + Serial.print("Connecting to AP "); |
| 111 | + Serial.println(ssid); |
| 112 | + WiFi.begin(ssid.c_str(), password.c_str()); |
| 113 | + |
| 114 | + int r = 0; |
| 115 | + while (r < numRetries) |
| 116 | + { |
| 117 | + if (WiFi.status() == WL_CONNECTED) |
| 118 | + { |
| 119 | + result = true; |
| 120 | + Serial.println(""); |
| 121 | + Serial.println("WiFi connected"); |
| 122 | + break; |
| 123 | + } |
| 124 | + |
| 125 | + r++; |
| 126 | + delay(500); |
| 127 | + Serial.print("."); |
| 128 | + } |
| 129 | + |
| 130 | + return result; |
| 131 | +} |
| 132 | + |
| 133 | +bool ESP8266WifiConnect::selectAP() |
| 134 | +{ |
| 135 | + // Scan for existing networks |
| 136 | + scanNetwork(); |
| 137 | + |
| 138 | + // Start the server |
| 139 | + std::function<void()> fpHandleRoot = std::bind(&ESP8266WifiConnect::handleRoot, this); |
| 140 | + server.on("/", fpHandleRoot); |
| 141 | + server.begin(); |
| 142 | + |
| 143 | + // Wait for a client to connect |
| 144 | + finished = false; |
| 145 | + while (!finished) |
| 146 | + { |
| 147 | + server.handleClient(); |
| 148 | + } |
| 149 | + |
| 150 | + // Stop the server |
| 151 | + server.close(); |
| 152 | + |
| 153 | + // Write SSID and password to the EEPROM |
| 154 | + writeEEPROM(); |
| 155 | + |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | +void ESP8266WifiConnect::scanNetwork() |
| 160 | +{ |
| 161 | + // Init return value |
| 162 | + apHTMLtable = ""; |
| 163 | + |
| 164 | + // Set WiFi to station mode and disconnect from an AP if it was previously connected |
| 165 | + Serial.println("Setting WiFi mode to STA"); |
| 166 | + WiFi.mode(WIFI_STA); |
| 167 | + WiFi.disconnect(); |
| 168 | + delay(100); |
| 169 | + |
| 170 | + // WiFi.scanNetworks will return the number of networks found |
| 171 | + Serial.println("Scanning network"); |
| 172 | + int numNetworks = WiFi.scanNetworks(); |
| 173 | + |
| 174 | + if(numNetworks > 0) |
| 175 | + { |
| 176 | + // Create a HTML table for each network found |
| 177 | + apHTMLtable += "<table>"; |
| 178 | + apHTMLtable += "<tr>"; |
| 179 | + apHTMLtable += "<th>SSID</th>"; |
| 180 | + apHTMLtable += "<th>Encryption</th>"; |
| 181 | + apHTMLtable += "<th>Signal strength (dBm)</th>"; |
| 182 | + apHTMLtable += "</tr>"; |
| 183 | + for (int i = 0; i < numNetworks; i++) |
| 184 | + { |
| 185 | + apHTMLtable += "<tr>"; |
| 186 | + apHTMLtable += "<td>"; |
| 187 | + apHTMLtable += WiFi.SSID(i); |
| 188 | + apHTMLtable += "</td>"; |
| 189 | + apHTMLtable += "<td>"; |
| 190 | + apHTMLtable += getEncryptionString(WiFi.encryptionType(i)); |
| 191 | + apHTMLtable += "</td>"; |
| 192 | + apHTMLtable += "<td>"; |
| 193 | + apHTMLtable += WiFi.RSSI(i); |
| 194 | + apHTMLtable += "</td>"; |
| 195 | + apHTMLtable += "</tr>"; |
| 196 | + } |
| 197 | + apHTMLtable += "</table>"; |
| 198 | + } |
| 199 | + else |
| 200 | + apHTMLtable = "No networks found!"; |
| 201 | + |
| 202 | + // Set WiFi to soft AP mode |
| 203 | + delay(100); |
| 204 | + Serial.println("Setting WiFi to Soft AP"); |
| 205 | + WiFi.softAP(ssidAP); |
| 206 | +} |
| 207 | + |
| 208 | +String ESP8266WifiConnect::getEncryptionString(uint8_t encryptionType) |
| 209 | +{ |
| 210 | + String type; |
| 211 | + |
| 212 | + if (encryptionType == ENC_TYPE_WEP) |
| 213 | + type = "WEP"; |
| 214 | + else if(encryptionType == ENC_TYPE_TKIP) |
| 215 | + type = "WPA2-TKIP"; |
| 216 | + else if(encryptionType == ENC_TYPE_CCMP) |
| 217 | + type = "WPA2-AES"; |
| 218 | + else if(encryptionType == ENC_TYPE_NONE) |
| 219 | + type = "None"; |
| 220 | + else if(encryptionType == ENC_TYPE_AUTO) |
| 221 | + type = "Auto"; |
| 222 | + else |
| 223 | + type = "Unknown"; |
| 224 | + |
| 225 | + return type; |
| 226 | +} |
| 227 | + |
| 228 | +void ESP8266WifiConnect::readEEPROM() |
| 229 | +{ |
| 230 | + // Init iterator |
| 231 | + int it = 0; |
| 232 | + ssid = ""; |
| 233 | + password = ""; |
| 234 | + |
| 235 | + // Read the length variable of the SSID data |
| 236 | + ssidLength = byte(EEPROM.read(it)); |
| 237 | + it++; |
| 238 | + |
| 239 | + // Read the SSID data |
| 240 | + for (int i = 0; i < ssidLength; i++) |
| 241 | + { |
| 242 | + ssid += char(EEPROM.read(it)); |
| 243 | + it++; |
| 244 | + } |
| 245 | + |
| 246 | + // Read the length variable of the password data |
| 247 | + passLength = byte(EEPROM.read(it)); |
| 248 | + it++; |
| 249 | + |
| 250 | + // Read the password datai |
| 251 | + for (int i = 0; i < passLength; i++) |
| 252 | + { |
| 253 | + password += char(EEPROM.read(it)); |
| 254 | + it++; |
| 255 | + } |
| 256 | + |
| 257 | + Serial.print("ssid: "); |
| 258 | + Serial.println(ssid); |
| 259 | + Serial.print("password: "); |
| 260 | + Serial.println(password); |
| 261 | + Serial.print("ssid length: "); |
| 262 | + Serial.println(ssidLength); |
| 263 | + Serial.print("password length: "); |
| 264 | + Serial.println(passLength); |
| 265 | +} |
| 266 | + |
| 267 | +void ESP8266WifiConnect::writeEEPROM() |
| 268 | +{ |
| 269 | + // Init iterator |
| 270 | + int it = 0; |
| 271 | + |
| 272 | + // First clear the EEPROM |
| 273 | + clearEEPROM(); |
| 274 | + |
| 275 | + // Write the length variable of the SSID data |
| 276 | + ssidLength = ssid.length(); |
| 277 | + EEPROM.write(it, ssidLength); |
| 278 | + it++; |
| 279 | + Serial.print("ssidLength: "); |
| 280 | + Serial.println(ssidLength); |
| 281 | + |
| 282 | + // Write the SSID data |
| 283 | + for (int i = 0; i < ssidLength; i++) |
| 284 | + { |
| 285 | + EEPROM.write(it, ssid[i]); |
| 286 | + it++; |
| 287 | + Serial.println(ssid[i]); |
| 288 | + } |
| 289 | + |
| 290 | + // Write the length variable of the password data |
| 291 | + passLength = password.length(); |
| 292 | + EEPROM.write(it, passLength); |
| 293 | + it++; |
| 294 | + Serial.print("passLength: "); |
| 295 | + Serial.println(passLength); |
| 296 | + |
| 297 | + // Write the password data |
| 298 | + for (int i = 0; i < passLength; i++) |
| 299 | + { |
| 300 | + EEPROM.write(it, password[i]); |
| 301 | + it++; |
| 302 | + Serial.println(password[i]); |
| 303 | + } |
| 304 | + |
| 305 | + // Commit |
| 306 | + EEPROM.commit(); |
| 307 | + Serial.println("Finished writing EEPROM"); |
| 308 | +} |
| 309 | + |
| 310 | +void ESP8266WifiConnect::clearEEPROM() |
| 311 | +{ |
| 312 | + int dataLength = ssidLength + passLength + 2; |
| 313 | + for (int i = 0; i < dataLength; ++i) |
| 314 | + EEPROM.write(i, 0); |
| 315 | + |
| 316 | + // Commit |
| 317 | + EEPROM.commit(); |
| 318 | + Serial.println("Finished clearing EEPROM"); |
| 319 | +} |
0 commit comments