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

Advance Time Input Widget

Hi I am trying to make and advanced time input widget work for a future much larger project. I have base the code on code which was from Costas and Gunner. The problem is the it does not works as it should. It appears from the Serial monitor that it gets stuck at " if (activeToday == true) of function TimeCheck(). Could someone possibly look over my code a point me to my mistake. Much appreciated.

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial



#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "68c0c1f4f4e74f5cb561a29cad50b8cd";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

//Time Input 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;

WidgetRTC rtc;
BlynkTimer timer;

BLYNK_WRITE(V10)
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  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;
    }
}

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)
      {
        Serial.println("Doing something now");
        digitalWrite(2, LOW); // Turn ON built-in LED
        }
        else if (minute() < STmin)
        {
          Serial.println("Will do something");
        }
        else if (minute() > STmin)
        {
          Serial.println("Did something");
        }
        else
        {
          Serial.println("Clueless");
        }
    }
    if (hour() == SPhour)
    {
      if (minute() == SPmin)
      {
        Serial.println("Stopping something now");
        digitalWrite(2, HIGH); // Turn OFF built-in LED
      }
      else if (minute() < SPmin)
      {
        Serial.println("Will stop something");
        }
        else if (minute() > SPmin)
        {
          Serial.println("Stopped something");
          }
          else
          {
            Serial.println("Clueless");
          }
    }
    Serial.println("----------");
  }
}

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

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

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

void setup()
{
  // Debug console
  Serial.begin(9600);
  digitalWrite(2, HIGH);
  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);
  rtc.begin();
  setSyncInterval(360);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(30000L, TimeCheck);
}

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

:rofl: :rofl: :rofl: Sorry guys, couldn’t resist that one!

Your problem lies in this piece of code:

The activeToday flag is never being changed to true because dayNumber is always null (0).
The current day number is obtained with the weekday() command and this doesn’t exist in your code. You should add this line to your BLYNK_WRITE(V10) function:

dayNumber = weekday();

like this:

BLYNK_WRITE(V10)
{
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  dayNumber = weekday(); // <----- New line added
  activeToday = false;
  unsigned int OnDays = 0;

I see from the admin screens that your ISP is in west London.
I’m in the Shepherds Bush area, are we anywhere near eachother?

BTW, you need to generate a new Auth code before your larger project goes live, otherwise we’ll be able to mess with your settings :wink:, although it was handy for me to be able to check your app settings in this case.

Pete.

1 Like

Thanks for that i will test that now. As for the Auth code the sketch is currently named test. Once i get things right and with the bigger programme i want to write i will create a new project. BTW I am based in Twickenham.

1 Like

Never wait to change your AUTH code… strange things can be implemented by anyone who sees the Auth you posted in this forum :wink:

Do I need to be worried about you:worried::worried::worried::worried:…Jokes aside thanks for the advice

1 Like

Worried about me???.. Not at all :innocent: Aside from making others aware, I am one for helping, not harming.

But it is a valid warning!!.. A Cloud Auth is like a worldwide key to allow ANYONE to openly or even secretly view and control your Project, and not something to be all “Oh well, I will get around to changing it later” about.

1 Like

I used the code above and I can turn the output on and off by Blynk’s timer, but the days of the week are indifferent. Whether selected or not, the output will always time out. I already deselected every day of the week and the departure remained timed.

I’d suggest that you post the exact code you are using (correctly formatted with triple backticks of course), along with some screenshots of the app and details of the exact behaviour you are seeing.

Pete.

Performing tests I realized that I was clearing the days of the week and still leaving the hours. Maybe it’s a bug, but it was solved by resetting the times.