1+ // FARM DATA RELAY SYSTEM
2+ //
3+ // Example sketch to provide several examples to obtain and use time
4+ // from a gateway
5+ // This sketch retrieves the time from a nearby FDRS gateway using ESP-NOW.
6+ // After retrieving the time, at 60 second intervals, it prints out each field of the time
7+ // in two different methods.
8+ //
9+ // Developed by Timm Bogner (timmbogner@gmail.com) in Urbana, Illinois, USA
10+ // FDRS Timekeeping was developed and contributed by Jeff Lehman (aviateur17)
11+ //
12+ //
13+
14+ #include " fdrs_node_config.h"
15+ #include < fdrs_node.h>
16+
17+ unsigned long printJob = 0 ;
18+ struct tm timeData;
19+ char strftime_buf[80 ];
20+
21+ static char dayNames[][12 ] = { " Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" };
22+ static char monthNames[][12 ] = { " January" , " February" , " March" , " April" , " May" , " June" ,
23+ " July" , " August" , " September" , " October" , " November" , " December" };
24+ bool isPM = false ;
25+
26+ void setup () {
27+ DBG (" FDRS ControllerTime example." );
28+
29+ beginFDRS ();
30+
31+ // Request that the gateway send us the current time
32+ DBG (" Requesting time from gateway" );
33+ reqTimeFDRS ();
34+ delay (1000 );
35+ }
36+
37+ void loop () {
38+ loopFDRS ();
39+
40+ // If time is valid, prints time out every 60 seconds (60,000 ms)
41+ if (millis () - printJob > (1000 * 60 ) || printJob == 0 )
42+ {
43+ // Check to see that we have a valid time
44+ if (validTime ())
45+ {
46+ // time is valid
47+
48+ DBG (" " );
49+ DBG (" Configured Standard Time offset from UTC: " + String (STD_OFFSET) + " hours." );
50+ DBG (" Configured Daylight Savings Time offset from UTC: " + String (DST_OFFSET) + " hours." );
51+
52+ // Option 1 - use strftime function
53+ // Local time
54+ // time is stored in UTC so add offset to convert to local time
55+ time_t localt = time (NULL ) + (isDST?dstOffset:stdOffset);
56+ // convert from time_t format to struct tm format
57+ localtime_r (&localt, &timeData);
58+ // Store the date/time in a character array
59+ // https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm
60+ strftime (strftime_buf, sizeof (strftime_buf), " %c" , &timeData);
61+ // Print out the character array to display the local date/time
62+ DBG (" ---- strftime() function output ----" );
63+ DBG (" Local date/time is: " + String (strftime_buf) + (isDST?" DST" :" STD" ));
64+
65+ strftime (strftime_buf, sizeof (strftime_buf), " %Y" , &timeData);
66+ DBG (" Year: " + String (strftime_buf)); // %Y - year in 4 digits
67+ strftime (strftime_buf, sizeof (strftime_buf), " %m" , &timeData);
68+ DBG (" Month: " + String (strftime_buf)); // %m - Month as a decimal number
69+ strftime (strftime_buf, sizeof (strftime_buf), " %B" , &timeData);
70+ DBG (" Month Name: " + String (strftime_buf)); // %B - Month Full Name
71+ strftime (strftime_buf, sizeof (strftime_buf), " %d" , &timeData);
72+ DBG (" Day of Month: " + String (strftime_buf)); // %d - day of the month (1 - 31)
73+ strftime (strftime_buf, sizeof (strftime_buf), " %w" , &timeData);
74+ DBG (" Day of Week: " + String (strftime_buf)); // %w - Days since Sunday = 0
75+ strftime (strftime_buf, sizeof (strftime_buf), " %A" , &timeData);
76+ DBG (" Day Name: " + String (strftime_buf)); // %A - Full weekday name
77+ strftime (strftime_buf, sizeof (strftime_buf), " %j" , &timeData);
78+ DBG (" Day of Year: " + String (strftime_buf)); // %j - days since January 1st = 0
79+ strftime (strftime_buf, sizeof (strftime_buf), " %H" , &timeData);
80+ DBG (" Hour (24 hour format): " + String (strftime_buf)); // %H - Hours since mindnight in 24 hour format
81+ strftime (strftime_buf, sizeof (strftime_buf), " %I" , &timeData);
82+ DBG (" Hour (12 hour format): " + String (strftime_buf)); // %I - Hours since mindnight in 12 hour format
83+ strftime (strftime_buf, sizeof (strftime_buf), " %M" , &timeData);
84+ DBG (" Minute: " + String (strftime_buf)); // %M - Minutes
85+ strftime (strftime_buf, sizeof (strftime_buf), " %S" , &timeData);
86+ DBG (" Second: " + String (strftime_buf)); // %S - Seconds
87+ strftime (strftime_buf, sizeof (strftime_buf), " %p" , &timeData);
88+ DBG (" AM/PM: " + String (strftime_buf)); // %p - AM/PM
89+ DBG (" Daylight Savings: " + String ((isDST ? " DST (yes)" : " STD (no)" )));
90+ strftime (strftime_buf, sizeof (strftime_buf), " %A, %m/%d/%Y %H:%M:%S %p" , &timeData);
91+ DBG (" US format: " + String (strftime_buf) + String ((isDST ? " DST" : " STD" ))); // Day, MM/DD/YYYY HH:MM:SS AM/PM DST/STD
92+ strftime (strftime_buf, sizeof (strftime_buf), " %A, %d/%m/%Y %I:%M:%S" , &timeData);
93+ DBG (" European format: " + String (strftime_buf) + String ((isDST ? " DST" : " STD" ))); // Day, DD/MM/YYYY HH:MM:SS DST/STD
94+
95+
96+
97+ // Option 2 - read directly from the tm struct
98+ // Local time
99+ // time is stored in UTC so add offset to convert to local time
100+ localt = time (NULL ) + (isDST?dstOffset:stdOffset);
101+ // convert from time_t format to struct tm format
102+ localtime_r (&localt, &timeData);
103+ // Struct tm provides the date/time elements split out
104+ // https://www.tutorialspoint.com/c_standard_library/c_function_strftime.htm
105+ DBG (" ---- struct tm output ----" );
106+ DBG (" Year: " + String (timeData.tm_year + 1900 )); // tm_year returns number of years since 1900 so we add 1900
107+ DBG (" Month: " + String (timeData.tm_mon + 1 )); // Jan = 0 so add 1 to (1 - 12)
108+ DBG (" Month Name: " + String (monthNames[timeData.tm_mon ]));
109+ DBG (" Day of Month: " + String (timeData.tm_mday )); // day of the month (1 - 31)
110+ DBG (" Day of Week: " + String (timeData.tm_wday )); // Days since Sunday = 0
111+ DBG (" Day Name: " + String (dayNames[timeData.tm_wday ]));
112+ DBG (" Day of Year: " + String (timeData.tm_yday + 1 )); // days since January 1st = 0
113+ DBG (" Hour (24 hour format): " + String (timeData.tm_hour )); // Hours since mindnight in 24 hour format
114+ if (timeData.tm_hour >= 12 && timeData.tm_hour <= 23 )
115+ {
116+ isPM = true ;
117+ }
118+ else
119+ {
120+ isPM = false ;
121+ }
122+ if (timeData.tm_hour >= 13 )
123+ {
124+ timeData.tm_hour = timeData.tm_hour - 12 ;
125+ }
126+ DBG (" Hour (12 hour format): " + String (timeData.tm_hour ) + " " + String ((isPM ? " PM" : " AM" ))); // Hours since mindnight in 12 hour format
127+ DBG (" Minute: " + String (timeData.tm_min ));
128+ DBG (" Second: " + String (timeData.tm_sec ));
129+ DBG (" Daylight Savings: " + String ((isDST ? " DST (yes)" : " STD (no)" )));
130+ DBG (" Local Time: " + String (dayNames[timeData.tm_wday ]) + " , " + String (timeData.tm_mon + 1 ) + " /" + String (timeData.tm_mday ) + " /"
131+ + String (timeData.tm_year + 1900 ) + " " + String (timeData.tm_hour ) + " :" + String (timeData.tm_min ) + " :" + String (timeData.tm_sec )
132+ + String ((isPM ? " PM" : " AM" )) + String ((isDST ? " DST" : " STD" )));
133+ DBG (" " );
134+ DBG (" " );
135+
136+ printJob = millis ();
137+ }
138+ else // Time is not valid. Request time from the gateway
139+ {
140+ DBG (" Time not valid." );
141+ DBG (" Check gateway is running and has valid time." );
142+ DBG (" " );
143+ reqTimeFDRS ();
144+
145+ // Try again in 20 seconds
146+ printJob = printJob + 20000 ;
147+ }
148+ }
149+ }
0 commit comments