RTC show time in terminal when started

Hello,
I’m looking for a method to show the time when my Project goes online.
However, my Code wont work how i want it, there is no compiling error but it just won’t start and keeps refreshing the ping in Serial monitor. Maybe someone can help me.

  1. Is it wrong to put these lines outside of a function? I put them with my other strings and variables in the beginning and it didn’t work. They had to be inside of the function I want them in.
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  1. I want the time right after everything is connected so I tryed putting it like this but it didnt work:
//Verbindung mit Blynk 
BLYNK_CONNECTED() {
String currentTime = String(hour()) + ":" + minute() + ":" + second();

  Blynk.syncAll();
  terminal.clear();
  terminal.println(F("Blynk v" BLYNK_VERSION ": You are online."));
  terminal.println(currentTime);
  terminal.println("-------------");
  terminal.flush();
}   

So I tried to put it into void Setup() where it didnt start too.
Where is my mistake? And what are the alternatives?

My aim is to make the Terminal write something like:

“Blynk v0.6.1: You are online.
19:35:12”

It’s very difficult for people to help if you just posts snippets of code rather than your full sketch.

Have you seen this example sketch, it provides some very helpful info in the comments:

Have you included the RTC widget in your app?

Pete.

1 Like

Hello, my sketch has a lot of other information in it and is mostly in german but I’ll post it if ist more helpful.
Yes I have the Widget and I get it working in my other function. The only thing I need is: I just want it to post the time as first thing when it goes online.
For better understanding, my Project is a laser tripwire which sends notification and Information to the phone and Needs a Passwort in the terminall to deactivate.
I already implemented the rtc function for my Alarm message in the terminal. I just have a Problem as I am not able to send the time when the Project goes online. Why do I Need this? - The terminal clears when the Sketch connects but sometimes when I have booting Errors for example there is still the message from earlier in the terminal and I thought it to be useful if it sends the time as it goes online to Show me as a user when this was send.
However, here is the full Sketch (which is working well):



#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = 
char ssid[] = 
char pass[] = 

#include <SoftwareSerial.h>

SoftwareSerial EspSerial(11, 12); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200

ESP8266 wifi(&EspSerial);

WidgetTerminal terminal(V2);
BlynkTimer timer;
WidgetRTC rtc;

//Konstanten
const int redled = 3;           //Pin für rote LED
const int greenled = 4;         //Pin für grüne LED
const int ldrdigital = 10;   //Alternativ das Modul digital auslesen
const int speaker = 5;          //Pin für akkustischen Alarm
const int armbutton = 6;        //Pin für An- und Ausschalten des Systems
const int armbutton2 = 8;       //analog  
const int gelbled = 7;          //Pin für gelbe LED

//Variablen
boolean aktiv = true;       // Variable für Aktivierung
boolean alarm = false;      // Variable für Alarm
int buttonVal = 0;           // Variable um Buttonzustand festzustellen
int prev_buttonVal = 0;      // Variable um Buttonzustand zu vergleichen
int buttonVal2 = 0;          // Analog
int prev_buttonVal2 = 0;

// Konstanten für Sirene
const int lowrange = 2000;   // niedrigste Frequenz
const int highrange = 4000;  // höchste 

//Flags                 //Vermeiden von loop für Benachrichtigungen und Terminal
int flagAlarm = 0;        
int flagAktiv = 0;

//Strings
String password = "cool";

void setup()
{
  //Bestimmen der Output und Input Pins
  pinMode(redled,OUTPUT);
  pinMode(greenled,OUTPUT);
  pinMode(gelbled, OUTPUT);
  pinMode(speaker,OUTPUT);

  pinMode(ldrdigital, INPUT);
  pinMode(armbutton, INPUT);
  
  timer.setInterval(1000L, data); //timer will run every sec 
  Serial.begin(19200);

  delay(10);
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  rtc.begin();
  Aktivieren ();
}

void loop()
{
  Blynk.run();
  timer.run();
}

void data()
{
  String currentTime = String(hour()) + ":" + minute() + ":" + second();

  int buttonVal = digitalRead(armbutton);                     //liest Button aus und vergleicht
  if ((buttonVal == HIGH) && (prev_buttonVal == LOW)){
    Aktivieren();                                             //Wenn Bedingung erfüllt wird das System ativiert
    if (flagAktiv == 0){
       terminal.println(F("Sie haben das System aktiviert!"));
       terminal.flush();
       flagAktiv = 1;  
    }
    delay(50);
  }
  int digitalValue = digitalRead(ldrdigital);                   //liest den Wert des LDR Moduls aus
  if ((aktiv) && (digitalValue == HIGH)){                   //vergleicht ausgelesenen Wert mit Grenzwert
    alarm = true;
  }
  
  if (alarm){

    if (flagAlarm == 0){
       Blynk.notify("Der Alarm wurde ausgelöst!");
       terminal.print(F("Der Alarm wurde um "));
       terminal.print (currentTime);
       terminal.println (F(" ausgelöst."));
       terminal.println(F("Geben Sie Ihr Passwort ein, um den Alarm auszuschalten!"));
       terminal.flush();
       flagAlarm = 1;  
    }
    // Sirene
    // steigender Ton
    for (int i = lowrange; i <= highrange; i++)
    {
      tone (speaker, i, 250);
    }
    // sinkender Ton
    for (int i = highrange; i >= lowrange; i--)
    {
      tone (speaker, i, 250);
    }

    // optischer Alarm bzw. blinken
    digitalWrite(gelbled, HIGH);
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, HIGH);
    delay(10);
    digitalWrite(greenled, LOW);
    digitalWrite(gelbled, LOW);
    digitalWrite(redled, LOW);
  }

    delay(20);

// Funktion aktiviert das System
void Aktivieren(){

  if (aktiv){
    digitalWrite(greenled, LOW);
    digitalWrite(redled, HIGH);
    alarm = false;
    aktiv = false;
  } else {
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
    tone(speaker, 220, 125);
    delay(200);
    tone(speaker, 196, 250);
    aktiv = true;
  } 
}

//Verbindung mit Blynk 
BLYNK_CONNECTED() {
  Blynk.syncAll();
  terminal.clear();
  terminal.println(F("Blynk v" BLYNK_VERSION ": Sie sind jetzt online."));
  terminal.println("-------------");
  terminal.flush();
}   


//Passwortschutz Terminal Blynk
BLYNK_WRITE(V2)
{
  if (alarm) {
     if (String (password) == param.asStr()){
         //deactivate alarm
          aktiv = false;
          alarm = false;
          flagAlarm = 0; 
          flagAktiv = 0;
          digitalWrite(greenled, LOW);
          digitalWrite(redled, HIGH);
          tone(speaker, 196, 250);
          delay(200);
          tone(speaker, 220, 125);
          terminal.println(F("Korrekt! Alarm deaktiviert."));
          terminal.flush();
        }
    else {
      terminal.println(F("Falsches Passwort!"));
      terminal.flush();
    }
  }
}
´´´

The problem I see is that this function starts immediately after connection… so even with rtc.begin() right after Blynk.begin() it is too soon to have any valid data in String(hour(), etc.

And of course you can not put it first since Blynk isn’t connected to synchronize :stuck_out_tongue:

So basically you need to start a short timeout timer and trigger your required output shortly after rtc.begin(). But even that can be iffy as I have seen it take minutes to properly synchronize… as evidenced with a year of 1970.

I remembered I do have a project that prints out a “startup” intro to the App Terminal (On V1), and I use this method.

However, as stated, sometimes it doesn’t have the correct time/date.

BLYNK_CONNECTED() {
  rtc.begin();
  setSyncInterval(120);  // Time Sync every 2 minutes
  delay(100);  // Just enough time to sync the time?
  TermIntro();  // Display Blynk logo on terminal widget at startup
  // other stuff...
}
void TermIntro()  // Automatically runs at Mega boot up
{
  Blynk.virtualWrite(V1, "\n"
                     "   ___  __          __\n"
                     "  / _ )/ /_ _____  / /__\n"
                     " / _  / / // / _ \\/  '_/\n"
                     "/____/_/\\_, /_//_/_/\\_\\\n"
                     "       /___/ v." BLYNK_VERSION " on\n            " BLYNK_INFO_DEVICE "\n");
  size_t ram = BlynkFreeRam();
  Blynk.virtualWrite(V1, "\n Free RAM: ", ram);
  Blynk.virtualWrite(V1, "\n ", currentTime, " ", currentDate);
}  // END TermIntro Loop

Yes that makes sense. I thought a solution could be to put it at the beginning of my “void data()” using a flag so that it just sends the time once to terminal but I couldn’t get it working.
Is there a Problem with These lines?

Something like this

int flagtime = 0;

void data ()
{ […]
if (flagtime == 0) {
   terminal.println (currentTime);
   flagtime = 1;
[…]
}
}

Just writing the following sends the message to the terminal.

void data(){
terminal.println (currentTime);
}

Unfortunately it keeps repeating without the flag. Whats wrong with the first lines I wrote?

What it this in your code?? it compiles with it?

Ah sorry. I just meant that to show there is other stuff between these lines. Didnt want to post the whole Sketch again. It’s not really in my Sketch.

With the variable set globally as you have, then I don’t see why that shouldn’t work once and only once… unless you use the same flag elsewhere.

I got it working like this:

boolean flagtime = true;

void data() 
{
  if (Blynk.connected())
  {
    if(flagtime){
  Blynk.virtualWrite(V2, "\n ", currentTime, " ", currentDate);
  flagtime = false;}
  }
}

Your example helped me as it worked with Blynk.virtualWrite and it didnt work before and as you mentioned I also dont understand why it didnt work with the flag. But like this it works how I liked it to.

Thank you very much!

And I also found the Problem why it didnt work earlier!

I just forgot the terminal.flush() afterwards…

3 Likes