Simple Network Time Protocol Client. This lib feature: Access to SNTP server, and get Epoch (Unix) time.

Embed: (wiki syntax)

« Back to documentation index

SimpleNTP Class Reference

SimpleNTP Class Reference

Simple Network Time Protocol Client. More...

#include <SimpleNTP.h>

Public Member Functions

Result setNTPServer (string _server, unsigned short _port=123)
 Setting for NTP/SNTP server.
long getNetworkTime ()
 Get NetworkTime Converted Epoch.
Result close ()
 close the socket

Detailed Description

Simple Network Time Protocol Client.

This lib feature: Access to SNTP server, and get Epoch (Unix) time.

When cording, I refered to: http://www.softech.co.jp/mm_140305_firm.htm http://www.venus.dti.ne.jp/~yoshi-o/NTP/NTP-SNTP_Format.html

#include "mbed.h"

#include "EthernetInterface.h"
#include "RealTimeClock.h"
#include "SimpleNTP.h"

EthernetInterface ether;
RealTimeClock rtc;
DigitalOut led[]= {LED1, LED2, LED3, LED4};

void setTime()
{
    ether.init();
    ether.connect();
    led[1]= 1;
    wait(2);

    SimpleNTP sntp;
    SimpleNTP::Result resultNTP;
    resultNTP= sntp.setNTPServer("ntp.sanoh.com");
    led[2]= 1;
    if(resultNTP == SimpleNTP::SUCCESS) {
        led[0]= 0;
        wait(2);
        long timeEpoch= sntp.getNetworkTime();
        if(rtc.setRealTime(timeEpoch))
            led[1]= 0;
    }
    if(sntp.close() == SimpleNTP::SUCCESS)
        led[2]= 0;
    return;
}
int main()
{
    led[0]= 1;
    setTime();
    while(1) {
        led[3]= !led[3];
        wait(0.2);
    }
}

Definition at line 62 of file SimpleNTP.h.


Member Function Documentation

SimpleNTP::Result close (  )

close the socket

Definition at line 50 of file SimpleNTP.cpp.

long getNetworkTime (  )

Get NetworkTime Converted Epoch.

Returns:
epoch(UNIX) time. NOT 1900/01/01~

Definition at line 18 of file SimpleNTP.cpp.

SimpleNTP::Result setNTPServer ( string  _server,
unsigned short  _port = 123 
)

Setting for NTP/SNTP server.

Parameters:
_server;IPv4 or URL(with DNS)
_port;port of sntp server.
Returns:
enum.

Definition at line 3 of file SimpleNTP.cpp.