├── examples ├── NodeMCU │ ├── .uno.test.skip │ └── NodeMCU.ino └── ArduinoEspWifiShield │ ├── .esp8266.test.skip │ └── ArduinoEspWifiShield.ino ├── library.properties ├── library.json ├── keywords.txt ├── .travis.yml ├── src ├── EasyNTPClient.h └── EasyNTPClient.cpp ├── LICENSE └── README.md /examples/NodeMCU/.uno.test.skip: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/ArduinoEspWifiShield/.esp8266.test.skip: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=EasyNTPClient 2 | version=1.1.0 3 | author=Harsha Alva 4 | maintainer=Harsha Alva 5 | sentence=Library to read time from Network Time Protocol (NTP) servers. 6 | paragraph=Handles the connection to an NTP pool and parses Internet Time to UNIX time format. 7 | category=Timing 8 | url=https://github.com/aharshac/EasyNTPClient 9 | architectures=* 10 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "EasyNTPClient", 3 | "keywords": "easy, udp, web, nodemcu, esp, esp8266, ntp, client, time", 4 | "description": "Library to read time from Network Time Protocol (NTP) servers", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/aharshac/EasyNTPClient" 8 | }, 9 | "version": "1.1.0", 10 | "authors": { 11 | "name": "Harsha Alva", 12 | "url": "https://alvaharsha.me" 13 | }, 14 | "frameworks": "arduino", 15 | "platforms": "espressif" 16 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For StringSplitter 3 | ####################################### 4 | # Class (KEYWORD1) 5 | ####################################### 6 | 7 | EasyNTPClient KEYWORD1 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | getTimeOffset KEYWORD2 14 | setTimeOffset KEYWORD2 15 | getUnixTime KEYWORD2 16 | 17 | ####################################### 18 | # Constants (LITERAL1) 19 | ####################################### 20 | 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | sudo: false 3 | before_install: 4 | - source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh) 5 | install: 6 | # - git clone https://github.com/bportaluri/WiFiEsp $HOME/arduino_ide/libraries/WiFiEsp 7 | - arduino --install-library "WiFiEsp" 8 | script: 9 | - build_platform esp8266 10 | # - build_platform uno 11 | # - arduino --install-library "WiFiEsp" 12 | # - arduino --install-library "ESP8266WiFi" 13 | # - arduino --verify --board arduino:avr:uno $PWD/examples/ArduinoEspWifiShield/ArduinoEspWifiShield.ino 14 | # - arduino --verify --board esp8266:esp8266:nodemcuv2 $PWD/examples/NodeMCU/NodeMCU.ino 15 | notifications: 16 | email: 17 | on_success: change 18 | on_failure: change -------------------------------------------------------------------------------- /src/EasyNTPClient.h: -------------------------------------------------------------------------------- 1 | /* 2 | EasyNTPClient - Arduino library to read time from Network Time Protocol (NTP) servers. 3 | Created by Harsha Alva, June 29, 2017. 4 | Released into the public domain. 5 | 6 | Based on work by: 7 | * Francesco Potortì, 2013 8 | * https://playground.arduino.cc/Code/NTPclient 9 | * 10 | * Sandeep Mistry, 2016 11 | * https://github.com/arduino-libraries/NTPClient 12 | */ 13 | 14 | #ifndef EasyNTPClient_h 15 | #define EasyNTPClient_h 16 | 17 | #include "Arduino.h" 18 | #include 19 | 20 | class EasyNTPClient 21 | { 22 | public: 23 | EasyNTPClient(UDP &udp); 24 | EasyNTPClient(UDP& udp, const char* serverPool); 25 | EasyNTPClient(UDP& udp, const char* serverPool, int offset); 26 | int getTimeOffset(); 27 | void setTimeOffset(int offset); 28 | unsigned long getUnixTime(); 29 | 30 | private: 31 | UDP *mUdp; 32 | const char* mServerPool = "pool.ntp.org"; 33 | int mOffset = 0; 34 | unsigned int mUpdateInterval = 60000; 35 | unsigned long mLastUpdate = 0; 36 | long mServerTime = 0; 37 | unsigned long getServerTime(); 38 | }; 39 | 40 | #endif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Harsha Alva 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/NodeMCU/NodeMCU.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | EasyNTPClient example: NodeMCU 4 | 5 | This example shows the basic usage of the EasyNTPClient on a NodeMCU (ESP8266). 6 | The output is visible in the Serial Monitor at 9600 baud rate. 7 | 8 | For more details see: https://github.com/aharshac/EasyNTPClient 9 | 10 | */ 11 | 12 | /* 13 | * 14 | * Board/shield platform 15 | * 16 | * Arduino WiFi shield: #include 17 | * 18 | * WiFi 101 shield or MKR1000: #include 19 | * 20 | * ESP8266/NodeMCU: #include 21 | * 22 | */ 23 | 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | 30 | const char *ssid = ""; 31 | const char *password = ""; 32 | 33 | WiFiUDP udp; 34 | 35 | EasyNTPClient ntpClient(udp, "pool.ntp.org", ((5*60*60)+(30*60))); // IST = GMT + 5:30 36 | 37 | void setup(){ 38 | Serial.begin(9600); 39 | WiFi.begin(ssid, password); 40 | 41 | while (WiFi.status() != WL_CONNECTED) { 42 | delay(500); 43 | Serial.print("."); 44 | } 45 | } 46 | 47 | void loop() { 48 | Serial.println(ntpClient.getUnixTime()); 49 | 50 | delay(20000); // wait for 20 seconds before refreshing. 51 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EasyNTPClient 2 | 3 | [![Collaborizm](https://img.shields.io/badge/Collaborizm-Join%20now-blue.svg)](https://www.collaborizm.com/) 4 | [![Build Status](https://travis-ci.org/aharshac/EasyNTPClient.svg?branch=master)](https://travis-ci.org/aharshac/EasyNTPClient) 5 | 6 | Arduino library to read time from Network Time Protocol (NTP) servers. 7 | 8 |   9 | 10 | ## Features 11 | - Handles all heavy lifting involved with managing connections to and parsing time from an NTP server. 12 | - As easy as providing a **UDP** object to the constructor during initialization. 13 | - Works on **Arduino** and **ESP8266**. 14 | 15 |   16 | 17 | ## Examples 18 | 1. **NodeMCU** 19 | Using EasyNTPClient on a NodeMCU (ESP8266) 20 | 21 | 2. **ArduinoEspWifiShield** 22 | Using EasyNTPClient on an Arduino UNO with an ESP-01 (ESP8266) WiFi module. 23 | By [**Claran Martis**](https://www.collaborizm.com/profile/SJne7FcMg) 24 | 25 |   26 | 27 | ## Reference 28 | ### Class **EasyNTPClient** 29 | #### 1. Initialization #### 30 | 1. No frills 31 | ```c 32 | EasyNTPClient(UDP &udp) 33 | 34 | Parameters: 35 | udp: Reference to an UDP object. 36 | Returns: 37 | EasyNTPClient object. 38 | ``` 39 | 40 | 2. Custom server pool 41 | ```c 42 | EasyNTPClient(UDP& udp, const char* serverPool) 43 | 44 | Parameters: 45 | udp: Reference to an UDP object. 46 | serverPool: NTP server pool. Default = "pool.ntp.org" 47 | Returns: 48 | EasyNTPClient object. 49 | ``` 50 | 51 | 3. Time offset 52 | ```c 53 | EasyNTPClient(UDP& udp, const char* serverPool, int offset); 54 | 55 | Parameters: 56 | udp: Reference to an UDP object. 57 | serverPool: NTP server pool domain name. Default = "pool.ntp.org" 58 | offset: Difference from UTC in seconds. Default = 0 59 | Returns: 60 | EasyNTPClient object. 61 | ``` 62 | 63 | #### 2. Methods ### 64 | 1. Get time offset 65 | ```c 66 | int getTimeOffset() 67 | 68 | Returns: 69 | EasyNTPClient object. 70 | ``` 71 | 72 | 2. Set time offset 73 | ```c 74 | void setTimeOffset(int offset); 75 | 76 | Parameters: 77 | offset: Difference from UTC in seconds. 78 | ``` 79 | 80 | 3. Get time in UNIX format 81 | ```c 82 | unsigned long getUnixTime(); 83 | 84 | Returns: 85 | UTC time in UNIX time format (seconds) 86 | ``` -------------------------------------------------------------------------------- /examples/ArduinoEspWifiShield/ArduinoEspWifiShield.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | EasyNTPClient example: ArduinoEspWifiShield 4 | 5 | This example shows the basic usage of the EasyNTPClient on an Arduino UNO with an ESP-01 (ESP8266) WiFi module. 6 | The output is visible in the Serial Monitor at 9600 baud rate. 7 | 8 | For more details see: https://github.com/aharshac/EasyNTPClient 9 | 10 | An example by Claran Martis 11 | https://www.collaborizm.com/profile/SJne7FcMg 12 | 13 | */ 14 | 15 | 16 | /* 17 | 18 | Pin Connectiions 19 | 20 | +--------+-----------------------------+ 21 | | ESP-01 | Connection | 22 | +--------+-----------------------------+ 23 | | TXD | Arduino D3 | 24 | +--------+-----------------------------+ 25 | | CH_PD | External Power Supply +3.3V | 26 | +--------+-----------------------------+ 27 | | RST | Arduino Reset | 28 | +--------+-----------------------------+ 29 | | VCC | External Power Supply +3.3V | 30 | +--------+-----------------------------+ 31 | | RXD | Arduino D2 | 32 | +--------+-----------------------------+ 33 | | GPIO0 | {None} | 34 | +--------+-----------------------------+ 35 | | GPIO2 | {None} | 36 | +--------+-----------------------------+ 37 | | GND | Common GND | 38 | +--------+-----------------------------+ 39 | 40 | */ 41 | 42 | #include 43 | 44 | #include 45 | #include 46 | #include 47 | #include 48 | 49 | #include 50 | 51 | 52 | char ssid[] = "ssid"; // your network SSID (name) 53 | char password[] = "password"; // your network password 54 | 55 | const int pinEspRx = 2; // Esp Rx <----> Arduino Tx 56 | const int pinEspTx = 3; // Esp Tx <----> Arduino Rx 57 | SoftwareSerial ESP8266(pinEspTx, pinEspRx); 58 | 59 | WiFiEspUDP udp; 60 | EasyNTPClient ntpClient(udp, "pool.ntp.org", ((5*60*60)+(30*60))); // IST = GMT + 5:30 61 | 62 | void setup(){ 63 | Serial.begin(9600); 64 | ESP8266.begin(115200); 65 | WiFi.init(&ESP8266); 66 | WiFi.begin(ssid, password); 67 | 68 | while (WiFi.status() != WL_CONNECTED) { 69 | delay(500); 70 | Serial.print("."); 71 | } 72 | udp.begin(123); 73 | } 74 | 75 | void loop() { 76 | Serial.println(ntpClient.getUnixTime()); 77 | 78 | delay(20000); // wait for 20 seconds before refreshing. 79 | } -------------------------------------------------------------------------------- /src/EasyNTPClient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | EasyNTPClient - Arduino library to read time from Network Time Protocol (NTP) servers. 3 | Created by Harsha Alva, June 29, 2017. 4 | Released into the public domain. 5 | 6 | Based on work by: 7 | * Francesco Potortì, 2013 8 | * https://playground.arduino.cc/Code/NTPclient 9 | * 10 | * Sandeep Mistry, 2016 11 | * https://github.com/arduino-libraries/NTPClient 12 | */ 13 | 14 | // #pragma once 15 | #include "Arduino.h" 16 | #include "EasyNTPClient.h" 17 | 18 | EasyNTPClient::EasyNTPClient (UDP &udp) { 19 | this->mUdp = &udp; 20 | } 21 | 22 | EasyNTPClient::EasyNTPClient (UDP &udp, const char* serverPool) { 23 | this->mUdp = &udp; 24 | this->mServerPool = serverPool; 25 | } 26 | 27 | EasyNTPClient::EasyNTPClient (UDP &udp, const char* serverPool, int offset) { 28 | this->mUdp = &udp; 29 | this->mServerPool = serverPool; 30 | this->mOffset = offset; 31 | } 32 | 33 | int EasyNTPClient::getTimeOffset() { 34 | return this->mOffset; 35 | } 36 | 37 | void EasyNTPClient::setTimeOffset (int offset) { 38 | this->mOffset = offset; 39 | } 40 | 41 | 42 | unsigned long EasyNTPClient::getServerTime () { 43 | static int udpInited = this->mUdp->begin(123); // open socket on arbitrary port 44 | // Only the first four bytes of an outgoing NTP packet need to be set 45 | // appropriately, the rest can be whatever. 46 | const long ntpFirstFourBytes = 0xEC0600E3; // NTP request header 47 | 48 | // Fail if WiFiUdp.begin() could not init a socket 49 | if (! udpInited) 50 | return 0; 51 | 52 | // Clear received data from possible stray received packets 53 | this->mUdp->flush(); 54 | 55 | // Send an NTP request 56 | if (! (this->mUdp->beginPacket(this->mServerPool, 123) // 123 is the NTP port 57 | && this->mUdp->write((byte *)&ntpFirstFourBytes, 48) == 48 58 | && this->mUdp->endPacket())) 59 | return 0; // sending request failed 60 | 61 | // Wait for response; check every pollIntv ms up to maxPoll times 62 | const int pollIntv = 150; // poll every this many ms 63 | const byte maxPoll = 15; // poll up to this many times 64 | int pktLen; // received packet length 65 | for (byte i=0; imUdp->parsePacket()) == 48) 67 | break; 68 | delay(pollIntv); 69 | } 70 | if (pktLen != 48) 71 | return 0; // no correct packet received 72 | 73 | // Read and discard the first useless bytes 74 | // Set useless to 32 for speed; set to 40 for accuracy. 75 | const byte useless = 40; 76 | for (byte i = 0; i < useless; ++i) 77 | this->mUdp->read(); 78 | 79 | // Read the integer part of sending time 80 | unsigned long time = this->mUdp->read(); // NTP time 81 | for (byte i = 1; i < 4; i++) 82 | time = time << 8 | this->mUdp->read(); 83 | 84 | // Round to the nearest second if we want accuracy 85 | // The fractionary part is the next byte divided by 256: if it is 86 | // greater than 500ms we round to the next second; we also account 87 | // for an assumed network delay of 50ms, and (0.5-0.05)*256=115; 88 | // additionally, we account for how much we delayed reading the packet 89 | // since its arrival, which we assume on average to be pollIntv/2. 90 | time += (this->mUdp->read() > 115 - pollIntv/8); 91 | 92 | // Discard the rest of the packet 93 | this->mUdp->flush(); 94 | 95 | return time + this->mOffset - 2208988800ul; // convert NTP time to Unix time 96 | } 97 | 98 | unsigned long EasyNTPClient::getUnixTime() { 99 | // if (this->mServerTime < 0) { 100 | unsigned long delta = millis() - this->mLastUpdate; 101 | if (this->mServerTime <= 0 || this->mLastUpdate == 0 || delta >= this->mUpdateInterval) { 102 | this->mServerTime = this->getServerTime(); 103 | this->mLastUpdate = millis(); 104 | } 105 | return this->mServerTime + ((millis() - this->mLastUpdate) / 1000); 106 | } 107 | --------------------------------------------------------------------------------