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

[SOLVED] Problem with Time library and Time input controller


#1

Hello,

I am currently encountering a problem with (I assume) the time.h library and the time input setting controller. The value of my variable used to count the time from 1970 do not send me back the right time. I guess the problem comes from the “hour()” and others functions that don’t send me back the right value of time. It seems to contain the time from the begining of the uploaded sketch.
Usualy when the esp8266 nodeMCU is ready, this value contains around 40 to 60 secondes.
My code is based on several codes find in this forum where all log supplied seem correct so I don’t get what is my problem.

I am using an Esp8266 NodeMCU (http://www.cnx-software.com/wp-content/uploads/2015/10/NodeMCU_v0.9_Pinout.png).
Concerning my config, I have the lastest release of the blynk library (0.4.0) and the latest arduino software (1.6.12). I have tried the 1.6.5 and the 1.6.10 and I encounter the same problem.
All libraries used on sketch supplied on this forum are present in my sketch (probably too much). All libraries are installed on my computer.
I have tried to change some values and to modify my program but nothing changed. I have not seen on the internet any incompatibility problems and even if it has been some issues, my two others tries with older version of arduino software would solve my issue.
I already run some basic sketch permitting to blink a led and it worked well with the blynk app (Android). I don’t find anything on the web to fix my problem.

You can find in attachment my sketch and some logs of my problem. I have make my code much more simpler and the problem is still the same.

Do you have any idea to correct this ? I hope some of you will be able to help me.

By the way, I discovered blynk 3 days ago and the interface is really easy to manage and to use.

Thank you for your work and for your answer.

— LOGS

[26641] Connecting to blynk-cloud.com:8442
[26761] Ready (ping: 1ms).
Checked schedule at: 00:00:41

01/01/1970
Weekday 5
dayadjustment -1
valeur t.isWeekdaySelected((weekday() + dayadjustment)): 1
---- Inside t.isWeekdaySelected((weekday() + dayadjustment)) ---- nowseconds :
41
startseconds :
32700
Time zone: Europe/Paris
Time zone offset: 3600
Checked schedule at: 00:00:52

01/01/1970
Weekday 5
dayadjustment -1
valeur t.isWeekdaySelected((weekday() + dayadjustment)): 1
---- Inside t.isWeekdaySelected((weekday() + dayadjustment)) ---- nowseconds :
52
startseconds :
32700
Time zone: Europe/Paris
Time zone offset: 3600

— LOGS

==> As you can see the nowsecond value is 41 or 52. I am not expecting that.

– code

define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
include <ESP8266WiFi.h>
include <BlynkSimpleEsp8266.h>
include <WiFiUDP.h>
include <Time.h>
include <TimeLib.h>
include <SimpleTimer.h>
include <WidgetTimeInput.h>

//Blynk

char auth[] = ""; 

char ssid[] = "";
char pass[] = "";

char Date[16];
char Time[16];

SimpleTimer timer;

long startseconds;            // start time in seconds
long stopseconds;             // stop  time in seconds
long nowseconds;              // time now in seconds

int pinD = 12; // GPIO12 D6
int pinQ = 04; // GPIO4 D2

int valueD = LOW;
int valueQ = LOW;

//fin tempo

WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  
  while (Blynk.connect() == false)
  {
    // Wait until connected
  }

  timer.setInterval(60000L, activetoday);  // check every minute if schedule should run today 
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 

  // Initialisation
  pinMode(pinD, OUTPUT); // MOCP
  pinMode(pinQ, OUTPUT); // MOCN
  // Défault value
  digitalWrite(pinD, LOW);
  digitalWrite(pinQ, LOW);
} // end SETUP

void loop()
{  
  if (Blynk.connected())
  {
    Blynk.run();
  }
  timer.run();
} // end loop


/*-----------*/
// Functions
/*-----------*/

// BLYNK Time Input
// Time input widget allows you to select start/stop time, day of week, timezone, sunrise/sunset formatted values and send them to your hardware
BLYNK_WRITE(V1) {
  
  sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
  sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
  TimeInputParam t(param);
  
  Serial.print("Checked schedule at: ");
  Serial.println(Time);
  Serial.println("   ");Serial.println(Date);
  int dayadjustment = -1;
  // debug
  Serial.print("Weekday ");
  Serial.println(weekday());
  
  Serial.print("dayadjustment ");
  Serial.println(dayadjustment);
   
  Serial.print("valeur t.isWeekdaySelected((weekday() + dayadjustment)): ");
  Serial.println(t.isWeekdaySelected((weekday() + dayadjustment)));
  // debug
    if (weekday() == 1)
    {
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    // Monday to friday
    if (t.isWeekdaySelected((weekday() + dayadjustment))>0) //Time library starts week on Sunday, Blynk on Monday
    {
      Serial.print(" ---- Inside t.isWeekdaySelected((weekday() + dayadjustment)) ---- ");
     
        // current time
        nowseconds = ((hour() * 3600) + (minute() * 60) + second());
       
        // start time
        startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);

          Serial.println("nowseconds : ");
          Serial.println(nowseconds);

           Serial.println("startseconds : ");
           Serial.println(startseconds);

        if (nowseconds >= startseconds) 
        {
           digitalWrite(pinD, HIGH);
     
          if (nowseconds <= startseconds) 
          {
           digitalWrite(pinD, LOW);
          }
        }       

        // stop time
        stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);

        if (nowseconds >= stopseconds)
        {
             digitalWrite(pinD, LOW);
        }
     
    } //end monday-friday

  else
  {
    // Fait rien
  }
  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
}
void activetoday() {       // check if schedule should run today
  if (year() != 1970) {
    Blynk.syncVirtual(V1); // sync timeinput widget
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     // COSTAS
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());  // COSTAS
  }
}

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

Activity before sunrise or just before sunset
#2

use the latest library, 1.6.12, which I see you have tried. Stick with this one.


#3

I think your problem is that you are not picking up the time from anywhere.

You need the RTC widget or access to a ntp server before making the connection to the Blynk server.

Just added RTC widget to your sketch and the results are now fine.


#4
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUDP.h>
#include <Time.h>
#include <TimeLib.h>
#include <SimpleTimer.h>
#include <WidgetTimeInput.h>
#include <WidgetRTC.h>
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V4);

//Blynk

char auth[] = ""; 

char ssid[] = "";
char pass[] = "";

char Date[16];
char Time[16];

SimpleTimer timer;

long startseconds;            // start time in seconds
long stopseconds;             // stop  time in seconds
long nowseconds;              // time now in seconds

int pinD = 12; // GPIO12 D6
int pinQ = 04; // GPIO4 D2

int valueD = LOW;
int valueQ = LOW;

//fin tempo

WiFiServer server(80);

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  
  while (Blynk.connect() == false)
  {
    // Wait until connected
  }

  timer.setInterval(60000L, activetoday);  // check every minute if schedule should run today 
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 

  // Initialisation
  pinMode(pinD, OUTPUT); // MOCP
  pinMode(pinQ, OUTPUT); // MOCN
  // Défault value
  digitalWrite(pinD, LOW);
  digitalWrite(pinQ, LOW);
  rtc.begin();
} // end SETUP

void loop()
{  
  if (Blynk.connected())
  {
    Blynk.run();
  }
  timer.run();
} // end loop


/*-----------*/
// Functions
/*-----------*/

// BLYNK Time Input
// Time input widget allows you to select start/stop time, day of week, timezone, sunrise/sunset formatted values and send them to your hardware
BLYNK_WRITE(V1) {
  
  sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
  sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
  
  TimeInputParam t(param);
  
  Serial.print("Checked schedule at: ");
  Serial.println(Time);
  Serial.println("   ");Serial.println(Date);
  int dayadjustment = -1;
  // debug
  Serial.print("Weekday ");
  Serial.println(weekday());
  
  Serial.print("dayadjustment ");
  Serial.println(dayadjustment);
   
  Serial.print("valeur t.isWeekdaySelected((weekday() + dayadjustment)): ");
  Serial.println(t.isWeekdaySelected((weekday() + dayadjustment)));
  // debug
    if (weekday() == 1)
    {
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }

    // Monday to friday
    if (t.isWeekdaySelected((weekday() + dayadjustment))>0) //Time library starts week on Sunday, Blynk on Monday
    {
      Serial.print(" ---- Inside t.isWeekdaySelected((weekday() + dayadjustment)) ---- ");
     
        // current time
        nowseconds = ((hour() * 3600) + (minute() * 60) + second());
       
        // start time
        startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);

          Serial.println("nowseconds : ");
          Serial.println(nowseconds);

           Serial.println("startseconds : ");
           Serial.println(startseconds);

        if (nowseconds >= startseconds) 
        {
           digitalWrite(pinD, HIGH);
     
          if (nowseconds <= startseconds) 
          {
           digitalWrite(pinD, LOW);
          }
        }       

        // stop time
        stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);

        if (nowseconds >= stopseconds)
        {
             digitalWrite(pinD, LOW);
        }
     
    } //end monday-friday

  else
  {
    // Fait rien
  }
  // Process timezone
  // Timezone is already added to start/stop time

  Serial.println(String("Time zone: ") + t.getTZ());

  // Get timezone offset (in seconds)
  Serial.println(String("Time zone offset: ") + t.getTZ_Offset());
}
void activetoday() {       // check if schedule should run today
  if (year() != 1970) {
    Blynk.syncVirtual(V1); // sync timeinput widget
    sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());     // COSTAS
    sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());  // COSTAS
  }
}

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

#5

You are right, it works now!!!
I thought the time was included in the basics libraries

Sorry for this waste of time on your side and again thank you for your work and for your kind assistance. I will use Blynk for several project :slight_smile:


#6

Could you upload the sketch? I’m also having problems with TimerInput. Thanks!


#7

@Ayslan_Carolli

You are responding to an old post!!.

Please create your own new topic with relevant title and issue details and posting your own code, if necessary. Please properly format any code you do post as per… [README] Welcome to Blynk Community!