BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Auth token with on demand portal

Hello People !!

I am using wifimanager to avoid hard coding the ssid pass and auth token…

But the problem is during the power fail scenario obviously the device boots quicker that the router. So when the ESP doesnt get ssid to connect… It will turn into an Hotspot, which i dont want…

So i decided to use on demand portal it triggers when i push a button. But to store anything other that ssid or pass we need to setup parameters. And the parameters can be stored on SPIFFS but without !wifiManager.autoConnect i cannot call back the stored ssid and pass. and unfortunately this is what causing the ESP to go to AP mode (!wifiManager.autoConnect("AP", "pass")) to get configured when no routers are found…

Is there a way out of this ??
This is not a Blynk related question… But this may be helpful if someone is having the same issue…

Can anyone help me out ??

Try

This simple-to-use WM will connect and reconnect automatically.

There’s a simple solution to the initial problem that you’ve described:

Configuration Portal Timeout

If you need to set a timeout so the ESP doesn’t hang waiting to be configured, for instance after a power failure, you can add

wifiManager.setConfigPortalTimeout(180);

which will wait 3 minutes (180 seconds). When the time passes, the autoConnect function will return, no matter the outcome. Check for connection and if it’s still not established do whatever is needed (on some modules I restart them to retry, on others I enter deep sleep)

I really don’t follow the rest of what you’re saying, but it’s irrelevant if you use the solution described above.

Pete.

Yes !! This is what i am doing… I am waiting just 60 sec. Because by that time the router will probably be up and running…

But i am looking for a more polished way… But what you said and what i was following is a crude method.

is there a way like just call wifi.begin and read the credentials saved previously ??

I if you are reading the credentials out of SPIFFS and into variables within your code then it shouldn’t make any difference when and where you use these credentials within your code.

That’s why I didn’t understand what you wer saying in the rest of your earlier post.

Pete.

Can you please explain a bit more !? Like how to read the ssid n pass stored in SPIFFS ?

There are lots of example of WiFiManager with SPIFFS out there, so a bit of research will show you how to do it.
Here’s a bit of code that I use for something that’s non Blynk related, but the principals are the same:

The code to retrieve the data from SPIFFS starts around line 36

You need to ensure that you’re using the correct version of ArduinoJson.h (see line 8) and that when you flash the code to your device you allocate a small amount of SPIFFS memory for WiFiManager to use.

Pete.

I am already using the same lines of code you just said above

if (SPIFFS.begin()) {
    Serial.println("Mounting the Credential's");
    if (SPIFFS.exists("/config.json")) {
      Serial.println("Reading config file");
      File configFile = SPIFFS.open("/config.json", "r");
      if (configFile) {
        Serial.println("Opened config file");
        size_t size = configFile.size();
        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(server, json["server"]);
          strcpy(port, json["port"]);
          strcpy(auth, json["auth"]);

        } else {
          Serial.println("Failed to load json config");
        }
      }
    }
  } else {
    Serial.println("Failed to mount Config");
  }

I want to know how to get the ssid and pass stored so that i can use it with wifi.begin
instead of !wifiManager.autoConnect (which is currently doing the work of wifi.begin) this will turn into AP mode if router is not available.

MY BASIC IDEA IS

  1. Call on demand portal with a button pressed (loop function)
  2. In setup it just needs to read the stored ssid and pass that we entered in on demand portal.
  3. wifi.begin will take over and do the connection work.

So when there is no router available to connect it just continues to work without turning into a AP mode.

All i am trying to do is get the values stored by wifimanager on SPIFFS and pass it on to wifi.begin

I really dont know if this can be done.

Okay, I think I see where you’re coming from now. The issue is that WEifiManager uses SPIFFS to store other credentials, not SSID and WiFi Password. These are stored in a different area of the ESP’s memory and are stored regardless of whether you’re using WiFiManager or not.

WifiManager will be invoked if the stored SSID and password can’t be used to successfully connect to WiFi during the timeout period, if you use WiFiManager in autoConnect mode (line 110 of my sketch). However, it seems that you don’t want that, you only want to launch the WiFiManager captive portal on demand - when you press a button.
WiFiManager has a simple solution for that:

On Demand Configuration Portal

If you would rather start the configuration portal on demand rather than automatically on a failed connection attempt, then this is for you.

Instead of calling autoConnect() which does all the connecting and failover configuration portal setup for you, you need to use startConfigPortal() . Do not use BOTH.

Example usage

void loop() { // is configuration portal requested? if ( digitalRead(TRIGGER_PIN) == LOW ) { WiFiManager wifiManager; wifiManager.startConfigPortal(“OnDemandAP”); Serial.println(“connected…yeey :)”); } }

See example for a more complex version. OnDemandConfigPortal

I’d suggest that you read more about the configuration options available within WiFiManager and look at some of the other examples before starting down a different path.

One other thing…

Hopefully you’re planning to use a timer to poll the button or an interrupt to trigger the portal when the button is pressed rather than polling a pin in your void loop?

Pete.

This wont work. Because in the setup we are not calling any wifi connection functions. So we will not be able to connect to the wifi network after a reboot.

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

// select which pin will trigger the configuration portal when set to LOW
// ESP-01 users please note: the only pins available (0 and 2), are shared 
// with the bootloader, so always set them HIGH at power-up
#define TRIGGER_PIN 0


void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");

  pinMode(TRIGGER_PIN, INPUT);
}


void loop() {
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == LOW ) {
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;

    //reset settings - for testing
    //wifiManager.resetSettings();

    //sets timeout until configuration portal gets turned off
    //useful to make it all retry or go to sleep
    //in seconds
    //wifiManager.setTimeout(120);

    //it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration

    //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
    //WiFi.mode(WIFI_STA);
    
    if (!wifiManager.startConfigPortal("OnDemandAP")) {
      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);
    }

    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
  }


  // put your main code here, to run repeatedly:

}

And

adding startConfigPortal() in the setup doesnt solve the problem because even this will turn into AP mode when no router is found.

I tried all known ways to me only after that i am here to learn something new that you guys follow. Or get new ideas…

Yes… Sorry for my bad English.

The example isn’t meant to be a fully-fledged solution, just an example of how to launch the portal on demand.
You seem to have decided that the WiFiManager built-in solution isn’t elegant enough for you, and you don’t want to put the effort into getting the alternatives to work, so I’ll leave you to explore alternatives.

One thing I would say is that if you go down the route of the alternative that @khoih has suggested then this uses modified versions of the current BlynkSimpleEspxxxx.h library files. The originals of these files may be updated in future, and if the modified versions aren’t updated to keep track with them then you could find yourself in a dead-end in future. Also, those modifications may break something else that hasn’t been identified yet.
Either way, as they aren’t the core Blynk library files then you may find that you get less support from the community when you’re using them.

Here’s an example of a similar issue of using a modified version of the core Blynk library files that I came across recently…

Pete.

Thank you for the support…