[GH-ISSUE #370] Change default SoftAP SSID & password on runtime from config page. #311

Open
opened 2026-02-28 01:24:40 +03:00 by kerem · 7 comments
Owner

Originally created by @bkrajendra on GitHub (May 23, 2017).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/370

Just like routers, I want to have feature to change default AP mode SSID and/or password.
As WiFiManager currently goes into AP mode if its not able to connect to saved Wlan ssid/key, it can be little security issue. Any one who knows default AP password can connect and modify WiFi config.

So I want it like professional routers, where they provide default SSID n password in manual and then they offer it to change when login for first time. Also it can be changed latter on.

Originally created by @bkrajendra on GitHub (May 23, 2017). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/370 Just like routers, I want to have feature to change default AP mode SSID and/or password. As WiFiManager currently goes into AP mode if its not able to connect to saved Wlan ssid/key, it can be little security issue. Any one who knows default AP password can connect and modify WiFi config. So I want it like professional routers, where they provide default SSID n password in manual and then they offer it to change when login for first time. Also it can be changed latter on.
Author
Owner

@bkrajendra commented on GitHub (Jan 26, 2018):

Dear @tablatronix anything wrong with post?

<!-- gh-comment-id:360878069 --> @bkrajendra commented on GitHub (Jan 26, 2018): Dear @tablatronix anything wrong with post?
Author
Owner

@tablatronix commented on GitHub (Jan 26, 2018):

There is no default ap password you specify it in your code
autoconnect(SSID,PASSWORD);

Use your own code and a custom parameter to change the password

<!-- gh-comment-id:360891778 --> @tablatronix commented on GitHub (Jan 26, 2018): There is no default ap password you specify it in your code `autoconnect(SSID,PASSWORD);` Use your own code and a custom parameter to change the password
Author
Owner

@bkrajendra commented on GitHub (Jan 26, 2018):

What I mean by default password is the one we provide in code which is hardcoded in autoconnect(SSID,PASSWORD);. We can not change this at runtime after flashing hardware.

What i mean was, is it possible to create an interface for changing this AP password, like we do in routers to change admin password.?

<!-- gh-comment-id:360897254 --> @bkrajendra commented on GitHub (Jan 26, 2018): What I mean by default password is the one we provide in code which is hardcoded in autoconnect(SSID,PASSWORD);. We can not change this at runtime after flashing hardware. What i mean was, is it possible to create an interface for changing this AP password, like we do in routers to change admin password.?
Author
Owner

@tablatronix commented on GitHub (Jan 26, 2018):

Yeah it can be done with parameters by user, not sure it needs to be in core. Maybe someone can make an example

<!-- gh-comment-id:360924953 --> @tablatronix commented on GitHub (Jan 26, 2018): Yeah it can be done with parameters by user, not sure it needs to be in core. Maybe someone can make an example
Author
Owner

@bkrajendra commented on GitHub (Jan 27, 2018):

ok.. I'll give it a try.

<!-- gh-comment-id:360948045 --> @bkrajendra commented on GitHub (Jan 27, 2018): ok.. I'll give it a try.
Author
Owner

@mrlightsman commented on GitHub (Mar 7, 2018):

@bkrajendra
Did you ever figure out a way to use parameters to change the ESP SSID and PW via the captive portal? If so, please share how you did it. With code example, preferably. Thanks.

<!-- gh-comment-id:371181279 --> @mrlightsman commented on GitHub (Mar 7, 2018): @bkrajendra Did you ever figure out a way to use parameters to change the ESP SSID and PW via the captive portal? If so, please share how you did it. With code example, preferably. Thanks.
Author
Owner

@bkrajendra commented on GitHub (Mar 7, 2018):

@tablatronix
I guess its kind of tricky and Ive not tried yet but can be done. To start of, just use parameter to store ssid and password, then just retrieve them back while setting up.

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

char ap_ssid[18] = "admin";
char ap_pass[18] = "admin";
//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;
}

void setup()
{
  if (SPIFFS.begin()) {
    Serial.println("mounted file system");
    if (SPIFFS.exists("/config.json")) {
      //file exists, reading and loading
      Serial.println("reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("opened config file");
        size_t size = configFile.size();
        // Allocate a buffer to store contents of the file.
        std::unique_ptr<char[]> buf(new char[size]);

        configFile.readBytes(buf.get(), size);
        DynamicJsonBuffer jsonBuffer;
        JsonObject& json = jsonBuffer.parseObject(buf.get());
        json.printTo(Serial);
        if (json.success()) {
          Serial.println("\nparsed json");
          strcpy(ap_ssid, json["ap_ssid"]);
          strcpy(ap_pass, json["ap_pass"]);
        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }

  WiFiManagerParameter custom_ap_ssid("SSID", "ap ssid", ap_ssid, 18);
  WiFiManagerParameter custom_ap_pass("PASS", "ap passport", ap_pass, 18);

 //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_ap_ssid);
  wifiManager.addParameter(&custom_ap_pass);

 if (!wifiManager.autoConnect(ap_ssid, ap_pass)) {
    Serial.println("failed to connect and hit timeout");
    delay(3000);
    //reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(5000);
  }
Serial.println("connected...yeey :)");
  strcpy(ap_ssid, custom_ap_ssid.getValue());
  strcpy(ap_pass, custom_ap_pass.getValue());

 //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["ap_ssid"] = ap_ssid;
    json["ap_pass"] = ap_pass;
    File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
    //end save
  }

  Serial.println("local ip");
  Serial.println(WiFi.localIP());

}
void loop() {
  // put your main code here, to run repeatedly:


}

It can be like this.
Not tested.

<!-- gh-comment-id:371250669 --> @bkrajendra commented on GitHub (Mar 7, 2018): @tablatronix I guess its kind of tricky and Ive not tried yet but can be done. To start of, just use parameter to store ssid and password, then just retrieve them back while setting up. ``` #include <FS.h> //this needs to be first, or it all crashes and burns... #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson char ap_ssid[18] = "admin"; char ap_pass[18] = "admin"; //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; } void setup() { if (SPIFFS.begin()) { Serial.println("mounted file system"); if (SPIFFS.exists("/config.json")) { //file exists, reading and loading Serial.println("reading config file"); File configFile = SPIFFS.open("/config.json", "r"); if (configFile) { Serial.println("opened config file"); size_t size = configFile.size(); // Allocate a buffer to store contents of the file. std::unique_ptr<char[]> buf(new char[size]); configFile.readBytes(buf.get(), size); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { Serial.println("\nparsed json"); strcpy(ap_ssid, json["ap_ssid"]); strcpy(ap_pass, json["ap_pass"]); } else { Serial.println("failed to load json config"); } } } } else { Serial.println("failed to mount FS"); } WiFiManagerParameter custom_ap_ssid("SSID", "ap ssid", ap_ssid, 18); WiFiManagerParameter custom_ap_pass("PASS", "ap passport", ap_pass, 18); //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_ap_ssid); wifiManager.addParameter(&custom_ap_pass); if (!wifiManager.autoConnect(ap_ssid, ap_pass)) { Serial.println("failed to connect and hit timeout"); delay(3000); //reset and try again, or maybe put it to deep sleep ESP.reset(); delay(5000); } Serial.println("connected...yeey :)"); strcpy(ap_ssid, custom_ap_ssid.getValue()); strcpy(ap_pass, custom_ap_pass.getValue()); //save the custom parameters to FS if (shouldSaveConfig) { Serial.println("saving config"); DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); json["ap_ssid"] = ap_ssid; json["ap_pass"] = ap_pass; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { Serial.println("failed to open config file for writing"); } json.printTo(Serial); json.printTo(configFile); configFile.close(); //end save } Serial.println("local ip"); Serial.println(WiFi.localIP()); } void loop() { // put your main code here, to run repeatedly: } ``` It can be like this. Not tested.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#311
No description provided.