Last active
January 2, 2023 20:13
-
-
Save Tech500/96b2fbc70bea5663c11c02bda90b3fce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" | |
// Replace with your network details | |
const char* ssid = "yourssid"; | |
const char* password = "yourpassword"; | |
//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); | |
int days = 0; | |
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... | |
} | |
//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(); | |
Serial.println(dtStamp); | |
delay(1000); | |
days = 0; | |
days = getNumberOfDays(MONTH, YEAR); | |
Serial.print("Month: "); | |
Serial.println(MONTH); | |
Serial.print("Number of Days : "); | |
Serial.println(days); | |
Serial.println(""); | |
} | |
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); | |
} | |
//function will return total number of days | |
int getNumberOfDays(int MONTH, int YEAR) | |
{ | |
//leap year condition, if month is 2 | |
if( MONTH == 2) | |
{ | |
if((YEAR%400==0) || (YEAR%4==0 && YEAR%100!=0)) | |
return 29; | |
else | |
return 28; | |
} | |
//months which has 31 days | |
else if(MONTH == 1 || MONTH == 3 || MONTH == 5 || MONTH == 7 || MONTH == 8 | |
||MONTH == 10 || MONTH==12) | |
return 31; | |
else | |
return 30; | |
} | |
/* | |
Function will return total number of days in a month. | |
C++ code for days in month from: https://www.includehelp.com/cpp-programs/find-total-number-of-days-in-given-month-of-year.aspx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment