Go Down

Topic: Howto sync DateTime Arduino Clock? (Read 21204 times) previous topic - next topic

GPSanino

Hi,
I wonder if MEM or other friends can help me with this problem. :-/
I have been reading and trying to use the library DateTime to add the "software clock capability" (no RTC) to Arduino Duemilanove, in order to trigger some reed relays on specific pre-scheduled date/time events.  ;)

I have tried with the example sketch of the library but it does not sync nor send any script time to the serial monitor. With other examples that use a preset unix time (not by sync) it works well but of course not synced to the actual time.

So my problem is that I could not find easy to follow directions on how to sync with the external PC/notebook clock, the clock sketch using DateTime library.

Maybe we can get a mini Howto for newbies like me here.

I am using the Arduino IDE 0012 (for linux AMD64), also the 0015 for Windows, with the latest libraries of DateTime and DateTimeStrings.
Thanks a lot!
GPS

mem

To set the clock from a PC you need to run a program that sends time to Arudino in the format expected by the sketch. There is a processing sketch provided in the download that does this.

The Processing sketch needs to be set to the com port that is connected to Arduino. It's the same port you use when uploading sketches from the IDE, you can use the IDE tools Serial Port menu to see what ports you have and which one is used by arduino.

A variable called portIndex is used to select the port. Find thus line in the Processing sketch:
public static final short portIndex = 0;  // select the com port, 0 is the first port

and change it so that portIndex is set to the correct port. You can see list of ports available and the one selected in the output window on the Proccesing IDE.


If you still have trouble, post the Processing output so we can see what is happening.

You could also try the Processing SimpleWrite sketch to test the connection - from the Processing File menu, select: Examples  -  Libraries  - Serial - SimpleWrite
(You need to compile the and upload the Arduino sketch that is listed at the end)

Good Luck.

MikMo

GoBetwino also has a command type for getting the time ( and date) from the PC.

http://mikmo.dk/gobetwino.html

mem

#3
Apr 05, 2009, 11:38 am Last Edit: Apr 05, 2009, 11:38 am by mem Reason: 1
Hi MikMo, Gobetwino would be much easier to use with the DateTime library if you had an option to send the date and time in the format that the library can use more easily. This is the C time_t (also known as Unix/Posix time)

Perhaps you could add a command ('DT') that sent the current time in this format. The Library sample code expects a header followed by a string of ten digits representing the current time. Here is a C# function that shows one way to get the value:

static double ConvertToUnixTimestamp(DateTime date)
{
   DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
   TimeSpan diff = date - origin;
   return Math.Floor(diff.TotalSeconds);
}

The header is currently a byte with value 255 but it would be good if the header value could be configured. If it must be hard coded into gobetwino then use the character 'T' as the header as I am planning to make that change in the next release of the code.

For example the string expected for the time now is: "T1238927820"

MikMo

Mem:

Thanks for that advice, your function is on the todo list.


GPSanino

#5
Apr 07, 2009, 02:19 am Last Edit: Apr 07, 2009, 02:21 am by GPSanino Reason: 1
Thanks a lot Mem and MikMo.
Now I understand better how it works. I was hoping that Arduino IDE was able to sync directly. As my project is for standing alone (not connected to the USB more than just for the upload of the sketch) I think the easiest way to sync the clock will be to set a time_t on the future (e.g. next minute or so) and turn on the power source for the board just at that second. It would compromise accuracy in maybe half seconds but it is not that significant for my needs. I tried with KontrollerLab, Eclipse and Qt4 considering even to write a small IDE that translate "human" time to unix time_t and upload the sketch (something I have never done before  :-/) but at the end, it seems like to turn on the board at a preset time, is the easiest solution.
Thanks a lot!!!! ;)

mem

Quote
I tried with KontrollerLab, Eclipse and Qt4 considering even to write a small IDE that translate "human" time to unix time ...


why not just run the 'SetArduinoTime' Processing sketch, it reads the time from your computer and a mouse click sends the correct message to Arduino (if connected on the serial port) to set the time?

GPSanino

I am sorry Mem, I do not fully inderstand how to do that. I found the SetArduinoClock.pde file but I do not know how to run it without uploading it to the board (do I need Processing?) nor if it is possible this sketch to fuse it with mine in order to have one single sketch that does everything (that would be great). I am looking for getting a single API doing everything. About the ports, I should use 0,1,2.... for windows and linux? please forgive me if these questions are too basic.  :-/

mem

You can read about and get Processing from here : http://processing.org/

The Serial port setting for processing depends on how many serial ports on your computer. The Processing sketch shows you all the ports it can find, it will try to attach to the first one but you will need to modify the Processing sketch if Arduino is connected to a different com port.

I would think a search should turn up plenty of help for getting Processing working with arduino.

But if you prefer to set the time from the Arduino IDE, here is how you do it:

Modify the Arduino DateTime   sketch  by change:
  #define TIME_HEADER  255   // Header tag for serial time sync message
To
  #define TIME_HEADER  'T'   // Header tag for serial time sync message


Compile the sketch and upload to the board.
Click the Serial monitor icon (it's right most square icon at the top of the Arduino IDE). Set the baud rate for 19200.

Go to one of the many internet sites that provides the current time in Unix format, for example: http://www.epochconverter.com/
This will show you the current time GMT, you will need to convert it for your time zone.

You can manually enter the current date and time and the page will calculate the unix time. Copy the unix time ( a ten digit number with starting 1239??) and past it into the serial monitor send window (the text box to the left of the send button).

Paste a capital T in front of the ten digit number - it should look like this:
T1239130053

Press the send button and you should see the time updated in the Serial monitor every second.

Good luck!

econjack

Mem:

Thanks for the help. I added a #define for a five hour offset between GMT and EST because I'm lazy when reading the seconds offset.

#define TIME_OFFSET 18000   // Five hours is 18000 seconds

Then, just after the for() loop in getPCtime() I added the line:

     pctime -= TIME_OFSET;    //Adjust for GMT

Makes it a little easier to type in the sync value with the appropriate time.

nuggetz

#10
Sep 24, 2010, 07:36 pm Last Edit: Sep 24, 2010, 09:17 pm by nuggetz Reason: 1
I dont understand this. I got processing running along with the arduino. The processing script loads and runs and displays time in the serial monitor. I have to close the arduino monitor before I can run the time program via processing. The arduino terminal window states that its waiting for synch but no matter what I do in processing, nothing happens. I tried clicking in the serial window in the processing script but it never feeds the time to the arduino. How do I know what's going on since they're both using the same serial port? I'm missing something here.

mem

>I can to close the arduino monitor before I can run the time program on it

is that a question or a statement? whichever, its not clear what you want to say.

>The arduino terminal window states that its waiting for synch
> but no matter what I do in processing, nothing happens

You can't run the Arduino Serial Monitor and Processing on the same serial port at the same time. To use Processing to set the clock you need to make sure the serial port in the Processing sketch is set to the port used by Arduino (and that means that the Arduino Serial Monitor must not be running).

You can tell when the clock is set when you see the time updated every second. If you don't see this, check that the serial port used by Processing is the same as the one connected to Arduino

nuggetz

Well, thats the thing. I know that I cant use both at the same serial monitors at the same time. I'm assuming that the arduino code must be uploaded to the board and then processing will tell it the time, correct? In that case, if I launch processing, should the arduino program be closed completely? Processing seems to grab the same com port without any problem as long as I haven't opened it with arduino. If I load the code on the arduino and check the serial window, it says its waiting to synch which I think is normal. I close the serial window, launch processing, I see it grab com3 and I click the processing window that pops up. Now, if I go back to the arduino and open the serial window, it says its still waiting for synch.

mem

>Now, if I go back to the arduino and open the serial window, it says its still waiting for synch.

That's because the Arduino IDE resets the board when you start the Arduino Serial Monitor.  You can use Processing to view the serial output from Arduino to verify that the clock is set.

Go Up