DS1307RTC can not get pctime from Ubuntu 12.04

Hi Team

I am just starting on Arduino and trying to run a simple clock sketch. Facing problem with get pctime and make clock to count. Tried some examples sketches and error persists. Need your help to move forward. Another small point can not display correctly weekday ! Many Thanks in advance for any guidance on what is missed/wrong.

Here is my sketch:

/ Daily alarm using a DS1307 RTC
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

char *dayOfWeek[] = {"", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
Serial.begin(9600);
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");

Alarm.alarmRepeat(06,30,0, Event1); // every day
lcd.begin(16,2);
pinMode(13, OUTPUT);
}
void loop () {
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
clockDisplay();
Alarm.delay(1000);
}

void clockDisplay()
{
char date[15]; // -------------------- RTC Time --------------------
sprintf(date, "%02d/%02d/%4d %dayOfWeek[weekday()]", day(), month(), year(), weekday());
lcd.setCursor(0,0);
lcd.print(date);

char time[9]; // ------------------ Arduino Time -----------------
sprintf(time, "%02d:%02d:%02d", hour(), minute(), second());
lcd.setCursor(4,1);
lcd.print(time);
}

void Event1(){
digitalWrite(13, HIGH); //alarm test output
Alarm.delay(4000);
digitalWrite(13, LOW);
}

/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}

PLEASE USE CODE TAGS WHEN POSTING CODE

What is the problem you are actually having? Saying you have an error is not enough - you must tell is what the error is and how you replicate the error.

Thanks for reply

OK. Let's go:

  • Can compile without errors and upload.
  • When I use function setTime(hr,min,sec,day,month,yr); I can make it run, however can not get time from PC;
  • Serial Monitor shows RTC has set the system time (ttyusb0);
  • When removing USB and power and replug after minutes, clock keeps right time counting; so DS1307RTC working.

Main problems:

  1. Can not get pctime from Ubuntu. Needed setTime function to make that happens
  2. weekday not showing; seems not calling correct function name

Best Regards

adpinto

OK, but how are you trying to send the time from Ubuntu to the Arduino? I see no sending code there - where is it?

Hi Majenko. Great Job !!! Seems this is the missed piece !!

If you have an example on how to get that done I would appreciate that, while I am already looking for it.

Thanks and we can consider this almost closed. XD

Regards,

adpinto

Where did you get your original code from? I'm sure there must be example PC-side code available there.

Hi Master Majenko

After a carefull reading on code I realized that it is there (unix time_t) so I am back to this problem !!.

I just integrated pieces together and compiled. Got basis from TimeRTCSet example from Time Library, adding weekday (still wrong), LiquidCrystal and TimeAlarms Libraries, both working fine.

/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}

Ok, so looking at that it looks like it's expecting the PC to send something along the lines of

T1386700387

which is a T (HEADER) followed by the current timestamp from the time() function, or the output of "date +%s"

So you could send that manually through the serial monitor, or write a program to open the serial port, wait for the Arduino to finish rebooting, then send the data.

OK
As stated on reply #2, I solved using function setTime(hr,min,sec,day,month,yr);

I will no longer try to understand why it can not get work over Ubuntu serial... Let's consider this thread closed !!

Many Thanks Majenko. Think I had a great learning day...

Brgds,
adpinto

Maybe my ICSC library would help? It has example Linux code with it for communicating between Linux and an Arduino.