Hello, I hope you can help me I am a newbie in this

I want to get the current time from the blynk server in Argentina and compare with the time entered by the “Time Imput” widget with the time set by the application I want to turn on and off the light of my automatic nursery. My problem is that I can’t get the current time from the Blynk server. My problem is that I can’t get the current time from the Blynk server. I also want to know what would happen to my code if it disconnects from Wifi or if the NodeMcu driver is restarted after a power outage


#define BLYNK_PRINT Serial

/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "********"
#define BLYNK_DEVICE_NAME "Indoor"
#define BLYNK_AUTH_TOKEN "************"

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SPI.h>

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;

int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int TimeZone;
int HumedadMin = 5;
int LecturaHumedad;
int LecturaPorcentaje;
int SetPoint = 23;
int Hist = 2;
bool Light;

#define DHTPIN D2          // Digital pin D2     DEFINIR COMO CONST INT
#define DHTTYPE DHT21   // DHT 21, AM2301

BLYNK_WRITE(InternalPinRTC) //check the value of InternalPinRTC where time is stored internally. Reports time when gate status was last checked - which is every second

long t = param.asLong();  //store the time in t variable if it has changed

time_t current = t;   //this section uses Time Library to convert Unix time to normal time

void setup()
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(2000L, datasend);
  pinMode (D6, OUTPUT);  // VENTILACIÓN
  pinMode (D7, OUTPUT);  // ILUMINACIÓN


void datasend() {

  float h = dht.readHumidity();
  float temp = dht.readTemperature();

  if (isnan(h) || isnan(temp)) {
//    Serial.println("Failed to read from DHT sensor!");
//    return;
  Blynk.virtualWrite(V0, h);  //V0 is for Humidity
  Blynk.virtualWrite(V1, temp);  //V1 is for Temperature

  if (temp > SetPoint + Hist){
    Serial.println("Ventilación encendida");
    digitalWrite(D6, LOW);

  if (temp < SetPoint - Hist){
    Serial.println("Ventilación apagada");
    digitalWrite(D6, HIGH);

    // escribir datos
   Serial.print("La Temperatura es ");
   Serial.print("La humedad es de ");

   LecturaHumedad = analogRead (A0);

// Conviertiendo las lecturas a Porcentajes

   LecturaPorcentaje = map (LecturaHumedad, 280, 720, 100, 0);

   Serial.print("La Humedad Tierra es de: ");
Blynk.virtualWrite(V2, LecturaPorcentaje);
 if (SThour == TimeZone){

 Serial.println("Luces encendidas");
 digitalWrite(D7, LOW);

 else {

 Serial.println("Luces apagadas");
 digitalWrite(D7, HIGH);

 BLYNK_WRITE(InternalPinRTC) {   //check the value of InternalPinRTC  
  long t = param.asLong();      //store time in t variable
  Serial.print("Unix time: ");  
  Serial.print(t);              //prints time in UNIX format to Serial Monitor

void loop()

BLYNK_WRITE(V7) {  // Called whenever setting Time Input Widget
  TimeInputParam t(param);
  SThour = t.getStartHour();
  STmin = t.getStartMinute();
  STsec = t.getStartSecond();
  SPhour = t.getStopHour();
  SPmin = t.getStopMinute();
  SPsec = t.getStopSecond();
  //TimeZone = t.getTZ_Offset();

SetPoint = param.asInt(); // assigning incoming value from pin V3 to a variable 

please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly. Triple backticks look like this: ```

1 Like

@LeandroIrigaray this is the second time that you’ve created a topic on the forum, posted unformatted code, and been asked by @Madhukesh to edit your post to fix the formatting with triple backticks.

Last time you didn’t do that, and your unformatted code was removed (by me) after 24 hours.

You don’t seem to have learned from that experience, or learned to read the information that is pre-populated in the topic when you create it - which explains how to use triple backticks to format your code.

If you don’t want to follow the community forum rules then please stop posting to the forum.

If you want assistance with your project then please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
Copy and paste these if you can’t find the correct symbol on your keyboard.

Otherwise, your unformatted code will be removed after 24 hours, and future posts with unformatted code will be deleted immediately, without bothering to repeat requests to edit the post and fix the formatting.


Thanks, I was able to edit it.

sorry Pete don’t be mad
I already managed to edit the text.
I await help from him with the code now
the language difference plays a trick on me

The first thing you need to understand is how ESP8266 devices keep track of the time.
They have an internal clock that is accurate to around 1 second per day. Once you tell the device what the correct time is, it will be reasonably accurate for most applications, and maybe only needs to be synchronised with the correct time once per week.
The device will lose the current time information when it loses power, so needs to be re-synchronised at start-up.

If you need the device to know the current time when it powers-up and there is no internet available then you will need an external hardware RTC with battery backup, but it sounds like that’s not needed in your case.

You’ve taken some of the code snippet from the Blynk documentation, but omitted some important pieces.

You have two BLYNK_WRITE(InternalPinRTC) functions, which is incorrect…

The description you’ve added to the first of these functions is totally incorrect.
The BLYNK_WRITE(InternalPinRTC) function is how the server sends the current date/time to the device. If you want to trigger the server to send this data then you need to use the Blynk.sendInternal("rtc", "sync") command.

If you want this command to be sent each time the device starts-up and connects to Blynk then you would put it inside a BLYNK_CONNECTED() function, as explained in the RTC section of the Blynk documentation…


If you want the device to synchronise with the Blynk server at other times, to correct the approximately 1 second per day of inaccuracy then you can use BlynkTimer to call a function which triggers the `Blynk.sendInternal(“rtc”, “sync”) command.
You do not need to do this very often. Once per day would be fine for the type of application you are talking about.

If you want to automatically adjust for daylight saving time changes then you need to read this…



1 Like

I understood perfectly thanks Pete.
I will use the following code to get the current time value every time the server connects. As you point out, the inaccuracy of 1 second does not affect my code.

BLYNK_CONNECTED() { //When device is connected to server...
   Blynk.sendInternal("rtc", "sync"); //request current local time for device

my query now is how do I extract the current time value to an integer value and thus be able to compare it with the value entered by the “time imput” widget. Through the comparison I want to turn on or off the light in my nursery.

if (SThour == TimeZone){

  Serial.println("Lights on");
  digitalWrite(D7, LOW);

  else {

  Serial.println("Lights off");
  digitalWrite(D7, HIGH);

where is the rtc value stored and in what type of variable is it?

You probably need to read about the Time library, to allow you to store the RTC time to the device clock, and then compare the device clock time to the on/off times that you get from the Time Input widget.

You obviously haven’t implemented the code to receive the time input widget data yet.