Time and TimeAlarms Libraries – Ask here for help or suggestions

ctime is not found in - avr-libc: AVR Libc -

maybe this link helps - time.c - sanos source -

Rob, did you try to use it in the Arduino IDE?
If so, a brief example would be useful.

No,

however I did use the RTC lib for the DS1307 - - GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library - It includes a DateTime Class that has all needed to implement a time2string(...) function
Note DateTime is year 2000 based, the essential code is in the DateTime constructor that can be transformend quite easily, see below:

void setup()
{
  Serial.begin(115200);
  Serial.println("UnixTime simulator :) ");
}

void loop()
{
  char buffer[20];
  time2string(millis(), buffer);
  Serial.print(millis());
  Serial.print(" ==>  ");
  Serial.println(buffer);
}

uint8_t daysInMonth [] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

void time2string(unsigned long t, char *buf)
{
  uint8_t yOff, m, d, hh, mm, ss;

  ss = t % 60;
  t /= 60;
  mm = t % 60;
  t /= 60;
  hh = t % 24;
  uint16_t days = t / 24;
  uint8_t leap;
  for (yOff = 0; ; ++yOff) {
        leap = yOff % 4 == 0;
        if (days < 365 + leap)
            break;
        days -= 365 + leap;
  }
  for (m = 1; ; ++m) {
    uint8_t daysPerMonth = daysInMonth[ m - 1];
    if (leap && m == 2)
      ++daysPerMonth;
    if (days < daysPerMonth)
        break;
    days -= daysPerMonth;
  }
  d = days + 1;
  sprintf(buf,"%4d-%02d-%02d %02d:%02d:%02d", 1970+yOff, m,d, hh,mm,ss);
}

thanks Rob, Mem..

Many C compilers have a function named ctime that produces a date string but I don't think its available with the arduino tools. I have always used multiple print statements to display the data, is there a reason you can't do that for your application?

My Goal is sending unixtime via Wireless, then I have data in char array (char[11])
so when i try to send data unixtime which it is unsigned long, the compiler give error message:

incompatible types in assignment of 'time_t' to 'char [11]'

Rob, I use Mem's library (Time, TimeAlarm, DS1307RTC)..Any suggestion how to do it for DS1307RTC library?thanks anyway for the help.

thanks a lot.

do you mean some thing like this?

#include "stdlib.h"

void setup()
{
  Serial.begin(115200);
  char buffer[11];
  unsigned long unixtime=1234567890L;  // L for long
  ltoa(unixtime, buffer, 10);
  Serial.println(buffer); 
}

void loop(){}

(code not tested)

see - avr-libc: <stdlib.h>: General utilities - search for ltoa

UPDATE
-- added void before loop()

robtillaart:
do you mean some thing like this?

#include "stdlib.h"

void setup()
{
  Serial.begin(115200);
  char buffer[11];
  unsigned long unixtime=1234567890L;  // L for long
  ltoa(unixtime, buffer, 10);
  Serial.println(buffer);
}

loop(){}



(code not tested)

see - http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html - search for ltoa

FOOL me, i tried it, but i didn't print the buffer..
Thanks, a lot..i'll try tomorrow. now, 3.05am..here..need a sleep..aha!

you need to add void before loop(){}

allready patched in posting above.

robtillaart:
you need to add void before loop(){}

allready patched in posting above.

the code just works..thanks, rob..
ltoa and atol will be my new friends..LOL..

welcome,

surf one evening over the - avr-libc: <stdlib.h>: General utilities - site and you learn thousand new possibilities :wink:

How to check alarm time on timeAlarm library?
it is possible to know the "alarm ID" is enable or disable?
I got this code from the old forum

void checkAlarm(){

  showAlarmTime(ID1);
  showAlarmTime(ID2);
  showAlarmTime(ID3);
  showAlarmTime(ID4);
  showAlarmTime(ID5);
  showAlarmTime(ID6);

  Serial.println("----------------------------------------------------");
}//checkAlarm

void showAlarmTime(AlarmID_t id){
  time_t alarmTime = Alarm.read(id);
  if(alarmTime != 0)
  {
    if( alarmTime <= SECS_PER_DAY)  
      Serial.print(" repeat alarm with ID ");
    else  
      Serial.print(" once only alarm with ID  ");
    Serial.print(id, DEC);    
    Serial.print(" set for ");
    digitalClockDisplay1(alarmTime);
  }
}//showAlarmTime

but, i guess those code doesn't show the alarm is disabled or enabled.
thanks for any suggestions.
regards,
nug.

it is possible to know the "alarm ID" is enable or disable?

How does the alarm become enabled or disabled? Your code does that. Make your code remember that status it set the alarm to. You need to keep track of the alarm IDs, so a structure that linked alarm ID and status should be easy to implement.

struct alarmData
{
   int alarmID;
   bool armed;
};
typedef struct alarmData AlarmData;
AlarmData ad;
ad.alarmID = ?; // Assign the alarm ID
ad.armed = false; // It is not enabled

Whenever you change state, set the structure member, too.

Hi, I've been using the Time library for a while just based on the arduinos timers. My sketch has functions to setTime() and uses now() to get it. I sometimes want to set the time to something specific for testing but having the RTC time as default is best.
I've picked some code out of the examples to get the time from the RTC when the sketch starts.

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) {
     Serial.println("Unable to sync with the RTC");
  }
  else{
    Serial.println("RTC has set the system time");      
  }
  digitalClockDisplay(now());     cout<<endl;

If I later call setTime() with my test time it seems to stay set but I worry that some synch function might bite me when I call now() - will it? what it i don't setSyncProvider - can i get the RTC time into the library without doing that?

Also, thank you for the library - it's been a great help.

I didn't find this thread until after I already posted here:

I'm trying to figure out how Time handles the situation when millis() rolls over but I'm not getting it. I'd love a nudge in the right direction.

Cheers!

Hi,

I'm trying to use the Time library with arduino 1.0-beta4 for OSX (library is located at ~willem/Documents/Arduino/Libraries/Time) when I compile I get

UdpNtpClient_WE.cpp:1:18: error: Time.h: No such file or directory

I guess something has changed for IDE 1.0 (beta) .... who knows the solution ?

Cheers, Willem.

Hi all,

I'm using the time library for my home energy monitoring project. In this project, I count the pulses coming from my electricity, gas and water meter. I also read out a temp/humidity sensor (SHT15) located in my garage.
Each time a change in one of the "sensors" occurs (i.e pulse or value change from temp/humidity sensor) , a UDP message with a small XML string is sent to a monitoring system. Below is an example of such a message (when a value change occurs for the humidity sensor) :

<e>
<s id="6" vChMSRo="0" vChMS="1599330" vChDT="2011-09-15T10:06:01" val="487" />
</e>

Explanation :
id = sensor identification
vChMSRo = value change millis rollover (I count the number of times the millis() rolled over since start-up)
vChMS = number of milliseconds at which the value change occurred
vChDT = date/time at which the value change occurred
val = the actual value

And below is one for a pulse counter (I'm using interrupt service routines for counting pulses) :

<e>
<s id="1" pIDRo="0" pID="3" pStartMSRo="0" pStartMS="8162174" pStartDT="2011-09-15T11:55:27" pDurMS="138" pIntMS="0" state="0" />
</e>

Explanation :
id = sensor identification
pIDRo = number of times the pulse ID rolled over
pID = pulse ID
pStartMSRo = number of times the millis() rolled over at the time the pulse started
pStartMS = millis() value at the time the pulse started
pStartDT = date/time the pulse started
pDurMS = duration of the pulse in ms
pIntMS = interval between this pulse and the previous pulse (in ms)
state = current state of the pulse

The XML string contains - among other info - the date and time at which the event occurred.
For each (pulse) sensor, the date/time is stored in a time object.
For reporting, I'm using the following function to convert the time object to a date/time string :

char* XMLDateTime(time_t t)
{
   char chrDT[21];
   PString strDT(chrDT, sizeof(chrDT));
  
   strDT.print(year(t));
   strDT.print("-");
   if (month(t) < 10)
   {
      strDT.print("0");
   };
   strDT.print(month(t));
   strDT.print("-");
   if (day(t) < 10)
   {
      strDT.print("0");
   };
   strDT.print(day(t));
   strDT.print("T");
   if (hour(t) < 10)
   {
      strDT.print("0");
   };
   strDT.print(hour(t));
   strDT.print(":");
   if (minute(t) < 10)
   {
      strDT.print("0");
   };
   strDT.print(minute(t));
   strDT.print(":");
   if (second(t) < 10)
   {
      strDT.print("0");
   };
   strDT.print(second(t));
   strDT.print("Z");
   return chrDT;
}

The problem :
Very frequently I get crappy output in the date/time string part, like this :

<s id="5" vChMSRo="0" vChMS="8462268" vChDT="2011-09-15	!Ó" val="243" />
<s id="6" vChMSRo="0" vChMS="8433906" vChDT="2011-09!ÓS" val="507" />

It seems that sometimes a kind of buffer overflow occurs. Initially, I used the String object, but that made it even worse! (PString is a buffer overflow save string object).
Could you please check what could be the reason for this strange output?

Kind regards,

Bart.

willem4ever:
Hi,

I'm trying to use the Time library with arduino 1.0-beta4 for OSX (library is located at ~willem/Documents/Arduino/Libraries/Time) when I compile I get

UdpNtpClient_WE.cpp:1:18: error: Time.h: No such file or directory

I guess something has changed for IDE 1.0 (beta) .... who knows the solution ?

Cheers, Willem.

Here is an NTP code for Arduino 1.0:

/*
 * Time_NTP sketch
 * Example showing time sync to NTP time source
 *
 * This sketch uses the Time library
 * and the Arduino 1.0 Ethernet library
 */

#include <Time.h>
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUDP.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 44 }; // set this to a valid IP address (or use DHCP)

unsigned int localPort = 8888;      // local port to listen for UDP packets

IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in first 48 bytes of message
byte packetBuffer[ NTP_PACKET_SIZE]; // buffer to hold incoming/outgoing packets

time_t prevDisplay = 0;    // when the digital clock was displayed

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  Serial.println("waiting for sync");
  setSyncProvider(getNtpTime);
  while(timeStatus()== timeNotSet)
     ; // wait until the time is set by the sync provider
}

void loop()
{
  if( now() != prevDisplay)   //update the display only if the time has changed
  {
    prevDisplay = now();
    digitalClockDisplay();
  }
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(dayStr(weekday()));
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(monthShortStr(month()));
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding
  // colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*-------- NTP code ----------*/

unsigned long getNtpTime()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  delay(1000);
  if ( Udp.parsePacket() ) { 
     Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read packet into buffer


     //the timestamp starts at byte 40, convert four bytes into a long integer
     unsigned long hi = word(packetBuffer[40], packetBuffer[41]);
     unsigned long low = word(packetBuffer[42], packetBuffer[43]);
     // this is NTP time (seconds since Jan 1 1900
     unsigned long secsSince1900 = hi << 16 | low;  
     // Unix time starts on Jan 1 1970
     const unsigned long seventyYears = 2208988800UL;     
     unsigned long epoch = secsSince1900 - seventyYears;  // subtract 70 years
     return epoch;
  }
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress address)
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0

  // Initialize values needed to form NTP request
  packetBuffer[0] = B11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum
  packetBuffer[2] = 6;     // Max Interval between messages in seconds
  packetBuffer[3] = 0xEC;  // Clock Precision
  // bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
  packetBuffer[12]  = 49;  // four-byte reference ID identifying
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // send the packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

tigro:
Hi all,

I'm using the time library for my home energy monitoring project.
The problem :
Very frequently I get crappy output in the date/time string part, like this :

It seems that sometimes a kind of buffer overflow occurs. Initially, I used the String object, but that made it even worse! (PString is a buffer overflow save string object).
Could you please check what could be the reason for this strange output?

Kind regards,

Bart.

Bart,
You can check if the problem is due to the chrDT buffer overflowing by testing with a hard coded string of the correct length for the date.
If you still have the problem this is probably not a Time library issue and if that is the case then best to move the discussion to a new thread.

hello there :slight_smile:

back from testing my code concerning a web enabled programmer you can try it here :

http://sprinkler.zapto.org/arduinoJqueryUI.html
user/password viknet/viknet

mem, there (as always I think) is two bug I had to correct in your library:
in the function updateNextTrigger,

I removed the check if next trigger is not yet in the future:
if the check is already in the future but you just changed the alarm to something else, the updatetrigger won't be effective immediately

I also change the write function to :

if(isAllocated(ID))
      {
        Alarm[ID].value = value;
        Alarm[ID].Mode.alarmType=period;    
        if (value!=dtINVALID_TIME)
          enable(ID); 
        else
          free(ID);
      }

because trying to set the alarm to 0 was leaving alarm in a strange state before.

all in all I am more and more convinced that 0 should be treated as a valid alarm value meaning 0:00:00 today, the main reason for that when setting an alarm to 24:00:00 (which currently work) if you read the value and apply the usual hour() minute() second() it reply 00:00:00 which is kind of wrong.

To avoid this I disabled the possibility to set an alarm at midnight : 00:00:00 and 24:00:00 are not valid time in my project :slight_smile:

Mem/Michael,
I wasn't 100% sure how to set the time, so much thanks for the code snippet/s :slight_smile:

  • Red

Hello
My name is Joe
I have made clock from this page "The TC18 – My IV-18 Based VFD Tube Clock – vonnieda.org" but it doesn't have alarm,so I would like to add it.
This is first time for me to do something with arduino,can someone add alarm part for clock sketch for me please.

Im attaching sketch file

Thanks

TC18.pde (23.1 KB)