Difference between revisions of "Arduino: Time"

From OnnoWiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
* https://github.com/PaulStoffregen/Time/archive/master.zip
 
* https://github.com/PaulStoffregen/Time/archive/master.zip
 
* http://playground.arduino.cc/uploads/Main/DS1302RTC.zip
 
* http://playground.arduino.cc/uploads/Main/DS1302RTC.zip
 +
* https://github.com/PaulStoffregen/DS1307RTC/archive/master.zip
  
 
Lakukan
 
Lakukan
Line 22: Line 23:
 
  unzip DS1302RTC.zip
 
  unzip DS1302RTC.zip
  
 
+
cp DS1307RTC-master.zip /usr/share/arduino/libraries
 +
cd /usr/share/arduino/libraries/
 +
unzip DS1307RTC-master.zip
 +
mv DS1307RTC-master DS1307RTC
  
 
==Code==
 
==Code==
  
  
 +
/*
 +
  * TimeSerial.pde
 +
  * example code illustrating Time library set through serial port messages.
 +
  *
 +
  * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 +
  * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 +
  T1357041600 
 +
  *
 +
  * A Processing example sketch to automatically send the messages is included in the download
 +
  * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 +
  */
 +
 
 +
#include <TimeLib.h>
 +
 +
#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);
 +
  while (!Serial) ; // Needed for Leonardo only
 +
  pinMode(13, OUTPUT);
 +
  setSyncProvider( requestSync);  //set function to call when sync required
 +
  Serial.println("Waiting for sync message");
 +
}
 +
 +
void loop(){   
 +
  if (Serial.available()) {
 +
    processSyncMessage();
 +
  }
 +
  if (timeStatus()!= timeNotSet) {
 +
    digitalClockDisplay(); 
 +
  }
 +
  if (timeStatus() == timeSet) {
 +
    digitalWrite(13, HIGH); // LED on if synced
 +
  } else {
 +
    digitalWrite(13, LOW);  // LED off if needs refresh
 +
  }
 +
  delay(1000);
 +
}
 +
 +
void digitalClockDisplay(){
 +
  // digital clock display of the time
 +
  Serial.print(now());
 +
  Serial.print(" ");
 +
  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() {
 +
  unsigned long pctime;
 +
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
 +
 +
  if(Serial.find(TIME_HEADER)) {
 +
      pctime = Serial.parseInt();
 +
      if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
 +
        setTime(pctime); // Sync Arduino clock to the time received on the serial port
 +
      }
 +
  }
 +
}
 +
 +
time_t requestSync()
 +
{
 +
  Serial.write(TIME_REQUEST); 
 +
  return 0; // the time will be sent later in response to serial mesg
 +
}
  
 +
==Dari Linux PC==
  
 +
Lakukan
  
 +
date +T%s\n > /dev/ttyACM0
  
 
+
Untuk mensinkronisasi waktu
  
 
==Referensi==
 
==Referensi==

Latest revision as of 10:05, 27 February 2016

Sumber:


Download & Install Library

Download

Lakukan

cp Time-master.zip /usr/share/arduino/libraries/
cd /usr/share/arduino/libraries/
unzip Time-master.zip
mv Time-master Time 
cp DS1302RTC.zip /usr/share/arduino/libraries
cd /usr/share/arduino/libraries/
unzip DS1302RTC.zip
cp DS1307RTC-master.zip /usr/share/arduino/libraries
cd /usr/share/arduino/libraries/
unzip DS1307RTC-master.zip
mv DS1307RTC-master DS1307RTC

Code

/* 
 * TimeSerial.pde
 * example code illustrating Time library set through serial port messages.
 *
 * Messages consist of the letter T followed by ten digit time (as seconds since Jan 1 1970)
 * you can send the text on the next line using Serial Monitor to set the clock to noon Jan 1 2013
 T1357041600  
 *
 * A Processing example sketch to automatically send the messages is included in the download
 * On Linux, you can use "date +T%s\n > /dev/ttyACM0" (UTC time zone)
 */ 
 
#include <TimeLib.h> 

#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);
  while (!Serial) ; // Needed for Leonardo only
  pinMode(13, OUTPUT);
  setSyncProvider( requestSync);  //set function to call when sync required
  Serial.println("Waiting for sync message");
}

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

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(now());
  Serial.print(" ");
  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() {
  unsigned long pctime;
  const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013 

  if(Serial.find(TIME_HEADER)) {
     pctime = Serial.parseInt();
     if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
       setTime(pctime); // Sync Arduino clock to the time received on the serial port
     }
  }
}

time_t requestSync()
{
  Serial.write(TIME_REQUEST);  
  return 0; // the time will be sent later in response to serial mesg
}

Dari Linux PC

Lakukan

date +T%s\n > /dev/ttyACM0

Untuk mensinkronisasi waktu

Referensi