8 Channel Aquarium Led Lighting Controller Powered by BLYNK

Update.

I got the WiFiManager problem sorted out. Seems I had the Blynk.config(blynk_token); & Blynk.connect(); in the wrong order, which caused the ESP to crash repeatedly. I’ve posted the corrected code below.
Mind you, you don’t need the fancy PcB that I created to use this code. Simply connect the positive PWM lead of any commercially available 5V PWM dimmable led driver to the Wemos D1 Mini I/O pins(D1 - D8) and make sure that the Mini shares a common ground with the led driver to complete the PWM signal circuit.

Here’s the QR code followed by the working code;

/**************************************************************
   
   WEMOS D1 MINI (ESP8266) ANALOG  DIMMING CODE WITH 8 CHANNELS.
   Controlled by BLYNK
 **************************************************************/
#include <FS.h> // needs to be first in includes.
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ArduinoOTA.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

//included libraries for WiFiManager - AutoConnectWithFSParameters
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <ArduinoJson.h>

char blynk_token[34] = "YOUR_BLYNK_TOKEN";//added from WiFiManager - AutoConnectWithFSParameters
//flag for saving data
bool shouldSaveConfig = false;

//callback notifying the need to save config
void saveConfigCallback () {
  Serial.println("Should save config");
  shouldSaveConfig = true;
}


char Date[16];
char Time[16];
long startseconds = 0;         // start time in seconds
long stopseconds = 0;          // stop  time in seconds
long nowseconds = 0;           // time  now  in seconds
bool isFirstConnect = true;

int fadetime = 0;
long fadetimeseconds = 0;
long fadetimemillis = 0;

int minPWM = 1;// variable for min PWM value. keep at 1 to avoid crashing the ledFade()
byte fadeIncrement = 1; //How smooth to fade? Uses all 1023 steps available.




int maxPWM0 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM1 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM2 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM3 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM4 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM5 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM6 = 0; // variable for max PWM value attached to BLYNK Virtual pin.
int maxPWM7 = 0; // variable for max PWM value attached to BLYNK Virtual pin.


int currentFadePosition0 = 0;// don't change this!
int currentFadePosition1 = 0;// don't change this!
int currentFadePosition2 = 0;// don't change this!
int currentFadePosition3 = 0;// don't change this!
int currentFadePosition4 = 0;// don't change this!
int currentFadePosition5 = 0;// don't change this!
int currentFadePosition6 = 0;// don't change this!
int currentFadePosition7 = 0;// don't change this!

unsigned long previousFadeMillis0;// millis() timing Variable, just for fading
unsigned long previousFadeMillis1;// millis() timing Variable, just for fading
unsigned long previousFadeMillis2;// millis() timing Variable, just for fading
unsigned long previousFadeMillis3;// millis() timing Variable, just for fading
unsigned long previousFadeMillis4;// millis() timing Variable, just for fading
unsigned long previousFadeMillis5;// millis() timing Variable, just for fading
unsigned long previousFadeMillis6;// millis() timing Variable, just for fading
unsigned long previousFadeMillis7;// millis() timing Variable, just for fading


long stepWaitTime0 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime1 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime2 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime3 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime4 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime5 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime6 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)
long stepWaitTime7 = 0;  //How long to watch the clock before incrementing to the next step. (time in milliseconds)

int desiredledLevel0 = 0;
int desiredledLevel1 = 0;
int desiredledLevel2 = 0;
int desiredledLevel3 = 0;
int desiredledLevel4 = 0;
int desiredledLevel5 = 0;
int desiredledLevel6 = 0;
int desiredledLevel7 = 0;

#define pwmLED0  D1
#define pwmLED1  D2
#define pwmLED2  D3
#define pwmLED3  D4
#define pwmLED4  D5
#define pwmLED5  D6
#define pwmLED6  D7
#define pwmLED7  D8

WidgetRTC rtc;
SimpleTimer timer;

// divide your desired dimming time duration(in millis) by the maxPWM variable value / fadeIncrement variable value to
// get the stepWaitTime variable value needed.
// EXAMPLE: maxPWM of 1023/fadeIncrement of 1 to dim over 1 hour is a stepWaitTime of 3,519 millis.


void setLed() {
  if(currentFadePosition0 <= 1){
    analogWrite(pwmLED0, 0);
  }
  else{
  stepWaitTime0 = (fadetimemillis / (maxPWM0 / fadeIncrement));
  analogWrite(pwmLED0, currentFadePosition0);
  }
  if(currentFadePosition1 <= 1){
    analogWrite(pwmLED1, 0);
  }
  else{
  stepWaitTime1 = (fadetimemillis / (maxPWM1 / fadeIncrement));
  analogWrite(pwmLED1, currentFadePosition1);
  }
  if(currentFadePosition2 <= 1){
    analogWrite(pwmLED2, 0);
  }
  else{
  stepWaitTime2 = (fadetimemillis / (maxPWM2 / fadeIncrement));
  analogWrite(pwmLED2, currentFadePosition2);
  }
  if(currentFadePosition3 <= 1){
    analogWrite(pwmLED3, 0);
  }
  else{
  stepWaitTime3 = (fadetimemillis / (maxPWM3 / fadeIncrement));
  analogWrite(pwmLED3, currentFadePosition3);
  }
  if(currentFadePosition4 <= 1){
    analogWrite(pwmLED4, 0);
  }
  else{
  stepWaitTime4 = (fadetimemillis / (maxPWM4 / fadeIncrement));
  analogWrite(pwmLED4, currentFadePosition4);
  }
  if(currentFadePosition5 <= 1){
    analogWrite(pwmLED5, 0);
  }
  else{
  stepWaitTime5 = (fadetimemillis / (maxPWM5 / fadeIncrement));
  analogWrite(pwmLED5, currentFadePosition5);
  }
  if(currentFadePosition6 <= 1){
    analogWrite(pwmLED6, 0);
  }
  else{
  stepWaitTime6 = (fadetimemillis / (maxPWM6 / fadeIncrement));
  analogWrite(pwmLED6, currentFadePosition6);
  }
  if(currentFadePosition7 <= 1){
    analogWrite(pwmLED7, 0);
  }
  else{
  stepWaitTime7 = (fadetimemillis / (maxPWM7 / fadeIncrement));
  analogWrite(pwmLED7, currentFadePosition7);
  }
}

void ledFade0(unsigned long thisMillis0) {
  if (nowseconds < startseconds) {
    currentFadePosition0 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis0 - previousFadeMillis0 >= stepWaitTime0) {
      currentFadePosition0 = currentFadePosition0 + fadeIncrement;
      if (currentFadePosition0 >= maxPWM0) {
        // At max limit stop the fade
        currentFadePosition0 = maxPWM0;
      }
      // put actionable () here.
      //analogWrite(led0, currentFadePosition0);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis0 = thisMillis0;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis0 - previousFadeMillis0 >= stepWaitTime0) {
      currentFadePosition0 = currentFadePosition0 - fadeIncrement;
      if (currentFadePosition0 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition0 = minPWM;
      }
      // put actionable () here
      //analogWrite(led0, currentFadePosition0);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis0 = thisMillis0;
    }
  }
}
void ledFade1(unsigned long thisMillis1) {
  if (nowseconds < startseconds) {
    currentFadePosition1 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis1 - previousFadeMillis1 >= stepWaitTime1) {
      currentFadePosition1 = currentFadePosition1 + fadeIncrement;
      if (currentFadePosition1 >= maxPWM1) {
        // At max limit stop the fade
        currentFadePosition1 = maxPWM1;
      }
      // put actionable () here.
      //analogWrite(led1, currentFadePosition1);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis1 = thisMillis1;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis1 - previousFadeMillis1 >= stepWaitTime1) {
      currentFadePosition1 = currentFadePosition1 - fadeIncrement;
      if (currentFadePosition1 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition1 = minPWM;
      }
      // put actionable () here
      //analogWrite(led1, currentFadePosition1);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis1 = thisMillis1;
    }
  }
}
void ledFade2(unsigned long thisMillis2) {
  if (nowseconds < startseconds) {
    currentFadePosition2 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis2 - previousFadeMillis2 >= stepWaitTime2) {
      currentFadePosition2 = currentFadePosition2 + fadeIncrement;
      if (currentFadePosition2 >= maxPWM2) {
        // At max limit stop the fade
        currentFadePosition2 = maxPWM2;
      }
      // put actionable () here.
      //analogWrite(led2, currentFadePosition2);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis2 = thisMillis2;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis2 - previousFadeMillis2 >= stepWaitTime2) {
      currentFadePosition2 = currentFadePosition2 - fadeIncrement;
      if (currentFadePosition2 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition2 = minPWM;
      }
      // put actionable () here
      //analogWrite(led2, currentFadePosition2);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis2 = thisMillis2;
    }
  }
}
void ledFade3(unsigned long thisMillis3) {
  if (nowseconds < startseconds) {
    currentFadePosition3 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis3 - previousFadeMillis3 >= stepWaitTime3) {
      currentFadePosition3 = currentFadePosition3 + fadeIncrement;
      if (currentFadePosition3 >= maxPWM3) {
        // At max limit stop the fade
        currentFadePosition3 = maxPWM3;
      }
      // put actionable () here.
      //analogWrite(led3, currentFadePosition3);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis3 = thisMillis3;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis3 - previousFadeMillis3 >= stepWaitTime3) {
      currentFadePosition3 = currentFadePosition3 - fadeIncrement;
      if (currentFadePosition3 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition3 = minPWM;
      }
      // put actionable () here
      //analogWrite(led3, currentFadePosition3);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis3 = thisMillis3;
    }
  }
}
void ledFade4(unsigned long thisMillis4) {
  if (nowseconds < startseconds) {
    currentFadePosition4 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis4 - previousFadeMillis4 >= stepWaitTime4) {
      currentFadePosition4 = currentFadePosition4 + fadeIncrement;
      if (currentFadePosition4 >= maxPWM4) {
        // At max limit stop the fade
        currentFadePosition4 = maxPWM4;
      }
      // put actionable () here.
      //analogWrite(led4, currentFadePosition4);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis4 = thisMillis4;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis4 - previousFadeMillis4 >= stepWaitTime4) {
      currentFadePosition4 = currentFadePosition4 - fadeIncrement;
      if (currentFadePosition4 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition4 = minPWM;
      }
      // put actionable () here
      //analogWrite(led4, currentFadePosition4);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis4 = thisMillis4;
    }
  }
}
void ledFade5(unsigned long thisMillis5) {
  if (nowseconds < startseconds) {
    currentFadePosition5 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis5 - previousFadeMillis5 >= stepWaitTime5) {
      currentFadePosition5 = currentFadePosition5 + fadeIncrement;
      if (currentFadePosition5 >= maxPWM5) {
        // At max limit stop the fade
        currentFadePosition5 = maxPWM5;
      }
      // put actionable () here.
      //analogWrite(led5, currentFadePosition5);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis5 = thisMillis5;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis5 - previousFadeMillis5 >= stepWaitTime5) {
      currentFadePosition5 = currentFadePosition5 - fadeIncrement;
      if (currentFadePosition5 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition5 = minPWM;
      }
      // put actionable () here
      //analogWrite(led5, currentFadePosition5);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis5 = thisMillis5;
    }
  }
}
void ledFade6(unsigned long thisMillis6) {
  if (nowseconds < startseconds) {
    currentFadePosition6 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis6 - previousFadeMillis6 >= stepWaitTime6) {
      currentFadePosition6 = currentFadePosition6 + fadeIncrement;
      if (currentFadePosition6 >= maxPWM6) {
        // At max limit stop the fade
        currentFadePosition6 = maxPWM6;
      }
      // put actionable () here.
      //analogWrite(led6, currentFadePosition6);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis6 = thisMillis6;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis6 - previousFadeMillis6 >= stepWaitTime6) {
      currentFadePosition6 = currentFadePosition6 - fadeIncrement;
      if (currentFadePosition6 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition6 = minPWM;
      }
      // put actionable () here
      //analogWrite(led6, currentFadePosition6);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis6 = thisMillis6;
    }
  }
}
void ledFade7(unsigned long thisMillis7) {
  if (nowseconds < startseconds) {
    currentFadePosition7 = minPWM;
  }
  if (nowseconds > startseconds && nowseconds < stopseconds) {
    // is it time to start the Sunrise?
    // if not, nothing happens
    if (thisMillis7 - previousFadeMillis7 >= stepWaitTime7) {
      currentFadePosition7 = currentFadePosition7 + fadeIncrement;
      if (currentFadePosition7 >= maxPWM7) {
        // At max limit stop the fade
        currentFadePosition7 = maxPWM7;
      }
      // put actionable () here.
      //analogWrite(led7, currentFadePosition7);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis7 = thisMillis7;
    }
  }
  if (nowseconds > stopseconds) {
    // is it time to start the Sunset yet?
    // if not, nothing happens
    if (thisMillis7 - previousFadeMillis7 >= stepWaitTime7) {
      currentFadePosition7 = currentFadePosition7 - fadeIncrement;
      if (currentFadePosition7 <= minPWM) {
        // At min limit stop the fade
        currentFadePosition7 = minPWM;
      }
      // put actionable () here
      //analogWrite(led7, currentFadePosition7);
      // reset millis for the next iteration (fade timer only)
      previousFadeMillis7 = thisMillis7;
    }
  }
}
// Digital clock display of the time
void clockDisplay() {
  String currentDate = String(month()) + " " + day() + " " + year();
  nowseconds = ((hour() * 3600) + (minute() * 60) + second());

  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details
  if (minute() < 10 && second() < 10) {
    String currentTime = String(hour()) + ":0" + minute() + ":0" + second();
    Blynk.virtualWrite(V8, currentTime);
  }
  if (minute() < 10 && second() > 9 ) {
    String currentTime = String(hour()) + ":0" + minute() + ":" + second();
    Blynk.virtualWrite(V8, currentTime);
  }
  if (minute() > 9 && second() < 10) {
    String currentTime = String(hour()) + ":" + minute() + ":0" + second();
    Blynk.virtualWrite(V8, currentTime);
  }
  if (minute() >= 10 && second() >= 10) {
    String currentTime = String(hour()) + ":" + minute() + ":" + second();
    Blynk.virtualWrite(V8, currentTime);
  }

}

BLYNK_WRITE(V0) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel0 = param.asInt();// channel 1
  maxPWM0 = map(desiredledLevel0, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V1) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel1 = param.asInt();// channel 2
  maxPWM1 = map(desiredledLevel1, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V2) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel2 = param.asInt();// channel 3
  maxPWM2 = map(desiredledLevel2, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V3) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel3 = param.asInt();// channel 4
  maxPWM3 = map(desiredledLevel3, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V4) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel4 = param.asInt();// channel 5
  maxPWM4 = map(desiredledLevel4, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V5) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel5 = param.asInt();// channel 6
  maxPWM5 = map(desiredledLevel5, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V6) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel6 = param.asInt();// channel 7
  maxPWM6 = map(desiredledLevel6, 0, 100, minPWM, 1023);
}
BLYNK_WRITE(V7) {// slider widget to set the maximum led level from the Blynk App.
  desiredledLevel7 = param.asInt();// channel 8
  maxPWM7 = map(desiredledLevel7, 0, 100, minPWM, 1023);
}


BLYNK_WRITE(V9) {// slider widget to set the led fade duration up tp 3 hours.
  fadetime = param.asInt();
  fadetimeseconds = map(fadetime, 0, 180, 1, 10800);// 3 hour fade duration is max
  fadetimemillis  = map(fadetime, 0, 180, 1, 10800000);// 3 hour fade duration is max
  Serial.print("Fade Time in seconds =");
  Serial.println(fadetimeseconds);
}

void activetoday() {       // check if schedule should run today
  if (year() != 1970) {
    Blynk.syncVirtual(V3); // sync timeinput widget
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
  }
}

BLYNK_WRITE(V10) {

  TimeInputParam t(param);
  Serial.print("Checked schedule at: ");
  Serial.println(Time);
  int dayadjustment = -1;
  if (weekday() == 1) {
    dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
  }
  if (t.isWeekdaySelected((weekday() + dayadjustment))) { //Time library starts week on Sunday, Blynk on Monday
    Serial.println("Schedule ACTIVE today");
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    if (nowseconds >= startseconds) {

      if (nowseconds <= startseconds + 90) {  // 90s on 60s timer ensures 1 trigger command is sent
        // code here

      }
    }
    else {
      Serial.println("Relay not on");// nothing more to do here, waiting for relay to be turned on later today
    }
    stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
    if (nowseconds >= stopseconds) {
      // 90s on 60s timer ensures 1 trigger command is sent
      if (nowseconds <= stopseconds + 90) {
        // code here

      }
    }
    else {
      if (nowseconds >= startseconds) { // only show if motor has already started today
        Serial.println("Relay is still ON");
        // nothing more to do here, waiting for motor to be turned off later today
      }
    }
  }
  else {
    Serial.println("Schedule INACTIVE today");
    // nothing to do today, check again in 1 minutes time
  }
  Serial.println();
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if (Blynk.connect()) {
      BLYNK_LOG("Reconnected");
    }
    else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void setup()
{
  ArduinoOTA.begin();
  Serial.begin(115200);
  
  //The following code is borrowed from WiFiManager
  //clean FS, for testing
  //SPIFFS.format();

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

        } else {
          Serial.println("failed to load json config");
        }
      }
    }
  } else {
    Serial.println("failed to mount FS");
  }
  //end read
  // The extra parameters to be configured (can be either global or just in the setup)
  // After connecting, parameter.getValue() will get you the configured value
  // id/name placeholder/prompt default length
  WiFiManagerParameter custom_blynk_token("blynk", "blynk token", blynk_token, 34);
  Serial.println(blynk_token);
  //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_blynk_token);

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

  //set minimu quality of signal so it ignores AP's under that quality
  //defaults to 8%
  wifiManager.setMinimumSignalQuality();

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

  //fetches ssid and pass and tries to connect
  //if it does not connect it starts an access point with the specified name
  //here  "8 CH LED CONTROL"
  //and goes into a blocking loop awaiting configuration
  if (!wifiManager.autoConnect("8 CH LED CONTROL")) {
    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("8 CH LED CONTROL connected :)");

  //read updated parameters
  strcpy(blynk_token, custom_blynk_token.getValue());

  //save the custom parameters to FS
  if (shouldSaveConfig) {
    Serial.println("saving config");
    DynamicJsonBuffer jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["blynk_token"] = blynk_token;

    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());
  Blynk.config(blynk_token);
  Blynk.connect();
  Blynk.syncAll();
  if (!blynk_token)
  {
    Serial.println("Failed to connect to Blynk server");
    wifiManager.resetSettings();// reset all settings and spool up the "8 CH LED Contoller" AP again.

    delay(1000);
  }

  Serial.println("Done");
  
  Blynk.notify("8 Ch LDD-H led PcB ONLINE");
  
  rtc.begin();
  
  timer.setInterval(1000L, setLed);
  timer.setInterval(1000L, clockDisplay);     // digital time displayed every second
  timer.setInterval(60000L, activetoday);     // check every minute if schedule should run today
  timer.setInterval(60000L, reconnectBlynk);  // check every 60s if still connected to server

  Blynk.syncAll();
}
void loop()
{
 
  ArduinoOTA.handle();
  timer.run(); // Initiates SimpleTimer
  if (Blynk.connected()) {
    Blynk.run();
  }
// get the current time, for this time around loop
  // all millis() timer checks will use these time stamps
  unsigned long currentMillis0 = millis();
  ledFade0(currentMillis0);
  unsigned long currentMillis1 = millis();
  ledFade1(currentMillis1);
  unsigned long currentMillis2 = millis();
  ledFade2(currentMillis2);
  unsigned long currentMillis3 = millis();
  ledFade3(currentMillis3);
  unsigned long currentMillis4 = millis();
  ledFade4(currentMillis4);
  unsigned long currentMillis5 = millis();
  ledFade5(currentMillis5);
  unsigned long currentMillis6 = millis();
  ledFade6(currentMillis6);
  unsigned long currentMillis7 = millis();
  ledFade7(currentMillis7);

}

3 Likes