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

Wi-Fi Manager problem with ESP32

#1

hello PeteKnight!
Currently I’m starting with esp32 and blynk, I have problems with wifimanager. I want to set SSID, password and Blynk token when logging in, and save the future on SPIFFS. I tried many times but failed, can you help me?
Thank you very much

0 Likes

Autoconnect Blynk (WifiManager Alternative) - ESP8266/ESP32
#2

I’ve moved your post to a new topic.
Please post your full code, along with detailes of how much SPIFFS memory you are allocating when you compile/upload your sketch, and the output you are getting in your serial monitor.

Please put triple backticks before and after your code and before and after your serial monitor output, otherwise your unformatted code/serial monitor data will be deleted.

Pete.

1 Like

#4

:cry:

0 Likes

#5

You didn’t do that, so I’ve deleted your unformatted code.
Triple backticks look like this:
```

Also, simply posting your code doesn’t help. We need to know what SPIFFS settings you used when you compiled and uploaded it, and what appears in your serial monitor when you run the code on your device and attempt to enter Wi-Fi credentials via the captive portal.

Pete.

3 Likes

#6

@hungdk

triple backticks before and after your code should look like this.

-> ```

 **your code HERE**

-> ```

I did it for you.

#define BLYNK_PRINT Serial
#include <SPIFFS.h>
#if defined ESP8266 
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#else
#include <WiFi.h>//https://github.com/esp8266/Arduino
#endif

//needed for library
#include <DNSServer.h>
#if defined ESP8266 
#include <ESP8266WebServer.h>
#else
#include <WebServer.h>
#endif
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson

#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <SimpleTimer.h>
SimpleTimer timer;

const int PIN_AP = 0;

char blynk_token[34] = “YOUR_BLYNK_TOKEN”;
bool shouldSaveConfig = false;

void saveConfigCallback ()
{
shouldSaveConfig = true;
}

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


          pinMode(PIN_AP, INPUT);

          if (SPIFFS.begin())
          {
            if (SPIFFS.exists("/config.json"))
                {
                  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);
                    DynamicJsonBuffer jsonBuffer;
                    JsonObject& json = jsonBuffer.parseObject(buf.get());
                    json.printTo(Serial);
                    if (json.success())
                    {
                      Serial.println("\nparsed json");
                      strcpy(blynk_token, json["blynk_token"]);
                    }        
                  }
                }
              } 


//end read

WiFiManagerParameter custom_blynk_token(“blynk”, “blynk token”, blynk_token, 32);
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&amp;custom_blynk_token);

if(!wifiManager.startConfigPortal(“VuonThongMinh”, “12345678”) ){


    delay(2000);
    ESP.restart();
    delay(1000);
  }


strcpy(blynk_token, custom_blynk_token.getValue());
if (shouldSaveConfig) {
DynamicJsonBuffer jsonBuffer;
JsonObject&amp; json = jsonBuffer.createObject();
json[“blynk_token”] = blynk_token;
File configFile = SPIFFS.open("/config.json", “w”);
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}

Serial.println(“local ip”);
Serial.println(WiFi.localIP());
Blynk.config(blynk_token);
bool result = Blynk.connect();
}

void loop() {
Blynk.run();
timer.run();


          int WifiValue = digitalRead(PIN_AP);
          if(WifiValue == LOW){
           WiFi.disconnect();
            ESP.restart();
           delay(1000);
          }


}

0 Likes