2017-11-19 04:45:50 +01:00
|
|
|
//#include <DNSServer.h>
|
|
|
|
|
|
|
|
//DNSServer dnsServer;
|
|
|
|
|
|
|
|
//const byte DNS_PORT = 53;
|
|
|
|
|
|
|
|
// AP mode password
|
|
|
|
const char WiFiAPPSK[] = "";
|
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
// Wi-Fi network to connect to (leave blank to connect to saved network, or to start in AP mode)
|
|
|
|
const char* ssid = "";
|
|
|
|
const char* password = "";
|
2017-11-19 04:45:50 +01:00
|
|
|
|
|
|
|
#define HOSTNAME "ESP8266-" ///< Hostname. The initializeWiFi function adds the Chip ID at the end.
|
|
|
|
|
|
|
|
#define DEBUG_WIFI 1
|
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
WiFiMode mode = WIFI_STA; // connect to existing Wi-Fi network
|
|
|
|
//WiFiMode mode = WIFI_AP; // act as an Access Point, creating a new Wi-Fi network
|
|
|
|
//WiFiMode mode = WIFI_AP_STA; // act as both a client and Access Point (mesh mode)
|
2017-11-19 04:45:50 +01:00
|
|
|
|
|
|
|
template <typename Generic>
|
2017-11-26 17:20:09 +01:00
|
|
|
void debugPrint(Generic text) {
|
2017-11-19 04:45:50 +01:00
|
|
|
if (DEBUG_WIFI) {
|
2017-11-26 17:20:09 +01:00
|
|
|
Serial.print(text);
|
2017-11-19 04:45:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
template <typename Generic>
|
|
|
|
void debugPrintln(Generic text) {
|
|
|
|
if (DEBUG_WIFI) {
|
|
|
|
Serial.println(text);
|
2017-11-19 04:45:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String getWiFiJson() {
|
|
|
|
String hostname = String(HOSTNAME);
|
|
|
|
hostname += String(ESP.getChipId(), HEX);
|
|
|
|
|
|
|
|
String json = "{";
|
|
|
|
|
|
|
|
json += "\"status\":\"" + String(WiFi.status()) + "\"";
|
|
|
|
json += ",\"localIP\":\"" + WiFi.localIP().toString() + "\"";
|
|
|
|
json += ",\"softAPIP\":\"" + WiFi.softAPIP().toString() + "\"";
|
|
|
|
json += ",\"hostname\":\"" + hostname + "\"";
|
|
|
|
json += ",\"ssid\":\"" + WiFi.SSID() + "\"";
|
|
|
|
json += ",\"rssi\":\"" + String(WiFi.RSSI()) + "\"";
|
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
json += ",\"networks\":[";
|
|
|
|
byte ssidCount = WiFi.scanNetworks();
|
|
|
|
for (byte i = 0; i < ssidCount; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
json += ",";
|
|
|
|
|
|
|
|
json += "{\"name\":\"" + WiFi.SSID(i) + "\",\"rssi\":\"" + String(WiFi.RSSI(i)) + "\"}";
|
|
|
|
}
|
|
|
|
|
|
|
|
json += "]";
|
2017-11-19 04:45:50 +01:00
|
|
|
|
|
|
|
json += "}";
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initializeWiFi() {
|
|
|
|
// Set Hostname.
|
|
|
|
String hostname = String(HOSTNAME);
|
|
|
|
hostname += String(ESP.getChipId(), HEX);
|
|
|
|
WiFi.hostname(hostname);
|
|
|
|
|
|
|
|
// Print hostname.
|
|
|
|
Serial.println("Hostname: " + hostname);
|
|
|
|
|
|
|
|
char hostnameChar[hostname.length() + 1];
|
|
|
|
memset(hostnameChar, 0, hostname.length() + 1);
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < hostname.length(); i++)
|
|
|
|
hostnameChar[i] = hostname.charAt(i);
|
|
|
|
|
|
|
|
// MDNS.begin(hostnameChar);
|
|
|
|
|
|
|
|
// Add service to MDNS-SD
|
|
|
|
// MDNS.addService("http", "tcp", 80);
|
|
|
|
|
|
|
|
// attempt to connect; should it fail, fall back to AP mode
|
|
|
|
// WiFi.mode(WIFI_STA);
|
|
|
|
|
|
|
|
String stored_ssid = WiFi.SSID();
|
2017-11-26 17:20:09 +01:00
|
|
|
|
|
|
|
if (ssid != NULL && password != NULL &&
|
|
|
|
ssid != "" && password != "") {
|
|
|
|
debugPrint("WiFi mode: ");
|
|
|
|
debugPrintln(WIFI_STA);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
|
|
|
debugPrint("Connecting to hard-coded SSID: ");
|
|
|
|
debugPrintln(stored_ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
}
|
|
|
|
else if (stored_ssid != NULL && stored_ssid != "") {
|
|
|
|
debugPrint("WiFi mode: ");
|
|
|
|
debugPrintln(WIFI_STA);
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
|
|
|
debugPrint("Connecting to stored SSID: ");
|
2017-11-19 04:45:50 +01:00
|
|
|
debugPrintln(stored_ssid);
|
|
|
|
WiFi.begin();
|
|
|
|
}
|
2017-11-26 17:20:09 +01:00
|
|
|
else {
|
|
|
|
debugPrintln("No stored SSID");
|
|
|
|
debugPrint("Starting soft AP: ");
|
|
|
|
debugPrintln(hostnameChar);
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
|
|
|
|
if (WiFiAPPSK != NULL) {
|
|
|
|
debugPrint(WiFi.softAP(hostnameChar, WiFiAPPSK) ? "ready" : "failed");
|
|
|
|
} else {
|
|
|
|
debugPrint(WiFi.softAP(hostnameChar) ? "ready" : "failed");
|
|
|
|
}
|
2017-11-19 04:45:50 +01:00
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
debugPrint("Connect to Wi-Fi access point: ");
|
|
|
|
debugPrintln(hostnameChar);
|
|
|
|
|
|
|
|
delay(500); // Without delay I've seen the IP address blank
|
|
|
|
debugPrint("AP IP address: ");
|
|
|
|
debugPrintln(WiFi.softAPIP());
|
|
|
|
|
|
|
|
// dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
|
|
|
|
// dnsServer.start(DNS_PORT, "*", WiFi.softAPIP());
|
|
|
|
}
|
2017-11-19 04:45:50 +01:00
|
|
|
|
|
|
|
webServer.on("/wifi", HTTP_POST, []() {
|
|
|
|
String ssid = webServer.arg("ssid");
|
|
|
|
String password = webServer.arg("password");
|
|
|
|
|
|
|
|
char ssidChars[50];
|
|
|
|
ssid.toCharArray(ssidChars, 50);
|
|
|
|
|
|
|
|
char passwordChars[50];
|
|
|
|
password.toCharArray(passwordChars, 50);
|
|
|
|
|
2017-11-26 17:20:09 +01:00
|
|
|
debugPrint("Connecting to new SSID: ");
|
2017-11-19 04:45:50 +01:00
|
|
|
debugPrintln(ssid);
|
|
|
|
|
|
|
|
WiFi.begin(ssidChars, passwordChars);
|
|
|
|
|
|
|
|
webServer.sendHeader("Location", "/wifi.htm");
|
|
|
|
webServer.send(303);
|
|
|
|
});
|
|
|
|
|
|
|
|
webServer.on("/wifi", HTTP_GET, []() {
|
|
|
|
String json = getWiFiJson();
|
|
|
|
webServer.send(200, "application/json", json);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|