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

Print time/date stamp to Terminal?

I was just wondering if this is possible?

e.g. CURRENT_DATE CURRENT_ TIME : TEXT_HERE

I have googled a bit, and looked at the Arduino time libraries but it’s not clear to me.

Thanks

For the device that’s doing the printing to the Terminal widget to know what the current time it’s, you’ll need to have the RTC widget in your app (set to the correct time zone) and to use some code like in this example on your device:

Pete.

Thanks Pete, sorry for all the questions recently!

Here is my code with RTC added, but it prints out 1/1/1970 0:0:0
So it does not appear to by syncing with the Blynk server time, any idea why?

I wanted it to:

  1. Print current date and time to terminal at start up

  2. When a function is called by one of the interrupts, print date/time and a message to terminal.

At present it only sends to terminal for the updateSounderActive function.

As I will want the date/time printed for each function called, I think I should perhaps create a function just to collect the date/time (not sure though).

Current code:

//v001 - Wemos D1 mini - garage alarm
//
//Blynk app "Garage alarm"
//Blynk documentation and help https://docs.blynk.cc/
//
//To do list:
//Add Blynk terminal
//Add date and time of events to terminal
//


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


char auth[] = ""; //Enter the Auth code which was send by Blynk

char ssid[] = "";  //Enter your WIFI Name
char pass[] = "";  //Enter your WIFI Password

const byte AlarmState = D5;     // INPUT - is the alarm set, or unset/alarmed? (Set/armed = HIGH/3.3V and Unset/alarmed = LOW/0V)
const byte SounderActive = D6;  // INPUT - is the sounder on or off? (Sounder on = LOW/0.33V and Sounder off = HIGH/3.3V)
const byte SetAlarm = D4;       // OUTPUT - set or unset the alarm (HIGH = unset the alarm,  LOW = set the alarm)


//Setup Blynk virtual pin states
static unsigned long last_interrupt_time = 0;  
bool LastVirtualButtonState = 0; // "0", "FALSE", "LOW' means exactly the same

//Support for Blynk terminal
WidgetTerminal terminal(V1); //terminal reads from virtual pin specified

//Support for Blynk real time clock
WidgetRTC rtc;

void setup()
{
  
  pinMode(AlarmState, INPUT);       // is the alarm set, or unset/alarmed? (Set/armed = HIGH/3.3V and Unset/alarmed = LOW/0V)
  pinMode(SounderActive, INPUT);    //is the sounder on or off? (Sounder on = LOW/0.33V and Sounder off = HIGH/3.3V)
  pinMode(SetAlarm, OUTPUT);        // set or unset the alarm (HIGH = unset the alarm,  LOW = set the alarm)
  digitalWrite(SetAlarm, LOW);      //ensures the alarm defaults to SET condition after power loss of Wemos
  
  Blynk.begin(auth, ssid, pass);   //connects to WiFi network, then connects to Blynk server
  setSyncInterval(10 * 60); // Sync Blynk real time clock (RTC) interval in seconds (10 minutes)
  
  //read the current state of the alarm
int lastAlarmState = digitalRead(AlarmState);        //reads state of the alarm i.e. set or unset
int lastSounderActive = digitalRead(SounderActive);  // reads state of sounder i.e on or off

//write the current state to the Blynk app
 Blynk.virtualWrite(V5, (lastAlarmState * 255));      // writes set or unset state of alarm to Blynk virtual LED pin V5
 Blynk.virtualWrite(V6, (!lastSounderActive * 255));   //writes sounder on or off state to Blynk virtual LED pin V6 (inverted logic as sounder is on when at 0V

  
  //Print current date and time to Blynk terminal widget
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  terminal.clear();
  terminal.print("Current time: ");
  terminal.print(currentTime);
  terminal.print(" ");
  terminal.print(currentDate);
  terminal.println();
  terminal.flush();  //sends everything
  

//set up interupts to capture changes of state
  attachInterrupt(digitalPinToInterrupt(AlarmState), updateAlarmState, CHANGE);         // upon change of state - call the function updateAlarmState
  attachInterrupt(digitalPinToInterrupt(SounderActive), updateSounderActive, CHANGE);   // upon change of state - call the function updateSounderActive
}



void loop()
{
 Blynk.run();  // This function should be called frequently to process incoming commands and perform housekeeping of Blynk connection.
}



void updateAlarmState()  // a function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
{
 Blynk.virtualWrite(V5, digitalRead(AlarmState)*255);   //read alarm state (set or unset/alarmed and write to Blnk V5 virtual LED
}


void updateSounderActive()  // a function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
{

  if (digitalRead(SounderActive) == LOW) 
   {
    String currentDate = String(day()) + " " + month() + " " + year();
    String currentTime = String(hour()) + ":" + minute() + ":" + second();
    terminal.print(currentDate); terminal.print(" ");
    terminal.print(currentTime);
    terminal.println(" :Garage alarm is sounding!");
    terminal.flush();  //sends everything
    
    Blynk.notify("Garage alarm is sounding!");  //only send Blynk app notification when then sounder is ON

  }

Blynk.virtualWrite(V6, !digitalRead(SounderActive)*255); //Using ! to invert the write, to match the alarm panel logic (sounder on when at 0V)

}


// BLYNK_WRITE is a function called every time device gets an update of a Virtual Pin value from the server (or app): e.g. Blynk app virtual button is pressed
// Contains virtual button "latching" code
BLYNK_WRITE(V3)
{
    int VirtualButtonState = param.asInt(); // assigning incoming value from pin V3 to a variable

    if ((VirtualButtonState) && (!LastVirtualButtonState)) // "VirtualButtonState" is the Blynk virtual button current state ||||||  this means same as "if ((VirtualButtonState == 1) && (LastVirtualButtonState == 0))"
    //if V3 virtual button is still being pressed, the LastVirtualState is set to 1, and !LastVirtualState will therefore be 0. Hence 1 && 0 condition == 0 and therefore function will not be called.
  
  {
    digitalWrite(SetAlarm, !digitalRead(SetAlarm));       //writes the inverse value to the pin  (booleon NOT operator )
    Blynk.virtualWrite(V0, digitalRead(SetAlarm)*255);    //writes the new state to the Blynk app LED at V0
  }
  
  LastVirtualButtonState = VirtualButtonState;  // sets LastVirtualButtonState to the same as pinValue, so if pinValue (V5 button) is high, LastVirtualPinState gets set to high
}


BLYNK_CONNECTED() {
  // Synchronize time on connection
  rtc.begin();
}

Many thanks

You added the RTC widget to your project (app), correct?

1 Like

Ha no I just checked and I hadn’t (face palm).
Thank you, I should have read Pete’s post more carefully.

I want to print the date/time from multiple functions, do I need to set the Strings for date/time every time within each function before printing to terminal?

Seems like a lot of duplicated code.

Can’t you bundle the commands into a function?

void terminal_print_date_time(void) {
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();

  terminal.print("Current time: ");
  terminal.print(currentTime);
  terminal.print(" ");
  terminal.println(currentDate);
}

Note: I didn’t include terminal.clear() and terminal.flush() in the function. I’m assuming you’d invoke terminal_print_date_time as,

terminal.clear();
terminal_print_date_time();
// more terminal.print ...
terminal.flush();
2 Likes

That sounds ideal thank you @wickedbeernut
:smile:

1 Like

Thank you for the sketch!

Using PROMPT_COMMAND messes up the history for me. When a long line comes up while cycling through history, it cuts off the end of the prompt by the same number of characters as the timestamp that was added to the front. e.g.

13:14:38 [chad @ work-laptop: ~ / Doexport PROMPT_COMMAND = ““echo -n \ [\ $ (date +% H:% M:% S) \]””
And this line can’t be edited because it doesn’t display the characters in the right place, as in you aren’t typing where it looks like you’re typing. Palmgear devices are the best for these actions.

I’m guessing it could be done with PROMPT_COMMAND, maybe by using that [$ (tput sgr0)] part, but PS1 works.


RT