Problem with wifi manager

The weirdest thing, i have written a sketch for dimming up and dimming down leds using FadeLed library. Everything works well as when i am not using wifi manager. As soon as i insert the wifi manager stuff it disconnects and does not reconnect. Am i missing something? The wifi code used is the same as i have used for other projects and they work well…

CODE WHICH WORKS WITHOUT WIFI MANAGER
/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <FadeLed.h>
WidgetRTC rtc;
BlynkTimer timer;




//WIFI Variables
char auth[] = "";
char ssid[] = "";
char pass[] = "";

//RTC/Time Input Widget Variables
char currentTime[9];
char currentDate[11];
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int dayNumber;
bool activeToday = false;
bool scheduler = true;
bool schedulerStatusChange = false;

//LED FADE ATTRIBUTES & VARIABLES
FadeLed CH1(D4);
int FadeIN = 0;
int FadeOUT = 0;
int maxPWM;
unsigned long FadeInTime;
unsigned long FadeOutTime;

void LedFadeIn ()
{
  CH1.setTime(FadeInTime);
  CH1.set(maxPWM);

  
}

void LedFadeOut ()
{
  CH1.setTime(FadeOutTime);
  CH1.set(0);

}

    
BLYNK_WRITE(V1)  //CH1 TIME IMPUT WIDGET
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  dayNumber = weekday();
  activeToday = false;
  unsigned int OnDays = 0;
  for (int i = 1; i <= 7; i++)    // Process weekdays (1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
  {
    if (t.isWeekdaySelected(i))
    {
      OnDays++;
      if(i==dayNumber)
      {
        activeToday = true;
      }
      Serial.println(String("Day ") + i + " is selected");
    }
  }
  if (OnDays == 0)
  {
    scheduler = false;
    }
    else
    {
      scheduler = true;
    }
    if (scheduler != schedulerStatusChange)
    {
      schedulerStatusChange = scheduler;
    }
}

BLYNK_WRITE(V2)   //CH1 FADEIN TIME WIDGET
{
  FadeIN = param.asInt();
  FadeInTime = (FadeIN * 60000);   //Param is in minutes
  Serial.print("LedFadeIn Time: ");
  Serial.println(FadeInTime);
}

BLYNK_WRITE(V3)   //CH1 FADEOUT TIME WIDGET
{
  FadeOUT = param.asInt();
  FadeOutTime = (FadeOUT * 60000);
  Serial.print("LedFadeOut Time: ");
  Serial.println(FadeOutTime);
}

BLYNK_WRITE(V4)   //CH1 UV INTENSITY WIDGET
{
  maxPWM = param.asInt();

  Serial.print("Intensity: ");
  Serial.println(maxPWM);
}

void ProgressBar()
{
  //int CL;
  CH1.getCurrent();
  //CL = (maxPWM / CH1.getCurrent()*100.0);

    Serial.print("Progress: ");
    Serial.println(CH1.getCurrent());
    Blynk.virtualWrite(V5, CH1.getCurrent());
}

void TimeCheck()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second()); //Get RTC time
  Serial.print("Current Time: ");
  Serial.println(currentTime);
  sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec); //Get start time from widget
  Serial.print("Start Time: ");
  Serial.println(startTime);  
  sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec); //Get stop time from widget
  Serial.print("Stop Time: ");
  Serial.println(stopTime);
  
  if (activeToday == true)
  {
    if (hour() == SThour)
    {
      if (minute() == STmin)
      {
        LedFadeIn();
        Serial.println("Doing something now");
        }
        else if (hour() == SPhour)
        {
          if (minute() == SPmin)
          {
            LedFadeOut();
            Serial.println("Stopping something now");
          }
        }
    }
  }
}

void ClockDisplay()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  sprintf(currentDate, "%02d/%02d/%04d", day(), month(), year());

  Blynk.virtualWrite(V6, currentTime);    //Send time to TIME widget
  Blynk.virtualWrite(V7, currentDate);    //Send date to DATE widget
}

BLYNK_CONNECTED()   //Connect to Blynk server
{
  Blynk.syncAll();
}

void SyncChannels()
{
  //CH1.update();
  FadeLed::update();
}

void setup()
{
  // Debug console

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  //Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  //CH1 = LEDFader(Channel1);
  //CH1.set_value(0);
  CH1.set(0);
  rtc.begin();
  setSyncInterval(30);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(500L, TimeCheck);
  timer.setInterval(10L, SyncChannels);
  timer.setInterval(500L, ProgressBar);
 
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  //CH1.update();  
}

CODE WHICH DOES NOT WORK WITH WIFI MANAGER
 
//LIBRARIES//
#include <FS.h>                   

#include <ESP8266WiFi.h>  
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          
#include <ArduinoJson.h>                 
#include <BlynkSimpleEsp8266.h>                 //MUST BE IN THIS ORDER

//#include <ArduinoOTA.h>
//#include <ESP8266mDNS.h>  //For OTA w/ ESP8266
// #include <ESPmDNS.h>  //For OTA w/ ESP32
//#include <WiFiUdp.h>

#include <WidgetRTC.h>
#include <FadeLed.h>
WidgetRTC rtc;
BlynkTimer timer;


//PIN ASSIGNMENTS//

//RTC & TIME IMPUT ATTRIBUTES & VARIABLES
char currentTime[9];    //RTC time
char currentDate[11];   //RTC date
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int dayNumber;
bool activeToday = false;
bool scheduler = true;
bool schedulerStatusChange = false;

//WIFI GLOBALS//
char mqtt_server[40];
char mqtt_port[6] = "8080";
char blynk_token[34] = "YOUR_BLYNK_TOKEN";
bool shouldSaveConfig = false;
//char modulename[] = "AquArt Blynk Test";      //Insert app name

//LED FADE ATTRIBUTES & VARIABLES
FadeLed CH1(D1);
int FadeIN = 0;
int FadeOUT = 0;
int maxPWM;
unsigned long FadeInTime;
unsigned long FadeOutTime;
float PWMint;

//CONNECT TO BLYNK SERVER//
BLYNK_CONNECTED()
{
  Blynk.syncAll();
  rtc.begin();
}

void saveConfigCallback ()
{
  shouldSaveConfig = true;
}

void setup() 
{
  Serial.begin(115200);
  
  if (SPIFFS.begin())
  {
  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);
      DynamicJsonBuffer jsonBuffer;
      JsonObject& json = jsonBuffer.parseObject(buf.get());
      json.printTo(Serial);
      if (json.success())
      {
        strcpy(mqtt_server, json["mqtt_server"]);
        strcpy(mqtt_port, json["mqtt_port"]);
        strcpy(blynk_token, json["blynk_token"]);
      }
    }
  }
  }
  
  WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
  WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6);
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 33);

  WiFiManager wifiManager;

  wifiManager.setSaveConfigCallback(saveConfigCallback);

  wifiManager.addParameter(&custom_mqtt_server);
  wifiManager.addParameter(&custom_mqtt_port);
  wifiManager.addParameter(&custom_blynk_token);

  wifiManager.resetSettings();                                         //Used for reset only comment out if not needed

  if (!wifiManager.autoConnect("AquArt Blynk Test", "password"))   //Insert App name
  {
    delay(3000);
    //ESP.reset();
    //delay(5000);
  }
  strcpy(mqtt_server, custom_mqtt_server.getValue());
  strcpy(mqtt_port, custom_mqtt_port.getValue());
  strcpy(blynk_token, custom_blynk_token.getValue());

  if (shouldSaveConfig)
  {
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["mqtt_server"] = mqtt_server;
    json["mqtt_port"] = mqtt_port;
    json["blynk_token"] = blynk_token;

    File configFile = SPIFFS.open("/config.json", "w");
    json.printTo(Serial);
    json.printTo(configFile);
    configFile.close();
  }

  Blynk.config(blynk_token);
  bool result = Blynk.connect();
  if (result != true)
{
  wifiManager.resetSettings();
  ESP.reset();
  delay (5000);
}
  //ArduinoOTA.setHostname(modulename);
  //ArduinoOTA.begin();          
         
//OTHER SET UP STUFF HERE
  CH1.set(0);
  rtc.begin();
  setSyncInterval(30);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(500L, TimeCheck);
  timer.setInterval(10L, SyncChannels);
  timer.setInterval(500L, ProgressBar);
}

void loop()
{
Blynk.run();
timer.run();
//ArduinoOTA.handle();
}

void ClockDisplay()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
  sprintf(currentDate, "%02d/%02d/%04d", day(), month(), year());

  Blynk.virtualWrite(V6, currentTime);    //Send time to TIME widget
  Blynk.virtualWrite(V7, currentDate);    //Send date to DATE widget
}


BLYNK_WRITE(V1)  //CH1 TIME IMPUT WIDGET
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  dayNumber = weekday();
  activeToday = false;
  unsigned int OnDays = 0;
  for (int i = 1; i <= 7; i++)    // Process weekdays (1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
  {
    if (t.isWeekdaySelected(i))
    {
      OnDays++;
      if(i==dayNumber)
      {
        activeToday = true;
      }
      Serial.println(String("Day ") + i + " is selected");
    }
  }
  if (OnDays == 0)
  {
    scheduler = false;
    }
    else
    {
      scheduler = true;
    }
    if (scheduler != schedulerStatusChange)
    {
      schedulerStatusChange = scheduler;
    }
}

BLYNK_WRITE(V2)   //CH1 FADEIN TIME WIDGET
{
  FadeIN = param.asInt();
  FadeInTime = (FadeIN * 60000);   //Param is in minutes
  Serial.print("LedFadeIn Time: ");
  Serial.println(FadeInTime);
}

BLYNK_WRITE(V3)   //CH1 FADEOUT TIME WIDGET
{
  FadeOUT = param.asInt();
  FadeOutTime = (FadeOUT * 60000);
  Serial.print("LedFadeOut Time: ");
  Serial.println(FadeOutTime);
}

BLYNK_WRITE(V4)   //CH1 UV INTENSITY WIDGET
{
  PWMint = param.asInt();
  maxPWM = map(PWMint, 0, 100, 0, 255);
  Serial.print("Intensity: ");
  Serial.println(maxPWM);
}

void TimeCheck()
{
  sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second()); //Get RTC time
  sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec); //Get start time from widget
  sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec); //Get stop time from widget

  if (activeToday == true)
  {
    if (hour() == SThour)
    {
      if (minute() == STmin)
      {
        LedFadeIn();
        }
        else if (hour() == SPhour)
        {
          if (minute() == SPmin)
          {
            LedFadeOut();
          }
        }
    }
  }
}

void LedFadeIn ()
{
  CH1.setTime(FadeInTime, true);
  CH1.set(maxPWM);
}

void LedFadeOut ()
{
  CH1.setTime(FadeOutTime, true);
  CH1.set(0);
}

void ProgressBar()
{
  CH1.getCurrent();
  Blynk.virtualWrite(V5, CH1.getCurrent());
}

void SyncChannels()
{
  FadeLed::update();
}
//END//

I’m no expert on how wifiManager actually works. So usually I try to copy it exactly as the github example shows. It looks like you are missing a configFile.close(); in the mounting FS portion of the code. Not sure if this is what is causing your problem, but may be worth giving a try. IMO the serial prints are very handy in trying to debug a piece of code, not sure why you decided to leave them out.

//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(mqtt_server, json["mqtt_server"]);
          strcpy(mqtt_port, json["mqtt_port"]);
          strcpy(blynk_token, json["blynk_token"]);

        } else {
          Serial.println("failed to load json config");
        }
        configFile.close(); //YOU ARE MISSING THIS
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
//end read

I will try this. I left out the serial prints just to shorten the code for uploading to the forum

Always a bad idea!
I’ve lost count of the number of wasted hours because someone wasn’t running exactly the same code that they posted.

Pete.

I only took out the serial prints of the wifi manager which are the standard from the wifi manager

Ok i think the issue is with the Fadeled.h library. Some type of conflict. I have tried the wifi manager code with all my other projects and it works. with this one at setup on the wifi manager interface you connect then 2 seconds later disconnects and does not reconnect???

I do recall a thread about 12-18 months ago with a FastLED library issue, but I can’t find it now.

If you’re not run I g the latest FastLED library then upgrade. If you are running the latest then downgrade!

Also play around with the ESP core version you’re using. Anything later than 2.4.2 can be dodgy sometimes.

Pete.