victor qiu / Mbed OS SimpleNTP

Fork of SimpleNTP by Akinori Hashimoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "EthernetInterface.h"
00004 #include "RealTimeClock.h"
00005 #include "SimpleNTP.h"
00006 
00007 EthernetInterface ether;
00008 RealTimeClock rtc;
00009 DigitalOut led[]= {LED1, LED2, LED3, LED4};
00010 
00011 void setTime()
00012 {
00013    ether.init();
00014    ether.connect();
00015    led[1]= 1;
00016    wait(2);
00017 
00018    SimpleNTP sntp;
00019    SimpleNTP::Result resultNTP;
00020    resultNTP= sntp.setNTPServer("ntp.sanoh.com");
00021    led[2]= 1;
00022    if(resultNTP == SimpleNTP::SUCCESS) {
00023        led[0]= 0;
00024        wait(2);
00025        long timeEpoch= sntp.getNetworkTime();
00026        if(rtc.setRealTime(timeEpoch))
00027            led[1]= 0;
00028    }
00029    if(sntp.close() == SimpleNTP::SUCCESS)
00030        led[2]= 0;
00031    return;
00032 }
00033 int main()
00034 {
00035    led[0]= 1;
00036    setTime();
00037    while(1) {
00038        led[3]= !led[3];
00039        wait(0.2);
00040    }
00041 }