My intention is to show messages on a LCD1602 to tell a person to connect to the WiFiManager portal to configure the WiFi.
Also to show message "Connected" when connected and "Disconnected" when disconnected.
Got so far so I realized that WiFiManager must run in "wm.setConfigPortalBlocking(false);" so other code can be executed in parallel to show/toggle messages on the display.
But how to get a flag set, to act upon, when in portal mode?
In the example:
if(wm.autoConnect("AutoConnectAP")){
Serial.println("connected...yeey :)");
}
else {
Serial.println("Configportal running");
}
Here it's easy to set a flag when entering "Portal Mode". But how to know when exiting?
I finally got it working by checking if IP 192.168.4.1 was present.. But it must be a more correct and cleaner way to do it?
Tried to implement this without success:
I just get a compile error :- (
I assume the documentation is a bit outdated since wifiManger.xxxx seam to have been replaces with wm.xxxx
I'm pretty clueless for the moment... Please help! How do I get a "bool portalRunning = true;"?
// WiFiManager in non blocking mode to toggle between messages on display.
// Also noticed that:
// Entering the wrong WiFi-password in config portal will fuck it up. Not possible to change to correct password. My code? Do not know.
// Solution seam to be a "wm.resetSettings();" or connect to another AP temorary.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows connected to SCL --> D1, SDA -> D2
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager (2.0.15-rc.1) Latest available @ 230121
WiFiManager wm;
void setup() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// put your setup code here, to run once:
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight();
//reset settings - wipe credentials for testing
//wm.resetSettings();
wm.setConfigPortalBlocking(false);
std::vector<const char *> menu = { "wifi", "restart", "exit" }; // Added by me...
wm.setMenu(menu);
wm.setConfigPortalTimeout(60);
//automatically connect using saved credentials if they exist
//If connection fails it starts an access point with the specified name
wm.setAPCallback(configModeCallback); // TEST -------------------- callback -----------------------
if (wm.autoConnect("MyPortal", "password")) {
Serial.println("connected...yeey)");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connected!!!");
// } else {
// Serial.println("Configportal running");
// runningConfigPortal = true;
}
}
//****************************************************************** callback ----------------
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode -------------------- callback -----------------------");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void setBreakAfterConfig(boolean shouldBreak);
//wm.setSaveConfigCallback(saveConfigCallback); // Get the following compile error if this line is not commented out:
/*/home/pepe/Arduino/AutoConnectNonBlocking_fred_test_callback/AutoConnectNonBlocking_fred_test_callback.ino:46:1: error: 'wm' does not name a type
46 | wm.setSaveConfigCallback(saveConfigCallback);
| ^~
exit status 1
Compilation error: 'wm' does not name a type
*/
//flag for saving data
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback() {
Serial.println("Should save config");
shouldSaveConfig = true;
}
//************************************************* End of callback *****************
unsigned int currentMillis;
unsigned int startMillis;
unsigned int WiFiErrorCount;
bool portalMode;
bool startingUp = true;
void loop() {
wm.process();
// put your main code here, to run repeatedly:
currentMillis = millis();
if (currentMillis - startMillis > 2000) { //test whether the period has elapsed
String checkMode;
checkMode = WiFi.softAPIP().toString(); // See if this returns 192.168.4.1 = Portal Mode. Did not manage to find any other way to get notice
Serial.println(checkMode); // of when exiting from "PortalMod".
if (checkMode == "192.168.4.1") {
Serial.println("###################################### IN PORTAL MODE ################### ");
portalMode = true;
startingUp = true;
} else {
portalMode = false;
}
Serial.print("Every second - WiFi.status is : ");
Serial.println(WiFi.status());
if (WiFi.status() == WL_CONNECTED) { //
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Connected!!!");
WiFiErrorCount = 0;
startingUp = false;
} else if ((WiFi.status() != WL_CONNECTED) && (portalMode == false)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("NOT Connected!!!");
lcd.setCursor(0, 1);
lcd.print("Restarting.");
lcd.print(WiFiErrorCount);
WiFiErrorCount++;
if ((WiFiErrorCount > 100) && (startingUp == false)) { // Restart device after about 200 sec if no WiFi.
ESP.restart();
}
}
startMillis = currentMillis;
}
if ((WiFi.status() != WL_CONNECTED) && (portalMode == true)) { // Show configuration messages on display if no WiFi and in PortalMode.
portalDisplayMessages();
}
if (WiFi.status() == WL_CONNECTED) {
// futureCrappyCode(); // Code dependent on an active WiFi connection.
}
}
void portalDisplayMessages() {
int currentMillis;
static unsigned int startMillis;
static unsigned int messageCount = 0;
currentMillis = millis();
if (currentMillis - startMillis > 10000) { //test whether the period has elapsed
Serial.println("Running portal, 10 seconds message switch");
switch (messageCount) {
case 0:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("AccessPoint:");
lcd.setCursor(0, 1);
lcd.print("MyPortal");
messageCount++;
break;
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Password:");
lcd.setCursor(0, 1);
lcd.print("gggggggg");
messageCount = 0;
}
startMillis = currentMillis;
}
}