2013/08/04

NTP Server Synchronized LCD Keypad Adjustable Clock With Temperature Using Time.h Library

This sketch extend from post on Aug. 01, 2013 by adding synchronizing with NTP server.The Uno will synchronize with NTP server every 6 hours and override the time adjusted by using LCD shield keypad. An alternative to synchronize at certain time is to use Alarm.alarmRepeat() function in <TimAlarms.h>. The latency is about 2 seconds in my case, adjust the latency according your Internet connection condition. The binary sketch size is 19,614 bytes.

Requirements:

1. Arduino Uno R3
2. RTC (DS1307) Sensor Shield
3. W5100 Ethernet Shield
4. LCD Keypad Shield
5. LM35 Temperature Sensor Breakout

Usage

1. Use <RIGHT> keypad to enter set mode.
2. Use <RIGHT> to navigate on parameter to be modified.
3. Use <UP> to set value of the aimed parameter.

4. Use <DOWN> to set value of the aimed parameter.
5. Use <SELECT> to save date and time to RTC.
6. Use <LEFT> to leave set mode.


Schetch

/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * the sketch works on Arduino IDE 1.05
 * 1) modified by Befun Hung on Jul. 28, 2013 
 *    changing the sequence to year, month, day, hour, minute, second
 *    adding the day of week
 *    almost same function as sketch on May 1, 2012
 * 2) modified by Befun Hung on Jul. 29, 2013
 *    change display device to LCD Shield
 *    adding temperature readout from LM35
 *    the sketch does not contain delay() in the loop section 
 * 3) modified by Befun Hung on Jul. 31, 2013
 *    divide digitalClockDisplay() into dateDisplay(), weekdayDisplay(), timeDisplay() and temperatureDisplay()
 *    use time_t t to store the value of now()
 * 4) modified by Befun Hung on Aug. 04, 2013
 *    adding ntpSyncDS1307() to synchronize DS1307 real time clock with NTP server
 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Time.h>
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

char *dayOfWeek[] = {"", "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
int lcdKey = 0;
int adcKeyIn = 0;
time_t t;
int potPin = 3; // change potPin value to 0, 1, 2 for A0, A1, A2 respectly
float temperature = 0;
int displayAtSecond;

// Enter a MAC address for your controller bellow.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(140, 112, 2, 188); // ntp2.ntu.edu.tw NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
// timeZoneOffset = (Time Zone) * 3600L eg. (+8) * 3600L = 28800L for Taipei, Taiwan
const long timeZoneOffset = 28800L; 
// sync to NTP server every "ntpSyncTime" seconds, set to 1 hour or more to be reasonable
unsigned long ntpSyncTime = 21600;
// keep track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;
// adjust the sync latency with computer NTP client in seconds 
unsigned int syncLatency = 2;

void setup()  {
  lcd.begin(16,2);
  lcd.print("*cheaphousetek*");
  lcd.setCursor(0,1);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     lcd.print("Unable to sync");
  else
     lcd.print("Sync system time ");
  displayAtSecond = second();
  delay(2000);
  lcd.clear();
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    lcd.setCursor(0,1);
    lcd.print("DHCP failed");
    for (;;);
  }
  Udp.begin(localPort);
}

void loop()
{
  if ((now() - ntpLastUpdate) >= ntpSyncTime) {
    ntpSyncDS1307();
  }
  // for reading keypad stroke to set the date and time once the RIGHT is pressed
  t = now();
  lcdKey = readLCDButton();
  if (lcdKey == btnRIGHT) {
    keypadSetDateTime();
  }
  // for LCD shield to disp date, day of the week, time and temperature once a second
  if (displayAtSecond != second(t)) { 
    digitalClockDisplay(); 
    displayAtSecond = second(t); 
  }
}

int readLCDButton() {
  adcKeyIn = analogRead(0);
  delay(200);
  // read the value from the sensor
  // my buttons when read are centered at these values: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adcKeyIn > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adcKeyIn < 73) return btnRIGHT;
  if (adcKeyIn < 237) return btnUP;
  if (adcKeyIn < 415) return btnDOWN;
  if (adcKeyIn < 623) return btnLEFT;
  if (adcKeyIn < 882) return btnSELECT;
  return btnNONE; // when all others fail, return this...
}

void digitalClockDisplay(){
  // digital clock display of the time
  dateDisplay();
  weekdayDisplay();
  timeDisplay();
  temperatureDisplay();  
}

void dateDisplay() {
  lcd.setCursor(0,0);
  lcd.print(year(t));
  lcd.print('-');
  if (month(t) < 10) {
    lcd.print('0');
  }
  lcd.print(month(t));
  lcd.print('-');
  if (day(t) < 10) {
    lcd.print('0');
  }
  lcd.print(day(t));
  // lcd.print(' ');
}

void weekdayDisplay() {
  lcd.setCursor(11,0);
  lcd.print(dayOfWeek[weekday()]);
}

void timeDisplay() {
  lcd.setCursor(0,1);
  if (hour(t) < 10) {
    lcd.print('0');
  }
  lcd.print(hour(t));
  lcd.print(':');
  if (minute(t) < 10) {
    lcd.print('0');
  }
  lcd.print(minute(t));
  lcd.print(':');
  if (second(t) < 10) {
    lcd.print('0');
  }
  lcd.print(second(t));
  // lcd.print(' ');
}

void temperatureDisplay() {
  int span = 10;
  long aRead = 0;
  unsigned long temp;
  
  for (int i=0;i<span;i++) {
    aRead = aRead + analogRead(potPin);
  }
  temperature = (aRead / span * 500.0 / 1024.0); // for other analog sensor change the constants
  printTenths(long (temperature * 10));
  lcd.setCursor(14,1);
  lcd.print(char(223));
  // lcd.setCursor(15,1);
  lcd.print('C'); 
}

void printTenths(long value) {
  // prints a value of 123 as 12.3
  lcd.setCursor(10,1);
  lcd.print(value / 10);
  lcd.setCursor(12,1);
  lcd.print('.');
  lcd.setCursor(13,1);
  lcd.print(value % 10);
}

void keypadSetDateTime() {
  int setYear=year(t), setMonth=month(t), setDay=day(t), setHour=hour(t), setMinute=minute(t), setSecond=second(t);
  int setVariable=0, checkStatus=0;
  
  dateDisplay();
  timeDisplay();
  while(true) {
    lcdKey = readLCDButton();
    switch(lcdKey) {
      case btnNONE:
      {
        lcd.blink();
        if (setVariable == 0) lcd.setCursor(3,0);
        if (setVariable == 1) lcd.setCursor(6,0);
        if (setVariable == 2) lcd.setCursor(9,0);
        if (setVariable == 3) lcd.setCursor(1,1);
        if (setVariable == 4) lcd.setCursor(4,1);
        if (setVariable == 5) lcd.setCursor(7,1);
        break;
      }
      case btnRIGHT:
      {
        setVariable = (setVariable + 1) % 6;
        break;
      }
      case btnLEFT:
      {
        lcd.noBlink();
        lcd.clear();
        return;
      }
      case btnUP:
      {
        if (setVariable == 0) {
          setYear = ((setYear + 1) % 100) + 2000;
          lcd.setCursor(0,0);
          lcd.print(setYear);
        }
        if (setVariable == 1) {
          setMonth = (setMonth % 12) + 1;
          lcd.setCursor(5,0);
          if (setMonth < 10) {
            lcd.print('0');
            lcd.print(setMonth);
          }
          else {
            lcd.print(setMonth);
          }
        }
        if (setVariable == 2) {
          setDay = (setDay % 31) + 1;
          lcd.setCursor(8,0);
          if (setDay < 10) {
            lcd.print('0');
            lcd.print(setDay);
          }
          else {
            lcd.print(setDay);
          }
        }
        if (setVariable == 3) {
          setHour = (setHour + 1) % 24;
          lcd.setCursor(0,1);
          if (setHour < 10) {
            lcd.print('0');
            lcd.print(setHour);
          }
          else {
            lcd.print(setHour);
          }
        }
        if (setVariable == 4) {
          setMinute = (setMinute + 1) % 60;
          lcd.setCursor(3,1);
          if (setMinute < 10) {
            lcd.print('0');
            lcd.print(setMinute);
          }
          else {
            lcd.print(setMinute);
          }
        }
        if (setVariable == 5) {
          setSecond = (setSecond + 1) % 60;
          lcd.setCursor(6,1);
          if (setSecond < 10) {
            lcd.print('0');
            lcd.print(setSecond);
          }
          else {
            lcd.print(setSecond);
          }
        }
        break;
      }
      case btnDOWN:
      {
        if (setVariable == 0) {
          setYear = ((setYear - 1) % 100) + 2000;
          lcd.setCursor(0,0);
          lcd.print(setYear);
        }
        if (setVariable == 1) {
          setMonth = ((setMonth - 1) % 12);
          if (setMonth == 0) {
            setMonth = setMonth + 12;
          }
          lcd.setCursor(5,0);
          if (setMonth < 10) {
            lcd.print('0');
            lcd.print(setMonth);
          }
          else {
            lcd.print(setMonth);
          }
        }
        if (setVariable == 2) {
          setDay = ((setDay - 1) % 31);
          if (setDay == 0) {
            setDay = setDay + 31;
          }
          lcd.setCursor(8,0);
          if (setDay < 10) {
            lcd.print('0');
            lcd.print(setDay);
          }
          else {
            lcd.print(setDay);
          }
        }
        if (setVariable == 3) {
          setHour = (setHour - 1 + 24) % 24;
          lcd.setCursor(0,1);
          if (setHour < 10) {
            lcd.print('0');
            lcd.print(setHour);
          }
          else {
            lcd.print(setHour);
          }
        }
        if (setVariable == 4) {
          setMinute = (setMinute - 1 + 60) % 60;
          lcd.setCursor(3,1);
          if (setMinute < 10) {
            lcd.print('0');
            lcd.print(setMinute);
          }
          else {
            lcd.print(setMinute);
          }
        }
        if (setVariable == 5) {
          setSecond = (setSecond - 1 + 60) % 60;
          lcd.setCursor(6,1);
          if (setSecond < 10) {
            lcd.print('0');
            lcd.print(setSecond);
          }
          else {
            lcd.print(setSecond);
          }
        }
        break;
      }
      case btnSELECT:
      {
        if ((setMonth == 1 || setMonth == 3 || setMonth == 5 || setMonth == 7 || setMonth == 8 || setMonth == 10 || setMonth == 12) && setDay <= 31) checkStatus = 1;
        if ((setMonth == 4 || setMonth == 6 || setMonth == 9 || setMonth == 11) && setDay <=30) checkStatus = 1;
        if ((setMonth == 2 && (setYear % 4) == 0) && setDay <= 29) checkStatus = 1;
        if ((setMonth == 2 && (setYear % 4) != 0) && setDay <= 28) checkStatus = 1;
        if (checkStatus) {
          setTime(setHour, setMinute, setSecond, setDay, setMonth, setYear);
          RTC.set(now());
        }
        break;
      }
    }
  }
}

void ntpSyncDS1307() {
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  // wait to see if a replay is available
  delay(1000);
  if (Udp.parsePacket()) {
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
    // the timstamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, extract the two words:
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900)
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800L;
    // substract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears + timeZoneOffset + syncLatency;
    setTime(epoch);
    RTC.set(epoch);
    ntpLastUpdate = now();
  }
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) {
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011; // LI, Version, Mode
  packetBuffer[1] = 0; // Stratum, or type of clodk
  packetBuffer[2] = 6; // Polling Interval
  packetBuffer[3] = 0xEC; // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12] = 49;
  packetBuffer[13] = 0x4E;
  packetBuffer[14] = 49;
  packetBuffer[15] = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123);
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

No comments:

Post a Comment