Pages: 1 ... 3 4 [5] 6 7   Go Down
Author Topic: Who needs a Software Real Time Clock?  (Read 10819 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd never considered the idea that the computer would be giving the arduino the time in epoch seconds, but not in UTC. This may be the difference between operating system assumptions. On a Unix system it's fairly common (ie the standard case) for the platform to always have the time in UTC, and then a configuration specifies the local time zone.

I generally feel that handling time zones correctly is a to go down the rabbit's hole. You can't just have offsets because of daylight savings. Where I live (East coast of the US) we're +5 hours from UTC, but sometimes we deviate with daylight savings. So if you have your clock running w/o a computer for an extended period, you're going to end up with the wrong time (just like my bedroom clock and microwave).

And to make matters worse, that deviation changes with time. I think it was just a year ago they changed daylight savings time in the US so it's 3 weeks off from where it was, hence my comment about not being able to correctly display historical times. That's why the zoneinfo files are so large. I'm not advocating that; but I'm wondering about the utility of a set of functions which look at the date and make the adjustment accordingly for the timezones as they are at this moment.

Probably the right answer is for that to be a separate library, much like the DateTimeStrings are separate.

It's just a thought. It doesn't impact what I'm doing with the library right now.

It's a nice library, so thanks for clearing up the license question too.
Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The processing sketch in the library distribution that sets the time  does handle local time zone offset and daylight savings time, so the Arduino is set to the correct local time. Of course, the Arduino code is currently oblivious to time zones and DST  so it will ignore any changes in daylight savings wile disconnected until its resynched again from the computer.

The change proposed my previous post would provide a local correction for the displayed time but because the system time is not affected, date math will still be correct – i.e. not affected by changes in offsetFromUTC.

I will probably add the function to display time from a given offset in next release, but will see how many people demand onboard DST correction before thinking any more about that.

I agree about the rabbit hole comment  smiley-wink

Thanks for your interest.

Have fun!
Logged

Eugene Oregon, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Toys!?! I have TOYS!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello! another novice here.

I have been using Arduino for about a month and the DateTime and DatreTimeString libraries are the first I have tried to add myself.

I am getting warnings the first time I start the Arduino programming interface (v 0016) on my computer (Windows XP).

Errors:

DateTimeStrings.cpp:32: warning: only initialized variables can be placed into program memory area
DateTimeStrings.cpp:33: warning: only initialized variables can be placed into program memory area
DateTimeStrings.cpp:34: warning: only initialized variables can be placed into program memory area
   .
   . (numbering sequence continues - just did not want to bore anyone <grin> )
   .
DateTimeStrings.cpp:64: warning: only initialized variables can be placed into program memory area


then I load the DateTime.pde.  (since I am a n00b I did not know if the errors were expected or not)

clicked verify and no errors were reported, then I uploaded it, and again no errors, however there is no responce, either from the onboard LED or from the serial monitor set to 19200 baud.

I copied the top part of the DateTime.pde code as it appears in my programmer incase the coloring difference in the include lines means anything to anyone.

I am working on a project based on Garduino from dirtnail.com that uses the date functions for automating a greenhouse's growing environment.  I believe I can get away from using the DateTimeStrings library but I can see where t would be great for other projects I have in mind and am currently clueless how to correct the problem. Any help is appreciated.

-Michael (Endante)
Quote

// DateTime.pde
// example sketch for the DateTime library

#include <DateTime.h>
#include <DateTimeStrings.h>

#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

void setup(){
  Serial.begin(19200);
  pinMode(13,OUTPUT); // we flash the LED each second
}
Logged

Now if I could just get Arduino to work with my WoW account....A whole guild of Toonduinos. "All your bases are belong to us!"

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The example sketch only starts outputting the time after the clock has been set. In that example, this is done by sending a time string to the arduino serial port. There is an example Processing sketch that does this but if you want to test Arduino code without running the  Processing sketch,  you can add the following line to the Arduino code at the end of setup:

 DateTime.sync(1230768000);

This will start the clock from Jan 1 2009 when the sketch begins.

Once you have this working you can add the appropriate code to set the clock for your application.
Logged

Eugene Oregon, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Toys!?! I have TOYS!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks MEM!  That was it!

I had no idea how to use the Processing sketch (n00b here thought it was a name for another sketch that ran in the Arduino editor) now I have Processing downloaded and ran it there after uploading the Arduino sketch.   Everything works great!
Logged

Now if I could just get Arduino to work with my WoW account....A whole guild of Toonduinos. "All your bases are belong to us!"

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Me!

I am currently designing a new kind of an analog clock and this library comes in right handy!

The sample code with the sync from PC to the arduino is great too!
I changed TIME_HEADER to "U", so you can send TIME with the unix command: $ date +U%s > /dev/ttyUSB0
That works fine!

Maybe this could be integrated into aother sub-library?

Also it would be really cool, to get the drift adjustment working by sending the time to the arduino a second time (a few days later) and let it calculate the adjtime thing itself.

thanks anyway for this contribution!
Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

epe, I am happy to hear that you find the library is useful and would like to know more about your analog clock project.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This python code tested with Windows XP synchronizes the datetime example in the playground and captures the data to a file

It allows time stamped data saved in a text file.

Code:
# Time synch Arduino & save data in file

file = open("test.txt","w")   # Open file for write

import serial, time           # Setup port COM4
ser = serial.Serial(3, 38400, 8, 'N', 1, None)
time.sleep(3)                 # Allow initialization time
print ser.portstr             # Current port in use

# UTC time + timezone offset + daylight offset
tic = time.time() - time.timezone + time.daylight*3600
tics = '\377' + str(tic)[0:10]

ser.write(tics)               # Send timesync to Arduino

data = ' '
while data[0:4] != 'Stop':         # Process until 1st 4 chars = Stop
    data = ser.readline()
    print data
    file.write(data)

ser.close()                   # Close serial port
file.close()                  # Close file
« Last Edit: July 16, 2009, 01:21:57 pm by mlindeblom » Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great to see  interface code for other environments.

I was thinking of redoing the example sketch in the playground and replacing the header with a printable character (like 'T')  so that octal constants such as 377 were not needed.

Would that change be a help or not?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any unique character is fine.

I would not change the playground example other than adding more details/clarification information.

I think a datalogger example would demonstrate the library better.
Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I think a datalogger example would demonstrate the library better.

Do you have something specific in mind? perhaps you can you say more about what you think the sketch would do.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 47
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have something specific in mind? perhaps you can you say more about what you think the sketch would do.]
Use http://en.wikipedia.org/wiki/Data_logger for definition of terms.

If one time synchronizes a battery operated Arduino from a PC and then moves the unit to the measurement site, a few analog and/or digital inputs could be captured at a fixed interval.  Summary data would be stored in eeprom.  The unit would be brought back to the PC for data extraction for post processing via speadsheets or whatever.

Uses include environmental measurements (household temperatures, weather station), battery discharge tester (RC modellers, FE experiementers).

In summary, a good reliable template software/hardware? for general purpose use.

It may be desirable to use a table driven approach to specify digital and analog inputs, sampling times and software filters.
Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

mlindeblom, I know what a data logger is. I have built quite a few in my time, including an arduino version using SD cards for external storage to capture GPS data. But its way too complex to serve as an example for this library. When asking if you had something specific in mind, I was wondering if you would say more about what you thought the example sketch should contain.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Someone pm'd me with interest in the time server sync code I wrote.  I thought I'd post the code here should anyone else want to use it.  I made this into a pretty simple sketch that is part of a much larger project.  Right now it simply prints the time on the serial port every second as an example.  One caveat, is that it uses the WString.h library.  The library is very nice but needs to be patched to plug a memory leak.  Check here for the info on that.

Quote

 
//**************************************************************//
//  Name    : TimeClient                                        //
//  Author  : Digger450                                         //
//  Date    : 07/24/2009                                        //
//  Version : 1.0                                               //
//****************************************************************

#include <Ethernet.h>
#include <WString.h>
#include <DateTime.h>
#include <DateTimeStrings.h>

// network configuration.  gateway and subnet are optional.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 191 }; // Local IP
byte timeServer[] = {192, 43, 244, 18 }; // Time Server

Client timeClient(timeServer, 13);

String lineBuffer = String(255);

long daylightTime = -5;
long standardTime = -6;
unsigned long updateTime = 3590000L;
#define TIME_MSG_LEN  11   // time sync to PC is HEADER and unix time_t as ten ascii digits
#define TIME_HEADER  255

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("Clock Online");
}

void loop()
{
  checkTime();
  printTime();
  delay(1000);
}

void checkTime()
{
  unsigned long tempTimeComp = millis();
  boolean responseRec = false;
  long timeOut = millis();
  long tempTime = 0;
  
  long currentMJD;
  long unixMJD = 40587;
  long currentHour;
  long currentMin;
  long currentSec;
  int dstVal;
  long dstTime;
  long unixTime;
  
  lineBuffer = "";  
  
  if (abs(tempTimeComp - updateTime) > 3600000L)
  {
    Serial.println("Updating Time");
    if (timeClient.connect())
    {
      Serial.println("Connected");
      while (!responseRec)
      {
        tempTime = millis();
        if ((abs(tempTime - timeOut)) > 10000)
        {
          Serial.println("Timeout On Response");
          return;
        }
        if (timeClient.available())
        {
          char inChar = timeClient.read();
          if (lineBuffer.length() < 255)
          {
            if (inChar != '\n')
            {
              lineBuffer.append(inChar);
            }
          }
          if (lineBuffer.contains("*"))
          {
            currentMJD = atol(lineBuffer.substring(0, 5));
            currentHour = atol(lineBuffer.substring(15, 17));
            currentMin = atol(lineBuffer.substring(18, 20));
            currentSec = atol(lineBuffer.substring(21, 23));
            dstVal = atoi(lineBuffer.substring(24, 26));
            if (dstVal > 0)
            {
              dstTime = daylightTime;
            }
            else
            {
              dstTime = standardTime;
            }
            
            unixTime = (currentMJD - unixMJD) * 86400L;
            unixTime += (currentHour * 3600);
            unixTime += (currentMin * 60);
            unixTime += currentSec;
            unixTime += (dstTime * 3600);
            Serial.println(unixTime);
            DateTime.sync(unixTime);
            
            responseRec = true;
            updateTime = millis();
            timeClient.stop();
          }
        }
      }
    }
    else
    {
      timeClient.stop();
      Serial.println("Connection Failed");
      delay(5000);
    }
  }
}

void printTime()
{
  DateTime.available();
  Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
  Serial.print(" ");
  Serial.print(DateTimeStrings.monthStr(DateTime.Month));
  Serial.print(" ");
  Serial.print(DateTime.Day, DEC);
  Serial.print(" ");
  Serial.print(DateTime.Hour,DEC);
  Serial.print(":");
  if(DateTime.Minute < 10)
    Serial.print('0');
  Serial.print(DateTime.Minute,DEC);
  Serial.print(":");
  if(DateTime.Second < 10)
    Serial.print('0');
  Serial.println(DateTime.Second,DEC);
}



 
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI - I am quite new here. My first project is creating a GPS synced clock (just a an exercise). The LCD already displays the GPS time in UTC nicely (together with speed, course and altitude) - thangs to the TinyGPS library. I now need to add the 10 hours offset to the UTC GPS time to have the correct time zoen for Canberra / Australia.

Would this be possible with the DateTime library?

I am thinking alongside the following:
- converting the int values for sec, min, hour, day, month, year to unix seconds
- adding the UTC offset
- converting back to sec, min, hour, day, month, year (with correct leap year calc etc)

Can anyone help a newbie out here? Any help appreciated

Cheers jampac
Logged

Pages: 1 ... 3 4 [5] 6 7   Go Up
Jump to: