Introduction: Linkit One: Date and Time

Picture of Linkit One: Date and Time

Accurate dates and time are an important feature to many programs. The linkit one provides it's own time tracking hardware, in the Unix style epoch (time since January 1st 1970).

This Instructable will go over the linkit one implementation of date time, and how to read / set the time on your board.

Step 1: Use Example: LDateTime

The example code "LDateTime" included with the Linkit One IDE is very bare-bones. In fact, I'll include it below:

#include 
datetimeInfo t; unsigned int rtc; void setup() {} void loop() { LDateTime.getTime(&t); LDateTime.getRtc(&rtc); delay(1000); }

Let's go over this in a little more detail, and build off this example.

The datetimeInfo is a struct, as defined below:

typedef struct<br>{
  int year;//year
  int mon;//month,begin from 1
  int day;//day,begin from 1
  int hour;//hour,24-hour
  int min;//minute
  int sec;//second
}datetimeInfo;

As you can see from this struct, it will contain the date components you will need to formulate a timestamp.

This timestamp datetime struct can be grabbed via the getTime() function.

If instead you wanted to get a unix style epoch timestamp (an integer, the value being the number of seconds that have passed since January 1st, 1970), you would use the getRtc() function.

Both the getTime() and getRtc() functions take a pointer, which will have a value filled by the function.

Let's modify the example, so that we can see how to pull the values out of each result.

Firstly, when calling LDateTime.getTime(&t); there's an integer result returned by the method. If it's less than 0, then getting the time failed, and you shouldn't consider using the result.

The result stored in the memory address of the point will be of the type datetimeInfo struct. So we can access each date component by name. Here's a simple method we can add to the example to print out a human readable date:

String dateString(datetimeInfo dti) {<br>  String dateStr = "m/d/yyyy = ";
  dateStr += dti.mon;
  dateStr += "/";
  dateStr += dti.day;
  dateStr += "/";
  dateStr += dti.year;
  return dateStr;
}

Great! Now that we've got some way to output the date in a better format, lets move onto setting the date and time.

We will use the same struct to configure the datetime we want to set.

  datetimeInfo now;
now.year = 2015; now.mon = 12; now.day = 21;

You can additionally configure your hours, minutes and seconds before continuing.

Next we call the LDateTime.setTime() function, which will process each of the components in our struct, and set the system clock on the linkit one.

  LDateTime.setTime(&now);

I've included the example file I modified, LDateTimeTest.ino.

Step 2: Next Steps

Now that you can read and set the date time, a few ideas come to mind for where to take this.

  • There is now way to set the time by using an epoch timestamp (integer). Could modify the .cpp/.h files of LDateTime to allow for this.
  • Write a better display output, that follows ISO-8601 time formatting standards.
  • Use GPS fixes to get very accurate time, to keep mobile projects up-to-date if they reboot, etc.

Comments