Problem setting the system date from the sketch

Hi all,

I'm new to Arduino's world and that's my very first post.

I'm working with an Arduino YUN connected to a RTC module and everything works fine: I can interface with the board via I2C and I can get the date and time. The need here is to have the correct sys date even in case YUN loses the wifi connection or power is missing. Now, to meet this requirement I need the sketch to retrieve date and time form the RTC and set the sys date accordingly. I'm trying to use the Process lib to call 'date' process on OpenWrt side, but it seems it doesn't work at all.

Here my code:

Process p;            
p.begin("date");      
p.addParameter(unixDateTime); 
p.run();

Where unixDateTime is the date and time coming from RTC in the correct format accepted by date. System date is not updated. I tried to manually set the date and time via SSH using the same String I'm passing as parameter and I receive the follwoing error:

$ date 052113422011.53
date: bind: Permission denied
date: settimeofday (timeval): Operation not permitted

So I tried to add 'sudo' this way:

Process p;            
p.begin("[b]echo arduino | sudo[/b] date");      
p.addParameter(unixDateTime); 
p.run();

Again, not working. Does anybody know the right way?

Thanks

Use RTC then should be disable NTP

nano /etc/config/system
#config timeserver 'ntp'
#       list server '0.openwrt.pool.ntp.org'
#       list server '1.openwrt.pool.ntp.org'
#       list server '2.openwrt.pool.ntp.org'
#       list server '3.openwrt.pool.ntp.org'
reboot

correct set date/time command ( busy-box's ) is

/bin/date -s "`/bin/date -d @1403299666 "+%Y-%m-%d%l:%M:%S"`"

Disabling the NTP made me able to manually change the date - and for sure it's the right way to go in my case.

Also the 'date' command I previously used was wrong, looking at your suggestion and OpenWrt wiki http://wiki.openwrt.org/doc/howto/ntp.client it's clear I have to use a different format that has to be [[[[[YY]YY]MM]DD]hh]mm[.ss] (or YYYY-MM-DD hh:mm[:ss]). Unfortunately I don't really understand the date/time command you are suggesting: let me explain.

If I now execute the following command via SSH:

date -s 201106210730.51

the date/time is correctly configured. But if I put this code into my sketch, it seems that nothing happens:

Process p;            
p.begin("date");      
p.addParameter(String("-s " + unixDateTime)); 
p.run();

Where unixDateTime is "201106210730.51" (controlled with a Serial.print command)

So my questions are now:

1) how can I be sure my command is correctly passed to OpenWrt side? is there a way to check/print/log the call I'm doing from the sketch? or a return value I can check.

2) why are you suggesting to run a command so "complex" instead of just calling

date -s 201106210730.51

that is working when launched directly from a SSH shell? Is there something in Process library I should study better or is it strictly related to Linino?

Thanks

Test this code ;-

Process p;            
p.begin("/bin/date");      
p.addParameter("-s"); 
p.addParameter("201406210730.51"); 
p.run();

1) how can I be sure my command is correctly passed to OpenWrt side? is there a way to check/print/log the call I'm doing from the sketch? or a return value I can check.

Write python or ash script wrapper then log it to file system.

why are you suggesting to run a command so "complex" instead of just calling

unixDateTime at most case means Unix epoch time format, otherwise the format is related to culture and language, more close to culture than language ( American English v.s. British English). http://en.wikipedia.org/wiki/Date_(Unix)

My "complex" command convert Unix epoch time format to the "date -s" compatible format.

Your code works like a charm! Thanks a lot. The problem was related to the way I used the Process.addParameter() function.