1

I have tested this code on Lolin Nodemcu v3 and it is working perfectly. However when I switched to ESP-01 The Wifimanager works but no custom parameters are saved although WIFI Credentials are saved.

#include <FS.h>          // this needs to be first, or it all crashes and burns...
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>

int ledPin = 2; 
char mqtt_username[40]; char mqtt_password[40];     String mqttusername;    String mqttpassword;   

WiFiManager wm;

bool shouldSaveConfig = false;
void saveConfigCallback () {  Serial.println("Should save config");  shouldSaveConfig = true; }

void setup() {
  Serial.begin(115200);  pinMode(ledPin, OUTPUT);  digitalWrite(ledPin, LOW);
  
  setupSpiffs();  wm.setSaveConfigCallback(saveConfigCallback);
  WiFiManagerParameter custom_mqtt_username("username", "username", mqtt_username, 40);
  WiFiManagerParameter custom_mqtt_password("password", "password", mqtt_password, 40);

  wm.addParameter(&custom_mqtt_username);  wm.addParameter(&custom_mqtt_password);
  
  strcpy(mqtt_username, custom_mqtt_username.getValue());     mqttusername = custom_mqtt_username.getValue();
  strcpy(mqtt_password, custom_mqtt_password.getValue());     mqttpassword = custom_mqtt_password.getValue();

  if (shouldSaveConfig) {    Serial.println("saving config");        DynamicJsonDocument doc(1024);
    doc["mqtt_username"] = mqtt_username;    doc["mqtt_password"] = mqtt_password;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {  Serial.println("failed to open config file for writing"); }
    serializeJson(doc, Serial);    serializeJson(doc, configFile);    configFile.close();    Serial.println("");    //end save
  }
  while (WiFi.status() != WL_CONNECTED) { delay(100); Serial.print(".");  resetbtn(); }  Serial.println("");
  Serial.print("Connected to WiFi: ");  Serial.println(WiFi.SSID());
  Serial.print("IP: ");  Serial.println(WiFi.localIP());
  Serial.println("connected...yeey :)");
}

void loop() { 
Serial.println("user is: ");   Serial.println(mqtt_username);
Serial.println("pass is: ");   Serial.println(mqtt_password);
}

void setupSpiffs(){
//   SPIFFS.format();
  Serial.println("mounting FS...");

  if (SPIFFS.begin()) {    Serial.println("** Mounting file system **");
    if (SPIFFS.exists("/config.json")) {      Serial.println("** Reading config file **");      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        size_t size = configFile.size();        std::unique_ptr<char[]> buf(new char[size]);
        configFile.readBytes(buf.get(), size);        DynamicJsonDocument doc(1024);        DeserializationError error = deserializeJson(doc, buf.get());
        if (error) {          Serial.print(F("deserializeJson() failed: "));          Serial.println(error.c_str());          return;        }
        strcpy(mqtt_username, doc["mqtt_username"]);             strcpy(mqtt_password, doc["mqtt_password"]);     
      } else {        Serial.println("** Failed to load json config **");      }
      configFile.close();      Serial.println("** Closed file **");
    }
  } else {    Serial.println("** Failed to mount FS **");  }
}

void resetbtn() {    if ( digitalRead(ledPin) == HIGH ) {  wm.startConfigPortal("Burooq");    Serial.println("connected...yeey :)");  }   }

At the serial Monitor I get:

*WM: [2] <- HTTP WiFi save  
*WM: [3] Method: POST
*WM: [2] Parameters 
*WM: [2] -------------------- 
*WM: [2] username: test
*WM: [2] password: test
*WM: [2] -------------------- 
*WM: [3] Sent wifi save page 
*WM: [2] processing save 
*WM: [2] Connecting as wifi client... 
*WM: [3] STA static IP:
*WM: [2] setSTAConfig static ip not set, skipping 
*WM: [1] CONNECTED:
*WM: [1] Connecting to NEW AP: MYSSID
*WM: [3] Using Password: SSIDPASS
*WM: [3] WiFi station enable 
*WM: [3] enableSTA PERSISTENT ON 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] Connect to new AP [SUCCESS] 
*WM: [1] Got IP Address: 
*WM: [1] XXX.XXX.XXX.XXX
Should save config
*WM: [2] disconnect configportal 
*WM: [2] restoring usermode STA
*WM: [2] wifi status: WL_CONNECTED
*WM: [2] wifi mode: STA
*WM: [1] config portal exiting 
connected...yeey :)

Connected to WiFi: MYSSID
IP: XXX.XXX.XXX.XXX
connected...yeey :)
user is: 

pass is: 

user is: 

pass is: 

user is: 

pass is: 

user is: 

pass is: 
1
  • 1
    the code which should store the values runs before WiFiManager is started
    – Juraj
    Feb 24 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.