Good morning.
I am using WIFI manager to spin up a AP for me to fill in my wifi credentials for home. I understand that if it fails to connect then it spins up the AP again which is fine.
If it connects to my home wifi successfully i want it to connect and retry forever until i reset the wifi settings myself with a button. It seems to just try and connect and then create the AP again...
Here is my code:
void setup() {
Serial.begin(9600);
bool spiffsSetup = loadConfigFile();
if (!spiffsSetup)
{
Serial.println(F("Forcing config mode as there is no saved config"));
forceConfig = true;
}
setupWifiManager();
}
void loop() {
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
ESP.restart();
delay(500);
}
reconnect_mqtt();
client.loop();
}
void setupWifiManager()
{
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 20);
WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 10);
WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 50);
WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, 50);
// WiFiManager
// Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//set config save notify callback
wifiManager.setSaveConfigCallback(saveConfigCallback);
//add all your parameters here
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_mqtt_port);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
// if (forceConfig)
// // Run if we need a configuration
// {
// if (!wifiManager.startConfigPortal("AP"))
// {
// Serial.println("failed to connect and hit timeout");
// delay(3000);
// //reset and try again, or maybe put it to deep sleep
// ESP.restart();
// delay(5000);
// }
// }
// else
// {
// if (!wifiManager.autoConnect("AP"))
// {
// Serial.println("failed to connect and hit timeout");
// delay(3000);
// // if we still have not connected restart and try all over again
// ESP.restart();
// delay(5000);
// }
// }
//erases the wifi settings from chip. Going to add this to mqtt command and button press.
// wifiManager.resetSettings();
//TEMP AP SSID
// wifiManager.autoConnect("AP");
WiFi.forceSleepWake();
WiFi.setSleepMode(WIFI_NONE_SLEEP);
// if (!(wifiManager.autoConnect("AP"))) // check if wifi is connected within configportaltimeout if not restart
// {
// Serial.println("Failed to connect");
// WiFi.begin();
// ESP.restart();
// delay(5000);
// }
wifiManager.autoConnect("AP");
// if (!wifiManager.autoConnect("AP")) {
// Serial.println("failed to connect and hit timeout");
// delay(3000);
// //reset and try again, or maybe put it to deep sleep
// ESP.restart();
// delay(5000);
// }
// wifiManager.setConnectTimeout(180);
//
// wifiManager.setTimeout(180);
//
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
ESP.restart();
delay(500);
}
// if you get here you have connected to the WiFi
Serial.println("Connected.");
strncpy(mqtt_server, custom_mqtt_server.getValue(), sizeof(mqtt_server));
strncpy(mqtt_port, custom_mqtt_port.getValue(), sizeof(mqtt_port));
strncpy(mqtt_user, custom_mqtt_user.getValue(), sizeof(mqtt_user));
strncpy(mqtt_pass, custom_mqtt_pass.getValue(), sizeof(mqtt_pass));
//save the custom parameters to FS
if (shouldSaveConfig) {
saveConfigFile();
}
// server.begin();
}