How to Extract Time/Date Information from the Yun and Parse It Into String

Hello,

I am sending sensor data to a Google Spreadsheet and I would like to have a time stamp associated with each reading. Currently have been able to add hour and minute (intentionally excluding seconds). I would like month, day, year and day of the week as well. All of these categories have to be comma separated string in order to send them to the spreadsheet. Does anyone know how I can get my Yun to extract these date variables as well? The code I used for the time is below. I have also attached a picture of how my spreadsheet currently looks and how I would like it to look (blue).

Any help is greatly appreciated. Thank you.

-Ryan

This is how I turned my time and sensor values into a comma separated string variable

String mySensorValues=(String)hours;
mySensorValues+=":";
mySensorValues+=(String)minutes;
mySensorValues+=",";
mySensorValues+=(String)h;
mySensorValues+=",";
mySensorValues+=(String)t;
mySensorValues+=",";
mySensorValues+=(String)f;

Here is the code I used to get the time:

#include <Process.h>

Process date; // process used to get the date
int hours, minutes, seconds; // for the results
int lastSecond = -1; // need an impossible value for comparison

void setup() {
Bridge.begin(); // initialize Bridge
Serial.begin(9600); // initialize serial

while(!Serial); // wait for Serial Monitor to open
Serial.println("Time Check"); // Title of sketch

// run an initial date process. Should return:
// hh:mm:ss :
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}

void loop() {

if(lastSecond != seconds) { // if a second has passed
// print the time:
if (hours <= 9) Serial.print("0"); // adjust for 0-9
Serial.print(hours);
Serial.print(":");
if (minutes <= 9) Serial.print("0"); // adjust for 0-9
Serial.print(minutes);
Serial.print(":");
if (seconds <= 9) Serial.print("0"); // adjust for 0-9
Serial.println(seconds);

// restart the date process:
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}

//if there's a result from the date process, parse it:
while (date.available()>0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();

// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon= timeString.lastIndexOf(":");

// get the substrings for hour, minute second:
String hourString = timeString.substring(0, firstColon);
String minString = timeString.substring(firstColon+1, secondColon);
String secString = timeString.substring(secondColon+1);

// convert to ints,saving the previous second:
hours = hourString.toInt();
minutes = minString.toInt();
lastSecond = seconds; // save to do a time comparison
seconds = secString.toInt();
}

}

You are using +%T as parameter.

You want
+%H for hour
+%M for minute

The library you are using is based on date(1) a UNIX/Linux tool
The reference you need is here:

http://unixhelp.ed.ac.uk/CGI/man-cgi?date

Look under "FORMAT controls the output"

Hope this helps
Jesse

jessemonroy650:
http://unixhelp.ed.ac.uk/CGI/man-cgi?date

Look under "FORMAT controls the output"

That is my recommendation as well. After reading that page, search for some examples. You should be able to craft a format string that has all of the fields you want, including the required comma punctuation between the fields. Then use Process to run the date command with that format string, and get the complete formatted date/time sequence to which you can append your sensor readings. You won't have to do any parsing and reformatting in the Arduino code.

Your code is also polling the time to see the change in seconds. You could parse that from this formatted string, or make a separate Process call to date which returns just the seconds.

Awesome! I will try your recommendations. Thank you!

Alright, I am admittedly very new to Arduino and programming. I tried just replacing the current parameters with the ones that I desire. This did not work. There must be something in the code that I am using aside from the parameters that determines the format. (The serial monitor returns 00:00:00 when I use different parameters, such as the month). This is the only example code I could find for time/date. Do you know if there is a simpler way to extract just the parameters I need without all the additional guidelines in this code? If possible, an example would be very helpful. Thanks again for your assistance with this.

Show us what you have using the format string so far, we may notice something wrong.

As a quick test:

date +"%m,%d,%y,%A,%H:%M,"

returns:

12,22,14,Monday,14:27,

No parsing required, just append your sensor values.

Ryan_Ferrin,
if you continue to have problems, post your code. It's often a typo.
Jesse

That worked! Thank you for all of your help!