I want to send a simple text and the current time from an Arduino (Wemos D1 mini) to a Blynk terminal on Android. What would be the easiest way to format the time and date so that it’s readable by a human? Right now I just send the timestamp, but it’s obviously suboptimal:
I read that I can get the time from Blynk servers somehow, but I’m thinking there should be an easier way, like a placeholder string that makes the Blynk app on the Android device automatically replace the placeholder with the device’s current localized date and time? Seems odd if this doesn’t exist, given how user friendly I’ve found Blynk to be so far.
I do not know of a way to grab the mobile device time.
You will have to add the RTC widget, get the time from that, format it, then and send it along with the rest of your text.
Have a look at this code I just finished recently:
#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
terminal.clear();
terminal.print("Startup : ");
terminal_print_date_time(); //call terminal function to print date and time stamp
//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.
}
//A function to print current date and time to Blynk terminal widget, gets called from the below functions
void terminal_print_date_time()
{
String currentDate = String(day()) + "/" + month() + "/" + year() + "-";
String currentTime = String(hour()) + ":" + minute() + ":" + second();
terminal.print(currentDate);
terminal.print(" ");
terminal.print(currentTime);
terminal.println();
}
//A function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
void updateAlarmState() //alarm set or unset/alarmed
{
Blynk.virtualWrite(V5, digitalRead(AlarmState)*255); //read alarm state (set or unset/alarmed and write to Blnk V5 virtual LED
if (digitalRead(AlarmState) == LOW)
{
terminal_print_date_time(); //call terminal function to print date and time stamp
terminal.println("Garage alarm now unset/alarmed");
}
else
{
terminal_print_date_time(); //call terminal function to print date and time stamp
terminal.println("Garage alarm now set");
}
terminal.flush(); //sends everything
}
//A function called by a CHANGE interrupt, which write the input state to a Blynk virtual LED
void updateSounderActive() //sounder on or off
{
Blynk.virtualWrite(V6, !digitalRead(SounderActive)*255); //Using ! to invert the write, to match the alarm panel logic (sounder on when at 0V)
if (digitalRead(SounderActive) == LOW)
{
Blynk.notify("Garage alarm is sounding!"); //only send Blynk app notification when then sounder is ON
terminal_print_date_time(); //call terminal function to print date and time stamp
terminal.println("Garage alarm is sounding!");
}
else
{
terminal_print_date_time(); //call terminal function to print date and time stamp
terminal.println("Garage alarm sounder off");
}
terminal.flush(); //sends everything
}
// 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
}
//Terminal input commands
// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V1)
{
// if you type "clear" into Terminal Widget - it will clear the terminal page
if (String("clear") == param.asStr())
{
terminal.clear();
}
else
{
terminal.println("Incorrect command - help below:");
terminal.println("Type clear to clear page");
terminal.println("More commands to be added soon..");
terminal.flush(); //sends everything
}
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
You will need RTC widget and of course terminal widget.