Internet Clock

Purpose:
Design a clock to automatically sync with NTP server time.windows.com. Make where timezone can be changed. Timezone is a hardcoded variable. I set up using EST time. Also I added timer to clock.

Components:
Push Buttons
mbed chip
solderless breaboard
LCD Display
Ethernet Connection

 

 

Code:

 

#include "mbed.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "TextLCD.h"
#include "string.h"
#include "stdlib.h"

EthernetNetIf eth;
NTPClient ntp;
TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d0-d3
InterruptIn switch1(p17);
InterruptIn switch2(p18);
DigitalOut led(LED1);

time_t ctTime;
int start = 0;
int dh = 0;
int h = 0;
int m = 0;
int s = 0;
int start_time_h =0;
int start_time_m =0;
int start_time_s = 0;
int old_time_h = 0;
int old_time_m = 0;
int old_time_s =0;
int stop_time_h =0;
int stop_time_m = 0;
int stop_time_s = 0;
int timer_h = 0;
int timer_m =0;
int timer_s =0;
int time_diff =0;
char buffer [80];
char hour [2];
char mins [4];
char sec [6];
struct tm * current_time;

void start_stop() {
    if (start == 1){
        start = 0;
        }
    else
        start = 1;
        start_time_h = h;
        start_time_m = m;
        start_time_s = s;
}

void reset() {
    timer_h = 0;
    timer_m = 0;
    timer_s = 0;
    old_time_h = 0;
    old_time_m = 0;
    old_time_s = 0;
}

int main() {

    switch1.rise(&start_stop);
    switch2.rise(&reset);
// Setup NTP connection
    lcd.printf("Setting up...\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    lcd.printf("Setup OK\r\n");


//EST
    time_diff = -4;

    //connect to NTP pool server
    Host server(IpAddr(), 123, "0.north-america.pool.ntp.org");
    ntp.setTime(server);

    while (1) {
      
        ctTime = time(NULL);
        current_time =  localtime(&ctTime);
        //change due to daylight saving time
        
        strcpy (hour,"");
        strftime (hour,2,"%H",current_time);
        h = atoi(hour);
        
         strcpy (mins,"");
         strftime(mins,4,"%M",current_time);
         m = atoi(mins);
         
         strcpy (sec,"");
        strftime (sec,6,"%S",current_time);
        s = atoi(sec);
      
      //Timer
        if (start == 1) {
            stop_time_s = s - start_time_s;
            if (s == 0)
               old_time_s += 1;
        }
        //if timer is reset
        else{
         old_time_s += stop_time_s;
         stop_time_s = 0;
        }
        //add to actual timer 
        timer_h = stop_time_h + old_time_h;
        timer_m = stop_time_m + old_time_m;
        timer_s = stop_time_s + old_time_s;
        
        //reset count if timer and add 
         if (timer_s == 59){
            old_time_m += 1;
            old_time_s = 0;
            start_time_s = s;
            }
        if (timer_m == 59){
            old_time_h += 1;
            old_time_m = 0;
            start_time_m = m;
            }
       //reset start timer to 0 if actual time is 0
        if (s == 59){
            start_time_s = 0;
            old_time_s += stop_time_s; 
        }
         if (m == 59){
            start_time_m = 0;
            old_time_m += stop_time_m;
        }

        //timezone change
        dh = h;

        if (dh > 12 - time_diff)
            dh+=-12 + time_diff;
        else if (h <= -time_diff)
            dh += 12 + time_diff;
        else
            dh += time_diff;

        //rest of clock
        strftime (buffer,80,":%M:%S",current_time);
        //Print
        lcd.printf("UTC%d:%d%s   ",time_diff,dh,buffer);
        lcd.printf("TIMER:%d:%d:%d \n",timer_h,timer_m,timer_s);
        wait(1);
    }
}

 

Resources:
http://mbed.org/cookbook/Text-LCD
http://mbed.org/cookbook/NTP-Client


1 comment

07 May 2011

This may be a silly question. I've written many programs for embedded controllers but never needed anything but standard libraries. I have never worked with Ethernet, so I'm trying to find out more about it by first playing with cloks. The program above states that the clock "automatically sync with NTP server". Does it mean it will sync once when it's powered up or will it continue to sync at some interval? If not, how do I make it sync, say, every 12 hours?  I tried a few things but always ended up with the code in never never land. Where do I find details of how the Ethernet libraries work other than trying to figure out the source code? I find the mbed server a bit frustrating when trying to find data as a novice in this particular area while, reasonably competent, I would contend, C++ programmer.

Thanks for any guidance.

George 

You need to log in to post a comment