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

Committer:
AkinoriHashimoto
Date:
Fri Nov 27 02:10:36 2015 +0000
Revision:
1:b5a2e4532331
Parent:
0:2be905de8e28
Add socketClose.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:2be905de8e28 1 #pragma once
AkinoriHashimoto 0:2be905de8e28 2
AkinoriHashimoto 0:2be905de8e28 3
AkinoriHashimoto 0:2be905de8e28 4 /** Simple Network Time Protocol Client.
AkinoriHashimoto 0:2be905de8e28 5 This lib feature: Access to SNTP server, and get Epoch (Unix) time.
AkinoriHashimoto 0:2be905de8e28 6
AkinoriHashimoto 0:2be905de8e28 7 When cording, I refered to:
AkinoriHashimoto 0:2be905de8e28 8 http://www.softech.co.jp/mm_140305_firm.htm
AkinoriHashimoto 0:2be905de8e28 9 http://www.venus.dti.ne.jp/~yoshi-o/NTP/NTP-SNTP_Format.html
AkinoriHashimoto 1:b5a2e4532331 10
AkinoriHashimoto 0:2be905de8e28 11 @code
AkinoriHashimoto 0:2be905de8e28 12 #include "mbed.h"
AkinoriHashimoto 0:2be905de8e28 13
AkinoriHashimoto 0:2be905de8e28 14 #include "EthernetInterface.h"
AkinoriHashimoto 0:2be905de8e28 15 #include "RealTimeClock.h"
AkinoriHashimoto 0:2be905de8e28 16 #include "SimpleNTP.h"
AkinoriHashimoto 0:2be905de8e28 17
AkinoriHashimoto 0:2be905de8e28 18 EthernetInterface ether;
AkinoriHashimoto 0:2be905de8e28 19 RealTimeClock rtc;
AkinoriHashimoto 0:2be905de8e28 20 DigitalOut led[]= {LED1, LED2, LED3, LED4};
AkinoriHashimoto 0:2be905de8e28 21
AkinoriHashimoto 0:2be905de8e28 22 void setTime()
AkinoriHashimoto 0:2be905de8e28 23 {
AkinoriHashimoto 0:2be905de8e28 24 ether.init();
AkinoriHashimoto 0:2be905de8e28 25 ether.connect();
AkinoriHashimoto 0:2be905de8e28 26 led[1]= 1;
AkinoriHashimoto 0:2be905de8e28 27 wait(2);
AkinoriHashimoto 0:2be905de8e28 28
AkinoriHashimoto 0:2be905de8e28 29 SimpleNTP sntp;
AkinoriHashimoto 0:2be905de8e28 30 SimpleNTP::Result resultNTP;
AkinoriHashimoto 0:2be905de8e28 31 resultNTP= sntp.setNTPServer("ntp.sanoh.com");
AkinoriHashimoto 0:2be905de8e28 32 led[2]= 1;
AkinoriHashimoto 0:2be905de8e28 33 if(resultNTP == SimpleNTP::SUCCESS) {
AkinoriHashimoto 0:2be905de8e28 34 led[0]= 0;
AkinoriHashimoto 0:2be905de8e28 35 wait(2);
AkinoriHashimoto 0:2be905de8e28 36 long timeEpoch= sntp.getNetworkTime();
AkinoriHashimoto 0:2be905de8e28 37 if(rtc.setRealTime(timeEpoch))
AkinoriHashimoto 0:2be905de8e28 38 led[1]= 0;
AkinoriHashimoto 0:2be905de8e28 39 }
AkinoriHashimoto 1:b5a2e4532331 40 if(sntp.close() == SimpleNTP::SUCCESS)
AkinoriHashimoto 1:b5a2e4532331 41 led[2]= 0;
AkinoriHashimoto 0:2be905de8e28 42 return;
AkinoriHashimoto 0:2be905de8e28 43 }
AkinoriHashimoto 0:2be905de8e28 44 int main()
AkinoriHashimoto 0:2be905de8e28 45 {
AkinoriHashimoto 0:2be905de8e28 46 led[0]= 1;
AkinoriHashimoto 0:2be905de8e28 47 setTime();
AkinoriHashimoto 0:2be905de8e28 48 while(1) {
AkinoriHashimoto 0:2be905de8e28 49 led[3]= !led[3];
AkinoriHashimoto 0:2be905de8e28 50 wait(0.2);
AkinoriHashimoto 0:2be905de8e28 51 }
AkinoriHashimoto 0:2be905de8e28 52 }
AkinoriHashimoto 0:2be905de8e28 53 @endcode
AkinoriHashimoto 0:2be905de8e28 54 */
AkinoriHashimoto 0:2be905de8e28 55
AkinoriHashimoto 0:2be905de8e28 56
AkinoriHashimoto 0:2be905de8e28 57 #include "mbed.h"
AkinoriHashimoto 0:2be905de8e28 58 #include "UDPSocket.h"
AkinoriHashimoto 0:2be905de8e28 59 #include <string>
AkinoriHashimoto 0:2be905de8e28 60
AkinoriHashimoto 0:2be905de8e28 61
AkinoriHashimoto 0:2be905de8e28 62 class SimpleNTP
AkinoriHashimoto 0:2be905de8e28 63 {
AkinoriHashimoto 0:2be905de8e28 64 public:
AkinoriHashimoto 0:2be905de8e28 65 enum Result {
AkinoriHashimoto 0:2be905de8e28 66 SUCCESS,
AkinoriHashimoto 1:b5a2e4532331 67 ERR_SocketInit, ERR_SetNTPAddr,
AkinoriHashimoto 1:b5a2e4532331 68 ERR_SocketClose
AkinoriHashimoto 0:2be905de8e28 69 };
AkinoriHashimoto 0:2be905de8e28 70
AkinoriHashimoto 0:2be905de8e28 71 /** Setting for NTP/SNTP server.
AkinoriHashimoto 0:2be905de8e28 72 @param _server; IPv4 or URL(with DNS)
AkinoriHashimoto 0:2be905de8e28 73 @param _port; port of sntp server.
AkinoriHashimoto 0:2be905de8e28 74 @return enum.
AkinoriHashimoto 0:2be905de8e28 75 */
AkinoriHashimoto 0:2be905de8e28 76 Result setNTPServer(string _server, unsigned short _port= 123);
AkinoriHashimoto 1:b5a2e4532331 77
AkinoriHashimoto 0:2be905de8e28 78 /** Get NetworkTime Converted Epoch.
AkinoriHashimoto 0:2be905de8e28 79 * @return epoch(UNIX) time. NOT 1900/01/01~
AkinoriHashimoto 0:2be905de8e28 80 */
AkinoriHashimoto 0:2be905de8e28 81 long getNetworkTime(); // mbed is 32bit epoch. NOT "UNSIGNED long".
AkinoriHashimoto 0:2be905de8e28 82
AkinoriHashimoto 1:b5a2e4532331 83 /** close the socket
AkinoriHashimoto 1:b5a2e4532331 84 */
AkinoriHashimoto 1:b5a2e4532331 85 Result close();
AkinoriHashimoto 1:b5a2e4532331 86
AkinoriHashimoto 0:2be905de8e28 87 private:
AkinoriHashimoto 0:2be905de8e28 88 string server;
AkinoriHashimoto 0:2be905de8e28 89 unsigned short port;
AkinoriHashimoto 1:b5a2e4532331 90
AkinoriHashimoto 0:2be905de8e28 91 UDPSocket socket;
AkinoriHashimoto 0:2be905de8e28 92 Endpoint serverNTP;
AkinoriHashimoto 0:2be905de8e28 93
AkinoriHashimoto 0:2be905de8e28 94 };