So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By mrlightsman
#82006 I have been using wifimanager for some time but wish to replace it in my code because I don’t like some aspects of it. Although it is excellent work.

For my current project I am using wifimulti as shown in the Arduino IDE examples. With the exception that I moved it to void setupwifi() and call it from the setup().

I am using webserver to serve up a page that I enter parameters. For 8 of those parameters, I’d like to enter the SSID and PW for use by the esp to connect to WiFi in wifimulti.

In order to achieve this, I want the esp to be in softap mode first. Then, once credentials are entered, I want the softap-STA mode to start (so both modes are always on).

Wifimulti seems to run in softap-sta mode once connected to WiFi. Can I start the softap before moving to softap-sta?

Yes. I am reinventing the wifimanager wheel, for the most part. The two main differences I am trying to achieve is to always have the “admin page” (the captive portal in wifimanager) available. And, I want to have multiple WiFi networks to choose from as in Wifimulti.

Any suggestions would be greatly appreciated. I’m thinking I need to first start the softap in setup() then invoke the wifimulti as part of handling the server webpage?? But, if I do this I want the esp to go right back into softap-sta mode if it has been configured and the loses power. On power up, I want it to be in softap-sta mode. Maybe by using if statements to examine the char where I am storing SSID for wifimulti?

Thank you.
User avatar
By mrlightsman
#82014 Here is my code to date. I use json & SPIFFS to store the SSID & PW variables to use in wiFiMulti. I also include ap_SSID and ap_Password to set the credentials for the softAP.

Here are the specific lines of code I am trying to figure out how to get value= to be a variable

Code: Select all<FORM METHOD="POST"action="/">

<h3>Access Point Credentials</h3>
<table>
<tr><th>Name</th><td><input type="text" name="custom_ap_SSID" value=ap_SSID></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_ap_Password" value=a_Password></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID1" value=SSID1></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW1" value=PW1></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID2" value=SSID2></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW2" value=PW2></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID3" value=SSID3></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW3" value=PW3></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID4" value=SSID4></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW4" value=PW4></td></tr>
</table>
<input type="submit" value="Submit Changes">
</form>


And for context, here is my entire code. Again, thank you for your help.
Code: Select all#include <FS.h>
#include <arduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>


ESP8266WiFiMulti wifiMulti;
ESP8266WebServer server(80);


String webPage; //to store html code from htmlpage

//Define WifiMulti Variables;
char SSID1[15];
char SSID2[15];
char SSID3[15];
char SSID4[15];
char PW1[15];
char PW2[15];
char PW3[15];
char PW4[15];
 
//Define the local AP variables
char ap_SSID[15] = "New_Box"; //SSID for ESP when in AP mode will be overwritten by json
char ap_Password[15] = "password";          //Password for ESP when in AP mode will be overwritten by json

const char htmlPage[]PROGMEM=R"=====(
<!DOCTYPE html>
<html>
<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
<link rel=\"icon\" href=\"data:,\">

<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}
table { border-collapse:collapse; width:35%; margin-left:auto; margin-right:auto; }
th { padding:12px; background-color: #0043af; color: white; }
tr { border:1px solid #ddd; padding:12px; }
tr:hover { background-color: #bcbcbc; }
td { border: none; padding:12px; }

.button {background-color: #195B6A; border: none; color: white; padding: 12px 40px;
text-align: center; font-family: \"Trebuchet MS\", Arial;
text-direction: none; font-size: 30px; margin: 2px; cursor: pointer;}

server.send(200,"</style></head><body><h1>Device Configuration Portal</h1>

<FORM METHOD="POST"action="/">

<h3>Access Point Credentials</h3>
<table>
<tr><th>Name</th><td><input type="text" name="custom_ap_SSID" value=ap_SSID></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_ap_Password" value=a_Password></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID1" value=SSID1></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW1" value=PW1></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID2" value=SSID2></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW2" value=PW2></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID3" value=SSID3></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW3" value=PW3></td></tr>
<tr><th>Name</th><td><input type="text" name="custom_SSID4" value=SSID4></td></tr>
<tr><th>Password</th><td><input type="text" name="custom_PW4" value=PW4></td></tr>
</table>
<input type="submit" value="Submit Changes">
</form>
</body>
</html>
)=====";

void handleConfigurationPage() {
  webPage=htmlPage;

 server.arg("custom_SSID1").toCharArray(SSID1,15);
 server.arg("custom_PW1").toCharArray(PW1,15);
 server.arg("custom_SSID2").toCharArray(SSID2,15);
 server.arg("custom_PW2").toCharArray(PW2,15);
 server.arg("custom_SSID3").toCharArray(SSID3,15);
 server.arg("custom_PW3").toCharArray(PW3,15);
 server.arg("custom_SSID4").toCharArray(SSID4,15);
 server.arg("custom_PW4").toCharArray(PW4,15);
 server.arg("custom_ap_SSID").toCharArray(ap_SSID,15);
 server.arg("custom_ap_Password").toCharArray(ap_Password,15);


 Serial.println("Text Received, contents:");
 Serial.println(SSID1);
 Serial.println(PW1);
 Serial.println(SSID2);
 Serial.println(PW2);
 Serial.println(SSID3);
 Serial.println(PW3);
 Serial.println(SSID4);
 Serial.println(PW4);
 Serial.println(ap_SSID);
 Serial.println(ap_Password);

 server.send(200,"text/html",webPage);
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["SSID1"] = SSID1;
    json["PW1"] = PW1;
    json["SSID2"] = SSID2;
    json["PW2"] = PW2;
    json["SSID3"] = SSID3;
    json["PW3"] = PW3;
    json["SSID4"] = SSID4;
    json["PW4"] = PW4;
    json["ap_SSID"] = ap_SSID;
    json["ap_Password"] = ap_Password;   

        File configFile = SPIFFS.open("/config.json", "w");
    if (!configFile) {
      Serial.println("failed to open config file for writing");
    }

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

void setupWifi() {
//  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(SSID1, PW1);
  wifiMulti.addAP(SSID2, PW2);
  wifiMulti.addAP(SSID3, PW3);
  wifiMulti.addAP(SSID4, PW4);

  Serial.println("Connecting Wifi...");
  while (wifiMulti.run() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("Connected to SSID: ");
  Serial.println(WiFi.SSID());
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
//  Serial.println();


  // Setup pins to off position
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH);


//  SPIFFS.format();  //clean FS, for testing

  //read configuration from FS json
  Serial.println("mounting FS...");

  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(SSID1, json["SSID1"]);
          strcpy(PW1, json["PW1"]);
          strcpy(SSID1, json["SSID2"]);
          strcpy(PW1, json["PW2"]);
          strcpy(SSID1, json["SSID3"]);
          strcpy(PW1, json["PW3"]);
          strcpy(SSID1, json["SSID4"]);
          strcpy(PW1, json["PW4"]);
          strcpy(ap_SSID, json["ap_SSID"]);
          strcpy(ap_Password, json["ap_Password"]);
         
          //add other variables here

        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  //end read
 
  setupWifi();

  server.on("/", handleConfigurationPage);
  server.begin();
  Serial.println("HTTP Server Started");

}
void loop() {
  server.handleClient();
}