10

I want to sync the Time from my pc to the arduino. I am using their Time library but it does not work.

How can I get the arduino to have the same time as on my computer ? I am currently using a mac.

Their documentations says :

On a unix system, you can set the time with the shell command:

TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbserial-A8008pym

I tried on the terminal

>export TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbmodemfd131 

and I get Permission denied.

What am I doing wrong? Is there a simpler way to sync the time on arduino with my computer?

The Code

#include <Time.h>  

#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
#define TIME_REQUEST  7    // ASCII bell character requests a time sync message 

void setup()  {
  Serial.begin(9600);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

void loop(){    
  if(Serial.available() ) 
  {
    processSyncMessage();
  }
  if(timeStatus()!= timeNotSet)   
  {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
    digitalClockDisplay();  
  }
  delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(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);
}

void processSyncMessage() {
  // if time sync available from serial port, update time and return true
  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    
        }
      }   
      setTime(pctime);   // Sync Arduino clock to the time received on the serial port
    }  
  }
}

time_t requestSync()
{
  Serial.print(TIME_REQUEST,BYTE);  
  return 0; // the time will be sent later in response to serial mesg
}
Conrad C
  • 746
  • 1
  • 11
  • 32
  • @sachleen Permission denied . Sorry for the oversight – Conrad C Nov 11 '12 at 21:12
  • You could try with sudo in front of the command? – David K Nov 11 '12 at 21:12
  • @DavidK WARNING: Improper use of the sudo command could lead to data loss or the deletion of important system files. Please double-check your typing when using sudo. Type "man sudo" for more information. To proceed, enter your password, or type Ctrl-C to abort. Password: sudo: export: command not found -bash: /dev/tty.usbserial-A8008pym: Permission denied – Conrad C Nov 11 '12 at 21:15
  • Ok try the following, sudo su, to get a root terminal, then try that command once more. – David K Nov 11 '12 at 21:17
  • export TZ_adjust=-8; is just creating a variable called TZ_adjust and setting it to -8 for use towards the end of the command.. so if it fails again, try: sudo echo T$(($(date +%s)+6060$-8)) > /dev/tty.usbmodemfd131 – David K Nov 11 '12 at 21:18
  • Conrads-MacBook-Pro:~ conradchamerski$ sudo su export TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbserial-A8008pym Password: su: unknown login: export – Conrad C Nov 11 '12 at 21:24
  • I apologise Conrad, I did not make that clear, ok do the following: sudo su then hit return. then export TZ_adjust=-8; echo T$(($(date +%s)+6060$TZ_adjust)) > /dev/tty.usbmodemfd131 and then hit return. – David K Nov 11 '12 at 21:25
  • @DavidK I tried you last comment and I get: sudo echo T$(($(date +%s)+6060$-8)) > /dev/tty.usbmodemfd131 -bash: 1352669116+6060himBH8: value too great for base (error token is "6060himBH8") Is there another way to get the time? – Conrad C Nov 11 '12 at 21:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19409/discussion-between-david-k-and-conrad-c) – David K Nov 11 '12 at 21:27
  • @DavidK Thank you for the clarifications. I now get : ` sh: /dev/tty.usbmodemfd131: Operation not supported` – Conrad C Nov 11 '12 at 21:28

1 Answers1

10

Conrad and I discovered the solution after a 20 minute chat:

To set the variable to EST
TZ_adjust=-5;

sudo echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/tty.usbmodemfa131
Community
  • 1
  • 1
David K
  • 692
  • 10
  • 23