Time Input Widget with Hours:Minutes and Days

You’d normally use weekday(t); where t is your time library object, but it’s difficult to tell exactly what you’re doing from your code snippet.

Pete.

I already watch sketchbuilder u attach…really a big help. Thank you and have a nice day

@tester I’ve merged your new topic in with your previous one, as it’s about the same subject.
Please don’t create new topic in future if you have further questions relating to the use of the Time Input widget.

Issue 1

This is why I prefer to use the seconds since midnight value to do my time calculation, but you don’t seem to want to go down that route, so in stead you will need to do the following:

Define Global variables to hold your start hours, start minutes, start seconds and the selected days of the week - this part could be an array, or seven different variable for Mon, Tues, Wed etc.

In BLYNK_WRITE(V10) (which triggers automatically whenever the user changes the values in the time input widget) populate the start hours, minutes seconds etc variables with the values from t.getStartHour() etc.

Once every second, call a function with a BlynkTimer to check if the current hours, minutes and seconds match the start time, and if so take the appropriate action.
Be aware that you could potentially miss a start time if you are looking for an exact match to the second of the two times, because any processing delay could mean that the current seconds value has rolled-over so doesn’t exactly match the start time. Because of that, it’s better to use a flag, and do a logic test to see if the current time is greater than the start time and the flag has yet to be set.

Issue 2

You obviously can’t have this processing in BLYNK_WRITE(V10) as this is only called when the value of the Time Input widget changes. It needs to be dome in your function that is called once every hour.

You’re currently not passing any parameter to the weekday() command, despite me explaining earlier that this is required
The simplest way to determine the current day of the week in this situation is to use now() as the parameter that you pass, like this:

int currentDay = weekday(now());

Pete.

Thank you Sir!!!
Issue 1
Actually I already used your method getting the time in seconds which more simpler and precise

Issue 2
I already done this and will update tomorrow either the weekday have change since my country today is Thursday(5). And sorry for my stubborn because actually I not understand your code earlier but now I keep getting your point. Will inform you later for next update

Thats why its better to keep the discussion in one place, and ask questions if you don’t understand the answers.

Pete.

Can u explain more by giving example foe “passing any parameter”? Once again,not pro in programming and

not working for me

I found one of community said

" if you ask Google what day the 1st Jan 1970 was it will confirm it was a Thursday.

Thursday is day 5 with “regular” time libraries, like the one you are using for weekday().

So it will always be day 5 if you don’t set your clock."

Can you explain what does it mean by “set your clock”. For ur info I already use RTC widget in blynk

The weekday keyword had brackets after it. You are meant to insert a parameter (in this case the current date/time) into those brackets and the result that is returned will be the day of the week that relates to that date/time.

This means that you need to tell your hardware what the current time is. By default, when the device starts up it assumes that the time is UNIX Epoch time, which is zero hours on 1st Jan 1970.

There is no such thing as the RTC widget in Blynk IoT, this widget was only used in Blynk Legacy.
In post 3 you were using a Network Time Protocol server to get the current time…

rather than using the Blynk RTC functionality.

I think it’s time for you to share your current code in full (and please don’t double space it as you did before, it makes it much harder to read on the forum).

Pete.

Pete.

Sorry, it occured by default

#include <NTPClient.h>
#include <WiFiUdp.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
#include <Wire.h>
#include <Time.h>
#include <math.h>
#include <TimeLib.h>
#include "HX711.h"
#include "Ultrasonic.h"
#include "ServoSG90.h"
#include "RFID.h"
#include "Firebase.h"
#include "LoadCell.h"

BlynkTimer timer;
WiFiUDP ntpUDP;
//timezone local;
const long utcOffsetInSeconds = 28800;
int HH, MM, currentTime;
int scheduleTimeM, scheduleTimeN, scheduleTimeE;
int scheduleDayM, scheduleDayN, scheduleDayE;
int MmDay, MtDay, MwDay, MthDay, MfDay, MstDay, MsuDay;
NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", utcOffsetInSeconds);

BLYNK_CONNECTED() {
  Blynk.sendInternal("rtc", "sync");
  Blynk.syncVirtual(V10, V11, V12);  //dapat sync masa dan hari tpi kna cari cara display all sync hari
}
void setup() {
  Serial.begin(115200);
  //Don't Change This Setting!!!
  Blynk.begin(auth, ssid, pass, "....", 8080);
  timeClient.begin();
  timer.setInterval(10000L, checkDays);
  //timer.setInterval(1000L, checkTime);
}
void loop() {
  Blynk.run();
  timer.run();
}
// Receive UTC data - this is triggered by
// Blynk.sendInternal("utc", "tz_name") and Blynk.sendInternal("utc", "iso")
//I just follow straightly your code but no idea how to view the output in serial monitor
BLYNK_WRITE(InternalPinUTC) {
  String cmd = param[0].asStr();
  if (cmd == "tz_name") {
String tz_name = param[1].asStr();
Serial.print("Your device Timezone is:                         ");
Serial.println(tz_name);
  } else if (cmd == "iso") {
String iso_time = param[1].asStr();
Serial.print("Your device local date, time and GMT offset are: ");
Serial.println(iso_time);
Serial.println("If this information is wrong then go to Web console > Device > Metadata > Device Timezone");
Serial.println(" and choose the correct timezone then restart this device");
Serial.println();
  }
}
//Set time and days for MORNING session
BLYNK_WRITE(V10) {
  TimeInputParam t(param);
  // Process timezone
  Serial.println(String("Time zone: ") + t.getTZ());
  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
  // param[0] is the user time value selected in seconds.
  scheduleTimeM = param[0].asInt();  //already up to date
  // param[3] is the day of the week where 0=Sun .. 6=Sat
  scheduleDayM = param[3].asInt();  //need user click to update
  for (int scheduleDayM = 1; scheduleDayM <= 7; scheduleDayM++) {
if (t.isWeekdaySelected(scheduleDayM)) {
  Serial.println(String("Day ") + scheduleDayM + " is selected");
}
  }
  // if (scheduleDayM == currentDay) {
  //   Serial.println("Activity today!");
  // }
}
//To display current day
void checkDays() {
 //I comparing the difference output of weekday() and time_t method but nothing different
  int currentDay = weekday(now());
  time_t today = now();
  Serial.print("Today is day : ");
  Serial.println(day(today));  //any day always output 5
  Serial.print("Today is month : ");
  Serial.println(month(today));
  Serial.print("Today is year : ");
  Serial.println(year(today));
  Serial.print("Today is weekday : ");
  Serial.println(weekday(today));
  Serial.print("Currentday is : ");
  Serial.println(currentDay);
}
void checkTime() {
  timeClient.update();
  HH = timeClient.getHours();
  MM = timeClient.getMinutes();
  int hours = HH;
  if (hours == 0) hours = 12;
  if (hours > 12) hours = hours - 12;
  Serial.print(hours);
  Serial.print(": ");
  Serial.println(MM);  //CURRENT TIME IN HH:MM
  int currentTimeS = (HH * 3600) + (60 * MM);
  // Serial.print("Seconds for hour & minute is : ");
  // Serial.println(currentTimeS);  // convert HH:MM to seconds
  Serial.print("Morning schedule is at : "); //set in TimeInput Widget for morning session (M refer to morning)
  Serial.println(scheduleTimeM);
  if (scheduleTimeM == currentTimeS) {  //compare by seconds not HH:MM
servoMotor.attached();
scale.power_up();
rotateServo(); //Pour kebble into bowl
  } 
}

Okay, the first thing you need to do is decide which method you wnat to use for obtaining the current time.
The options are NTP, or Blynk RTC.

You have partial code relating to both methods 8n your sketch, but neither are fully implemented.

You’ve pasted the Blynk timezone code I provided, but it’s currently redundant because it’s never called.

Please don’t respond by hacking your code around further, but instead vocalise which approach you prefer to use.

Pete.

I will use NTP

In that case you should remove the following code…

You need to set-up a way of calling

when your device first starts. As it will be relying in the internet connection created by Blynk then putting it in BLYNK_CONNECTED() is probably a good idea.

Once you obtain the time from the NTP server you need to update your device’s clock with that time. This is normally done with the settime(NPT_TIME) command, passing the current time from the NTP server as the NPT_TIME parameter.

It would make sense to set-up a BlynkTimer that calls timeClient.update() and settime(NPT_TIME) every few hours.

You can’t do this…

because param[3] is a string of comma separated values that represent the day numbers that are selected in the time input widget.
If Monday, Wednesday, Thursday, Saturday and Sunday are selected then the string will look like this:
1,3,4,6,7

If no days are selected the string will be empty, and if every day is selected it will be 1,2,3,4,5,6,7

As I said before…

An array to hold a yes/no (1,0) for each day would be the simplest method to use when checking if the current day is active in the app and therefore the pet needs to be fed on that day.

Pete.

Thank you for your help. I finish with time input widget. For other Blynker reference…

Solution
For weekday() return value 5. The solution is put this code in BLYNK_CONNECTED

setTime(timeClient.getEpochTime());

For store and call days selected in variable, in TimeInput Widget

BLYNK_WRITE(V10) {
  TimeInputParam t(param);
  // param[0] is the user time value selected in seconds.
  scheduleTimeM = param[0].asInt(); //For selected time not days

  for (int i = 0; i < 7; i++) {
    sDay[i] = 0;
    if (t.isWeekdaySelected(i + 1)) {
      sDay[i] = 1;
    }
  }
}

Retrieve the saved days

void checkDays() {
  //Sunday= 1 && Saturday== 7, adjusted become 6
  int currentDay = weekday();
  if (weekday() == 7) {
    currentDay = 6;
  }

  Serial.print("Currentday is : ");
  Serial.println(currentDay);
  if (sDay[0] == 1) {
    Serial.println("Monday");
  }
  if (sDay[1] == 1) {
    Serial.println("Tuesday"); 
  } etc etc etc....

Don’t forget declare global

int sDay[7] = { 0 }; //Will set all days as 0 @ Not selected

You might have error when no days is selected will result all day selected. This the solution

Thank you again to Pete. :+1:

And plus, I dont know how to mark this as solved since maybe the merge one make the way to edit as solved change

Hello Pete!
Time Input can “Change Widget Properties”? tks!
(Blynk.setProperty())

Is that a statement or a question?

Pete.

[Unformatted code removed by moderator]

why my code is not working , i want to control led pin2 using time input widget

@night your code has been removed because it isn’t correctly formatted with triple backticks at the beginning and end so that it displays correctly.

As I suggested earlier in one of the other topics that you hijacked…

Pete.

sorry,