Skip to content

Instantly share code, notes, and snippets.

@markcheno
Forked from Tech500/NTP_System_Time.ino
Created February 27, 2023 22:43
Show Gist options
  • Select an option

  • Save markcheno/2c9c2c87cff735ba60d5e8e6b51a9427 to your computer and use it in GitHub Desktop.

Select an option

Save markcheno/2c9c2c87cff735ba60d5e8e6b51a9427 to your computer and use it in GitHub Desktop.

Revisions

  1. @Tech500 Tech500 revised this gist Jan 14, 2022. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions NTP_System_Time.ino
    Original file line number Diff line number Diff line change
    @@ -201,6 +201,19 @@ void loop()
    //Code to do something...

    }

    //DOW = 0 Sunday
    //DOW 0-6 HOUR 0-23 MINUTE 0-59 SECOND 0-59
    if((DOW == 0) && (HOUR == 0) && (MINUTE == 0) && (SECOND == 0)) //Event 4
    {

    delay(1000);

    Serial.println("Event 4 occurs every Sunday");

    //Code to do something...

    }

    getDateTime();

  2. @Tech500 Tech500 revised this gist Jan 14, 2022. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions NTP_System_Time.ino
    Original file line number Diff line number Diff line change
    @@ -54,6 +54,17 @@
    }
    //DOW = 0 Sunday
    //DOW 0-6 HOUR 0-23 MINUTE 0-59 SECOND 0-59
    if((DOW == 0) && (HOUR == 0) && (MINUTE == 0) && (SECOND == 0)) //Event 4
    {
    delay(1000);
    Serial.println("Event 4 occurs every Sunday");
    }
    "%" Arithmetic operator reference: https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/
    */
  3. @Tech500 Tech500 revised this gist Jan 14, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions NTP_System_Time.ino
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,8 @@
    Serial.println("This event occurs every 1 minute and 0 seconds");
    }
    "%" Arithmetic operator reference: https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/
    */

  4. @Tech500 Tech500 revised this gist Jan 14, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions NTP_System_Time.ino
    Original file line number Diff line number Diff line change
    @@ -59,8 +59,8 @@
    //****************************************************************************

    // Replace with your network details
    const char* ssid = "R2D2";
    const char* password = "sissy4357";
    const char* ssid = "yourSSID";
    const char* password = "yourPASSWORD";

    //setting the addresses
    IPAddress ip(10, 0, 0, 6);
  5. @Tech500 Tech500 created this gist Jan 14, 2022.
    218 changes: 218 additions & 0 deletions NTP_System_Time.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,218 @@

    // NTP_System_Time.ino
    //schufti --of ESP8266.com Forum provided source for NTP time.
    //Developer of code not given

    #include <WiFi.h>
    #include <WiFiUdp.h>
    #include <sys/time.h>
    #include <time.h>

    /*
    Found this reference on setting TZ: http://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
    Here are some example TZ values, including the appropriate Daylight Saving Time and its dates of applicability.
    In North American Eastern Standard Time (EST) and Eastern Daylight Time (EDT), the normal offset from UTC is 5 hours;
    since this is west of the prime meridian, the sign is positive. Summer time begins on March's second Sunday at
    2:00am, and ends on November's first Sunday at 2:00am.
    */

    #define TZ "EST+5EDT,M3.2.0/2,M11.1.0/2"

    //****************************************************************************
    /*
    Need an event scheduler? "if" conditional statements can be used to schedule events; place in void loop(), for example:
    getDateTime();
    if((HOUR % 1 == 0) && (MINUTE == 0) && (SECOND == 0)) //Event 1
    {
    delay(1000);//wait for next second
    Serial.println("This event occurs every hour on the hour.");
    }
    if((MINUTE % 15 == 0) && (SECOND == 0)) //Event 2
    {
    delay(1000); //wait for next second
    Serial.println("This event occurs every 15 minutes and 0 seconds")
    }
    if((MINUTE % 1== 0) && (SECOND == 0)) //Event 3
    {
    delay(1000); //wait for next second
    Serial.println("This event occurs every 1 minute and 0 seconds");
    }
    */

    //****************************************************************************

    // Replace with your network details
    const char* ssid = "R2D2";
    const char* password = "sissy4357";

    //setting the addresses
    IPAddress ip(10, 0, 0, 6);
    IPAddress gateway(10, 0, 0, 1);
    IPAddress subnet(255, 255, 255, 0);
    IPAddress primaryDNS(8, 8, 8, 8);
    IPAddress secondaryDNS(8, 8, 4, 4);

    WiFiClient client;

    ///Are we currently connected?
    boolean connected = false;

    WiFiUDP udp;
    // local port to listen for UDP packets
    //Settings pertain to NTP time servers
    const int udpPort = 1337;
    //NTP Time Servers
    const char * udpAddress1 = "us.pool.ntp.org";
    const char * udpAddress2 = "time.nist.gov";
    char incomingPacket[255];
    char replyPacket[] = "Hi there! Got the message :-)";

    int DOW, MONTH, DATE, YEAR, HOUR, MINUTE, SECOND;
    //String = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
    String weekDay;

    int lc = 0;
    time_t tnow;

    char strftime_buf[64];

    String dtStamp(strftime_buf);

    void setup(void)
    {
    Serial.begin(115200);

    while (!Serial);

    WiFi.persistent( false ); // for time saving

    // Connecting to local WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    WiFi.mode(WIFI_STA);
    WiFi.config(ip, gateway, subnet, primaryDNS, secondaryDNS);
    WiFi.begin(ssid, password);
    delay(10);

    while (WiFi.status() != WL_CONNECTED)
    {
    Serial.print(".");
    delay(1000);
    }

    Serial.println("\nWiFi connected");
    Serial.print("MAC: ");
    Serial.println(WiFi.macAddress());
    Serial.print("Server IP: ");
    Serial.println(WiFi.localIP());

    //set up TimeZone in local environment
    configTime(0, 0, udpAddress1, udpAddress2);
    setenv("TZ", "EST+5EDT,M3.2.0/2,M11.1.0/2", 3); // this sets TZ to Indianapolis, Indiana
    tzset();

    Serial.print("wait for first valid timestamp ");

    while (time(nullptr) < 100000ul)
    {
    Serial.print(".");
    delay(5000);
    }

    Serial.println(" time synced");
    Serial.println("");

    }

    void loop()
    {

    //udp only send data when connected
    if (connected)
    {
    //Send a packet
    udp.beginPacket(udpAddress1, udpPort);
    udp.printf("Seconds since boot: %u", millis() / 1000);
    udp.endPacket();
    }

    getDateTime();

    if((HOUR % 1 == 0) && (MINUTE == 0) && (SECOND == 0)) //Event 1
    {

    delay(1000);//wait for next second

    Serial.println("Event 1 occurs every hour on the hour.");

    //Code to do something...

    }

    if((MINUTE % 15 == 0) && (SECOND == 0)) //Event 2
    {

    delay(1000); //wait for next second

    Serial.println("Event 2 occurs every 15 minutes and 0 seconds");

    //Code to do something...

    }

    if((MINUTE % 1== 0) && (SECOND == 0)) //Event 3
    {

    delay(1000); //wait for next second

    Serial.println("Event 3 occurs every 1 minute and 0 seconds");

    //Code to do something...

    }

    getDateTime();

    Serial.println(dtStamp);

    delay(1000);

    }

    String getDateTime()
    {
    struct tm *ti;

    tnow = time(nullptr) + 1;
    ti = localtime(&tnow);
    DOW = ti->tm_wday;
    YEAR = ti->tm_year + 1900;
    MONTH = ti->tm_mon + 1;
    DATE = ti->tm_mday;
    HOUR = ti->tm_hour;
    MINUTE = ti->tm_min;
    SECOND = ti->tm_sec;

    strftime(strftime_buf, sizeof(strftime_buf), "%a , %m/%d/%Y , %H:%M:%S %Z", localtime(&tnow));
    dtStamp = strftime_buf;
    return (dtStamp);

    }