GithubHelp home page GithubHelp logo

erriez / erriezntpclient Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 373 KB

NTP client library for Arduino to retrieve UNIX Epoch UTC time

Home Page: https://github.com/Erriez/ErriezArduinoLibraries

License: MIT License

C++ 100.00%
ntp client library arduino documentation examples avr esp8266 esp32 ethershield

erriezntpclient's Introduction

Erriez NTP Client library for Arduino

Build Status

This is a minimized NTP Client library for Arduino to retrieve UNIX Epoch UTC from NTP time servers.

Library features

  • Retrieve UNIX Epoch UTC time from network time servers
  • Compatible with time.h
  • Timeout handling

Supported hardware

  • Arduino UNO with EtherShield (Wiznet W5100 Ethernet controller)
  • ESP8266 WiFi
  • ESP32 WiFi

Documentation

Example output

Erriez ESP8266 NTP example
Connecting to 'wifi'...OK
Epoch: 1600025290
UTC:   Sun Sep 13 19:28:10 2020

Example ESP8266 / ESP32

#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif

#include <ErriezNTPClient.h>

// WiFi SSID and Password
#define WIFI_SSID           ""
#define WIFI_PASSWORD       ""

// "pool.ntp.org", "time.nist.gov" or NTP server IP address
#define NTP_SERVER          "pool.ntp.org"

ErriezNTPClient ntp(NTP_SERVER);


void setup()
{
    // Initialize serial
    delay(500);
    Serial.begin(115200);
    Serial.println(F("\nErriez NTP client ESP8266 / ESP32 example"));

    // Initialize WiFi
    Serial.print(F("Connecting to '"));
    Serial.print(WIFI_SSID);
    Serial.print(F("'"));

    // Connect to your WiFi router
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("OK");
}

void loop()
{
    time_t t;

    // Get epoch
    t = ntp.getEpoch();

    // Print result
    if (t > 0) {
        Serial.print(F("Epoch: "));
        Serial.println((uint32_t)t);
        Serial.print(F("UTC:   "));
        Serial.println(ctime(&t));
    } else {
        Serial.println(F("Timeout"));
    }

    delay(10000);
}

Example AVR (ATMega328 / ATMega2560)

#include <Ethernet.h>
#include <ErriezNTPClient.h>

// "pool.ntp.org", "time.nist.gov" or NTP server IP address
#define NTP_SERVER          "pool.ntp.org"

ErriezNTPClient ntp(NTP_SERVER);

// Newer Ethernet shields have a MAC address printed on a sticker on the shield
uint8_t mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};


void setup()
{
    // Initialize serial
    delay(500);
    Serial.begin(115200);
    Serial.println(F("\nErriez NTP client AVR example"));

    // Start Ethernet and UDP
    if (!Ethernet.begin(mac)) {
        Serial.println(F("Failed to configure Ethernet using DHCP"));

        // Check for Ethernet hardware present
        if (Ethernet.hardwareStatus() == EthernetNoHardware) {
            Serial.println(F("Ethernet shield was not found."));
        } else if (Ethernet.linkStatus() == LinkOFF) {
            Serial.println(F("Ethernet cable is not connected."));
        }
    }
}

void loop()
{
    time_t t;

    // Get epoch
    t = ntp.getEpoch();

    // Print result
    if (t > 0) {
        Serial.print(F("Epoch: "));
        Serial.println((uint32_t)t);

        // A UNIX offset is needed for AVR target
        t -= UNIX_OFFSET;

        Serial.print(F("UTC:   "));
        Serial.println(ctime(&t));
    } else {
        Serial.println(F("Timeout"));
    }

    delay(10000);
}

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and examples from Erriez

Erriez Libraries

erriezntpclient's People

Contributors

erriez avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

m4k3r-org

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.