I'm trying to work out how to grab a string "yyyy-mm-dd hh:mm:ss"
Where is this string coming from?
This is typically done using sscanf to break the string into time element values and using makeTime() from the Time Library to create the unix time.
With monitor entry, you can also use Serial.parseInt.
Here's an example using your format
#include <TimeLib.h>
tmElements_t tm;
int Year, Month, Day, Hour, Minute, Second ;
void setup() {
Serial.begin (115200);
Serial.println("string converted to tmElements.");
Serial.println("format is yyyy-mm-dd hh:mm:ss");
char dateTime[] = {"2017-03-27 10:30:15"};
Serial.println(dateTime);
createElements(dateTime);
setTime(makeTime(tm));//set Ardino system clock to compiled time
//setTime(Hour, Minute, Second, Day, Month, Year); //alternative form of setTime
Serial.println("System millis clock referenced to tmElements.");
}
void loop() {
Serial.print("Ok, Time = ");
digitalClockDisplay();
delay(2000);
}
void createElements(const char *str)
{
sscanf(str, "%d-%d-%d %d:%d:%d", &Year, &Month, &Day, &Hour, &Minute, &Second);
tm.Year = CalendarYrToTm(Year);
tm.Month = Month;
tm.Day = Day;
tm.Hour = Hour;
tm.Minute = Minute;
tm.Second = Second;
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(month());
Serial.print('/');
Serial.print(day());
Serial.print('/');
Serial.print(year());
Serial.println();
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
I can diff two of them to get the minutes difference.
#include <TimeLib.h>
tmElements_t tm;
int Year, Month, Day, Hour, Minute, Second ;
void setup() {
Serial.begin (115200);
Serial.println("string converted to tmElements.");
Serial.println("format is yyyy-mm-dd hh:mm:ss");
char firstDateTime[] = {"2017-03-26 10:30:15"};
char secondDateTime[] = {"2017-03-27 10:30:15"};
Serial.println(firstDateTime);
Serial.println(secondDateTime);
createElements(firstDateTime);
unsigned long firstTime = makeTime(tm);
createElements(secondDateTime);
unsigned long secondTime = makeTime(tm);
Serial.println("Time Difference in Minutes");
Serial.println((secondTime - firstTime)/60);
}
void loop() {
}
void createElements(const char *str)
{
sscanf(str, "%d-%d-%d %d:%d:%d", &Year, &Month, &Day, &Hour, &Minute, &Second);
tm.Year = CalendarYrToTm(Year);
tm.Month = Month;
tm.Day = Day;
tm.Hour = Hour;
tm.Minute = Minute;
tm.Second = Second;
}