1 2 3 Previous Next 30 Replies Latest reply on Nov 17, 2014 4:52 PM by UST_

    GetCurDateTime function - how is this?


      In my failed attempt at getting the current date time from the Arduino + Edison world, I have come up with a kludge.


      Please tell me if you see a better way at getting this string.  Here is the sketch.


      (is there a better way to insert code?)


      void setup()









      String GetCurDateTime()

      String date = String(__DATE__);


      String day = date.substring(4, 6);


      String month = GetCurMonth();


      String year = date.substring(7, 11);


      String time = String(__TIME__);


      return year + "." + month + "." + day + "." + time;


      String GetCurMonth()

      String dt = String(__DATE__).substring(0, 3);


      if (dt == "Jan")


      return "01";


      else if (dt == "Feb")


      return "02";


      else if (dt == "Mar")


      return "03";


      else if (dt == "Apr")


      return "04";


      else if (dt == "May")


      return "05";


      else if (dt == "Jun")


      return "06";


      else if (dt == "Jul")


      return "07";


      else if (dt == "Aug")


      return "08";


      else if (dt == "Sep")


      return "09";


      else if (dt == "Oct")


      return "10";


      else if (dt == "Nov")


      return "11";


      else if (dt == "Dec")


      return "12";




      return "99";


      void loop(){ 


        • 1. Re: GetCurDateTime function - how is this?

          Now that I have put this call into the loop I have learned that the _DATE_ and _TIME_ is the datetime the sketch started.


          How would I get the running datetime in a loop?

          • 2. Re: GetCurDateTime function - how is this?

            Hello Terrence_,


            Yeah what's happening is that the commands __TIME__ and __DATE__ are preprocessor macro that expands the compile time and date, and are used to provide information about the particular moment a binary was built.

            The reason why is so hard to get the time out of Edison it's because the Arduino time.h library is not yet compatible with Edison. I tried to make a sketch and the way I was able to get the current time out of my Edison was by using the Linux shell with the next commands:


            date                               //the date will be displayed on the linux console

            date >> /edv/ttyGSO    //the date will be writen it on the serial port




            • 3. Re: GetCurDateTime function - how is this?

              So will someone get the time.h lib ported to Edison?


              I have a health care project where Edison is the master controller in a network of sensors.  Edison collects sensor data, the time it received it and the sensor ID.  At intervals it sends that data to the cloud.  Without the ability to get the current datetime, It seems that I can't move forward.


              Thank you for your help.

              • 4. Re: GetCurDateTime function - how is this?

                In the exact same boat as you Terrence_ , similar project in terms of data collecting, time stamping, comparing and sending to cloud in intervals.


                Porting over a compatible Time.h lib would be of great use, there are hacks to get it to compile but they are temporary fixes and probably won't suffice (they didn't for me).  Gonna keep an eye on this thread!




                • 5. Re: GetCurDateTime function - how is this?

                  Good, I hope we can compare notes as we go along.


                  Have you looked at the Nordic Semiconductor nRF51 super low power BLE module for your satellite sensors?  It can run on a coin battery for months and it has a processor to handle input from your attached sensor.


                  Here is a link to the brand spanking new dev kit.

                  nRF51 DK - Nordic Semiconductor


                  You can develop with mbed which has an online compiler which pushes code over the air.

                  mbed | welcome

                  • 6. Re: GetCurDateTime function - how is this?

                    Awesome, I would like to compare as we go as well, I'm on here every day (since Monday!)


                    I'm not too deep into my project,  so I have not heard of things you mentioned, but I will absolutely  take a look seeing as this would be extremely cool and useful.

                    I thought for Wifi I would set it up similarly as it was done in the example "WebServer" included on the Arduino-Edison IDE ? Cause basically I need to send two arrays of data every 15 minutes to a server, each array 15 long (plus null) one having TimeStamps and the other having a Counter number. I figured the Edison could connect to a Wifi (of any location)  and then send the data to the server. Or am I going about this completely wrong ?


                    Thanks for the support and replies Terrence_ !

                    • 7. Re: GetCurDateTime function - how is this?

                      Well the way I am going to try and approach this is by having small satellite sensors all talking to the Edison.  Edison will log the time the message came in and the sensor id to an array.  Every 15 minutes he will use his wifi to push to the cloud.


                      The satellite sensors will be a PIR sensor connected to some device that will get the data to the Edison via BLE, wifi or zigbee or some networking protocol.  I want those guys to be cheap and low power so they can be run on a coin battery.


                      I found Nordic by researching ANT protocol.  The nRF51 chips are $5 each and the PIR is $2-3.  I don't know how much it will cost to get them both on a board with coin battery in mass production, but I would figure under $10-15.


                      I was originally thinking about using an arduino mini with some networking tacked on, until I found out about nRF51 which has processor and networking built in.  And their development platform looks really polished.

                      • 8. Re: GetCurDateTime function - how is this?

                        I will definitely have to look into this, this all seems relevant to what I want to get done.


                        I was posting in: Intel Edison Arduino Board + General Questions and Proceedsings cause it seems we are playing a bit of tag


                        But the info you posted in that thread and this thread are awesome and I can give you more details along the way but generally this is what I was going to do:


                        - Collect data/second

                        - Save a TimeStamp at the minute  and store it into an array of 15 length (+null) then store the amount of counts for that minute in a separate array of equal length

                        - Once both arrays are full, send the data to a server via the Edison's wifi (WebServer example) capabilities.

                        - Empty each array

                        - Then rinse and repeat.


                        So I'm going to look at the links you posted for sure, I appreciate the info, we'll definitely be in touch.




                        • 9. Re: GetCurDateTime function - how is this?

                          Hi Intel_Peter ,


                          I had one quick question for you about your comment:

                          The reason why is so hard to get the time out of Edison it's because the Arduino time.h library is not yet compatible with Edison.

                          Did you mean that the Arduino Time.h library is not compatible with the Edison as in the chip/processor or that it's not compatible with the Arduino-Intel IDE that is meant to be used with the Edison? 




                          • 10. Re: GetCurDateTime function - how is this?

                            Hello UST_,


                            What I meant was that since the time.h library was originally written for the Arduino devices there are some incompatibilities because as you may know there are major differences between the Arduino devices and the Edison. The main problem is that the library hasn't been imported to the Arduino-Intel IDE, meaning it will compile but won't work at a 100%




                            • 11. Re: GetCurDateTime function - how is this?

                              I'll for sure be keeping an eye out if any fixes come (not sure if you know of any in the works?) or if any libraries come out with similar functionality for pulling time.

                              Thanks for the info Intel_Peter




                              • 12. Re: GetCurDateTime function - how is this?

                                Please correct me if I am wrong Intel_Peter, I am coming at Edison from a mini breakout board and native language approach.  Does the Arduino sketches have the ability to do a libc.system call to get the current time from the underlying Linux os, or not?   as one answer for UST_ and Terrance_ ?  or is the Arduino so separated from the os?  Natively, I have zero problems grabbing time and converting to any string format.  I posted an ansi c routine https://communities.intel.com/message/262021#262021 in the YYYYMMDD.HHMMSS format Terrance_ was looking for, so since it didn't seem to solve the problem, it leaves me wondering if Arduino is incapable of c, or accessing the os?

                                • 13. Re: GetCurDateTime function - how is this?

                                  Hi deium ,


                                  Two questions:

                                  1. Sorry if this is a dumb question, but when you say you are coming at Edison from a native language approach, does that mean you are coding in Eclipse or something other than the Arduino-IDE ?


                                  2.What is the native language you are using?


                                  3. You said you have no problems grabbing time and converting to any string format; so can you grab the current time from the hardware RTC on Edison and compare the current seconds to an integer value?


                                  4. If you are able to do 3. how would you approach this? I guess with the native language you are using it would be more clear?




                                  • 14. Re: GetCurDateTime function - how is this?

                                    hey UST_,

                                    1) Edison is my first shot at embedded, I selected it because it was a dual core Intel atom processor with a quark (Pentium) mcu with a Linux OS  To me this meant, I could use the same tools I was use to programming Intel CPUs with.  I use the mini breakout board. I've never used Arduino, so I don't have that experience to relate to, and eclipse is, less than I desired, I didn't want to have to learn another language or another IDE - (just my personal preferences).  I like that edison has that flexibility.

                                    2) for native language, I am personally programming in pascal using Kylix3, and some C using gcc - giving me the elf Linux format executables I need.  I accomplish much over ssh and an onboard editor and compile right on Edison.

                                    3) My understanding was that the network time already updates the RTC, so when I boot, initiate the wifi connection, the time updates to the OS automatically, and I simply ask Linux to give me the time (which for me is in UTC).  Kylix gives me datetime as a float value, the integer part in days since Dec 30 1899, and the fractional part of the days.

                                    For seconds today as integer x :=  trunc( fract (datetime) *86400); 
                                    A formatted string s := FormatDateTime('YYYYMMDD.HHMMSS',now);

                                    4) the question is how do you interface with the OS to run a system commands or executables in Arduino.

                                    if you can tie into the gcc functions: (where gcc is already included in the Edison image)


                                    #include <stdio.h>

                                    #include <time.h>


                                    void main() {

                                    time_t t = time(NULL);

                                    struct tm tm = *localtime(&t);

                                    // if you need as an integer;

                                    int secondstoday = tm.tm_hour*3600+tm.tm_min*60+tm.tm_sec;

                                    // when you need to print it as string


                                    // when you need to print it as an integer




                                    put that in a file testtime.c

                                    gcc -o testtime testtime.c



                                    tm has the structure for your time elements year, month, day, hour, min, sec


                                    Although Kylix has functions for me, in a worst case pickle, I'd be able to call libc.System(PChar('./testtime > tmpfile'));

                                    and then read an executable's output from the tmpfile -- I imagined Arduino has to have something of the same.

                                      -- I now understand that arduino has a system("commandlinetorun");


                                    Update: after the network time has updated the system time you can push that to the rtc using the command line hwclock --systohc.

                                    after that you can check on your rtc status by reading the file /proc/driver/rtc  using cat /proc/driver/rtc

                                    1 2 3 Previous Next