wificonfig still not working

- some error when writing/reading from file
	setting ssid and password through web and then reconnecting wifi works
 = may change storage variant in file to json
This commit is contained in:
FrYakaTKoP
2017-10-08 17:33:46 +02:00
parent c3f7c45f46
commit 5aa7375f7a
2 changed files with 78 additions and 18 deletions

View File

View File

@@ -18,13 +18,16 @@
SoftwareSerial laserSerial(14, 12, false, 256); SoftwareSerial laserSerial(14, 12, false, 256);
char ssid[50] = "tojoin"; String ssid2 = "XTZ-72315";
char password[50] = ""; String password2 = "dgzv-6erv-63wl-vpzz";
String ssid = "";
String password = "";
int wifiMode = 0; // 0: client, 1: AP (default) int wifiMode = 0; // 0: client, 1: AP (default)
int wifiTimeout = 10000; //ms int wifiTimeout = 20000; //ms
/* Soft AP network parameters */ /* Soft AP network parameters */
char *ssidAP = "ESP-test"; char *ssidAP = "laserAp";
IPAddress apIP(192, 168, 4, 1); // note: update metaRefreshStr string if ip change! IPAddress apIP(192, 168, 4, 1); // note: update metaRefreshStr string if ip change!
IPAddress netMsk(255, 255, 255, 0); IPAddress netMsk(255, 255, 255, 0);
@@ -64,15 +67,26 @@ void readWifiConfigFile() {
Serial.println("Reading wifi config"); Serial.println("Reading wifi config");
String s = f.readStringUntil('\n'); String s = f.readStringUntil('\n');
if (s.substring(0, 4) == "mode") { if (s.substring(0, 4) == "mode") {
s.substring(5, 6).toInt(); wifiMode = s.substring(5, 6).toInt();
Serial.print("wifiMode=");
Serial.print(wifiMode);
Serial.println(";");
} }
s = f.readStringUntil('\n'); s = f.readStringUntil('\n');
if (s.substring(0, 4) == "ssid") { if (s.substring(0, 4) == "ssid") {
s.substring(5).toCharArray(ssid, 50); ssid = String();
ssid = s.substring(5);
Serial.print("ssid=");
Serial.print(ssid);
Serial.println(";");
} }
s = f.readStringUntil('\n'); s = f.readStringUntil('\n');
if (s.substring(0, 3) == "pwd") { if (s.substring(0, 3) == "pwd") {
s.substring(5).toCharArray(password, 50); password = String();
password = s.substring(4);
Serial.print("password=");
Serial.print(password);
Serial.println(";");
} }
f.close(); f.close();
} }
@@ -181,7 +195,7 @@ void handleMotor(String uri)
void handleSsid(String uri) void handleSsid(String uri)
{ {
uri.substring(6).toCharArray(ssid, 50); ssid = uri.substring(6);
String json = "{ \"handledSsid\":"; String json = "{ \"handledSsid\":";
json += "{\"ssid\":\"" + (String)ssid + "\"}}"; json += "{\"ssid\":\"" + (String)ssid + "\"}}";
@@ -190,7 +204,7 @@ void handleSsid(String uri)
} }
void handlePwd(String uri) void handlePwd(String uri)
{ {
uri.substring(5).toCharArray(password, 50); password = uri.substring(5);
String json = "{ \"handledPwd\":"; String json = "{ \"handledPwd\":";
json += "{\"password\":\"" + (String)password + "\"}}"; json += "{\"password\":\"" + (String)password + "\"}}";
server.send(200, "text/json", json); server.send(200, "text/json", json);
@@ -224,7 +238,7 @@ void handleSaveConf() {
f.close(); f.close();
success = true; success = true;
} }
if(success == true) if (success == true)
{ {
server.send(200, "text/plain", "saved config to file, reset to apply"); server.send(200, "text/plain", "saved config to file, reset to apply");
} }
@@ -235,17 +249,23 @@ void handleSaveConf() {
} }
boolean setupWifi() { boolean setupWifi() {
// Serial.print("setupWifi"); Serial.print("setupWifi mode=");
// Serial.println(wifiMode); Serial.println(wifiMode);
if (wifiMode == 0) if (wifiMode == 0)
{ {
Serial.printf("Try connecting to %s\n", ssid); Serial.print("Connecting to ");
WiFi.begin(ssid, password); Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password2.c_str());
previousMillis = millis(); previousMillis = millis();
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print(".");
Serial.print(WiFi.status());
if (millis() - previousMillis >= wifiTimeout) { if (millis() - previousMillis >= wifiTimeout) {
Serial.println("");
Serial.println("connection timedout"); Serial.println("connection timedout");
return false; return false;
} }
@@ -261,7 +281,6 @@ boolean setupWifi() {
Serial.printf("Starting SoftAP %s\n", ssidAP); Serial.printf("Starting SoftAP %s\n", ssidAP);
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
WiFi.softAP(ssidAP); WiFi.softAP(ssidAP);
Serial.println("");
Serial.print("SoftAP started! IP address: "); Serial.print("SoftAP started! IP address: ");
Serial.println ( WiFi.softAPIP() ); Serial.println ( WiFi.softAPIP() );
return true; return true;
@@ -284,10 +303,50 @@ void setup(void) {
} }
//WIFI INIT //WIFI INIT
WiFi.disconnect();
delay(100);
readWifiConfigFile(); readWifiConfigFile();
delay(100);
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
// Serial.print(n);
// Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
// Serial.print(i + 1);
// Serial.print(": ");
// Serial.print(WiFi.SSID(i));
// Serial.print(" (");
// Serial.print(WiFi.RSSI(i));
// Serial.print(")");
// Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
delay(10);
if (WiFi.SSID(i) == ssid) {
Serial.println("found ssid");
}
else if (WiFi.SSID(i) == ssid.c_str()) {
Serial.println("found ssid.c_str");
}
else if (WiFi.SSID(i) == ssid2) {
Serial.println("found ssid2");
}
else if (WiFi.SSID(i) == ssid2.c_str()) {
Serial.println("found ssid2.c_str()");
}
}
}
boolean wifiConnected = setupWifi(); boolean wifiConnected = setupWifi();
if (!wifiConnected) if (!wifiConnected)
{ {
Serial.println("set wifiMode=1");
wifiMode = 1; // fallback to AP mode wifiMode = 1; // fallback to AP mode
setupWifi(); setupWifi();
} }
@@ -308,6 +367,7 @@ void setup(void) {
//server.on("/wwc", HTTP_GET, writeWifiConfigFile); //server.on("/wwc", HTTP_GET, writeWifiConfigFile);
//server.on("/rwc", HTTP_GET, readWifiConfigFile); //server.on("/rwc", HTTP_GET, readWifiConfigFile);
server.on("/saveconf", HTTP_GET, handleSaveConf); server.on("/saveconf", HTTP_GET, handleSaveConf);
server.on("/sw", HTTP_GET, setupWifi);
server.on("/heap", HTTP_GET, []() { server.on("/heap", HTTP_GET, []() {
String json = "{"; String json = "{";
json += "\"heap\":" + String(ESP.getFreeHeap()); json += "\"heap\":" + String(ESP.getFreeHeap());