Skip to content

Commit 6b7134f

Browse files
authored
Merge pull request timmbogner#193 from aviateur17/timekeeping_202402
FDRS Support for timekeeping - 2024 version
2 parents 16a6ac4 + 216657b commit 6b7134f

File tree

49 files changed

+3363
-1393
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3363
-1393
lines changed

examples/0_MQTT_Gateway/fdrs_gateway_config.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@
4646
#define FDRS_DEBUG // Enable USB-Serial debugging
4747
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
4848

49+
// I2C - OLED or RTC
50+
#define I2C_SDA 4
51+
#define I2C_SCL 15
52+
4953
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
5054
///#define USE_OLED
5155
#define OLED_HEADER "FDRS"
5256
#define OLED_PAGE_SECS 30
53-
#define OLED_SDA 4
54-
#define OLED_SCL 15
5557
#define OLED_RST 16
5658

59+
// RTC - I2C
60+
// #define USE_RTC_DS3231
61+
// #define RTC_ADDR 0x57
62+
// #define USE_RTC_DS1307
63+
// #define RTC_ADDR 0x68
64+
5765
// UART data interface pins (ESP32 only)
5866
//#define RXD2 14
5967
//#define TXD2 15
@@ -78,5 +86,12 @@
7886
//#define MQTT_USER "Your MQTT Username"
7987
//#define MQTT_PASS "Your MQTT Password"
8088

81-
82-
89+
// NTP Time settings
90+
#define USDST
91+
// #define EUDST
92+
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
93+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
94+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
95+
#define TIME_FETCHNTP 60 // Time, in minutes, between fetching time from NTP server
96+
#define TIME_PRINTTIME 15 // Time, in minutes, between printing local time to debug
97+
#define TIME_SEND_INTERVAL 10 // Time, in minutes, between sending out time to remote devices

examples/1_UART_Gateway/fdrs_gateway_config.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@
4646
#define FDRS_DEBUG // Enable USB-Serial debugging
4747
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
4848

49+
// I2C - OLED or RTC
50+
#define I2C_SDA 4
51+
#define I2C_SCL 15
52+
4953
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
5054
///#define USE_OLED
5155
#define OLED_HEADER "FDRS"
5256
#define OLED_PAGE_SECS 30
53-
#define OLED_SDA 4
54-
#define OLED_SCL 15
5557
#define OLED_RST 16
5658

59+
// RTC - I2C
60+
// #define USE_RTC_DS3231
61+
// #define RTC_ADDR 0x57
62+
// #define USE_RTC_DS1307
63+
// #define RTC_ADDR 0x68
64+
5765
// UART data interface pins (ESP32 only)
5866
//#define RXD2 14
5967
//#define TXD2 15
@@ -78,5 +86,12 @@
7886
//#define MQTT_USER "Your MQTT Username"
7987
//#define MQTT_PASS "Your MQTT Password"
8088

81-
82-
89+
// NTP Time settings
90+
#define USDST
91+
// #define EUDST
92+
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
93+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
94+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
95+
#define TIME_FETCHNTP 60 // Time, in minutes, between fetching time from NTP server
96+
#define TIME_PRINTTIME 15 // Time, in minutes, between printing local time to debug
97+
#define TIME_SEND_INTERVAL 10 // Time, in minutes, between sending out time to remote devices

examples/2_ESPNOW_Repeater/fdrs_gateway_config.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@
4646
#define FDRS_DEBUG // Enable USB-Serial debugging
4747
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
4848

49+
// I2C - OLED or RTC
50+
#define I2C_SDA 4
51+
#define I2C_SCL 15
52+
4953
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
5054
///#define USE_OLED
5155
#define OLED_HEADER "FDRS"
5256
#define OLED_PAGE_SECS 30
53-
#define OLED_SDA 4
54-
#define OLED_SCL 15
5557
#define OLED_RST 16
5658

59+
// RTC - I2C
60+
// #define USE_RTC_DS3231
61+
// #define RTC_ADDR 0x57
62+
// #define USE_RTC_DS1307
63+
// #define RTC_ADDR 0x68
64+
5765
// UART data interface pins (ESP32 only)
5866
//#define RXD2 14
5967
//#define TXD2 15
@@ -78,5 +86,12 @@
7886
//#define MQTT_USER "Your MQTT Username"
7987
//#define MQTT_PASS "Your MQTT Password"
8088

81-
82-
89+
// NTP Time settings
90+
#define USDST
91+
// #define EUDST
92+
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
93+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
94+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
95+
#define TIME_FETCHNTP 60 // Time, in minutes, between fetching time from NTP server
96+
#define TIME_PRINTTIME 15 // Time, in minutes, between printing local time to debug
97+
#define TIME_SEND_INTERVAL 10 // Time, in minutes, between sending out time to remote devices

examples/3_LoRa_Repeater/fdrs_gateway_config.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@
4646
#define FDRS_DEBUG // Enable USB-Serial debugging
4747
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
4848

49+
// I2C - OLED or RTC
50+
#define I2C_SDA 4
51+
#define I2C_SCL 15
52+
4953
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
5054
///#define USE_OLED
5155
#define OLED_HEADER "FDRS"
5256
#define OLED_PAGE_SECS 30
53-
#define OLED_SDA 4
54-
#define OLED_SCL 15
5557
#define OLED_RST 16
5658

59+
// RTC - I2C
60+
// #define USE_RTC_DS3231
61+
// #define RTC_ADDR 0x57
62+
// #define USE_RTC_DS1307
63+
// #define RTC_ADDR 0x68
64+
5765
// UART data interface pins (ESP32 only)
5866
//#define RXD2 14
5967
//#define TXD2 15
@@ -78,5 +86,12 @@
7886
//#define MQTT_USER "Your MQTT Username"
7987
//#define MQTT_PASS "Your MQTT Password"
8088

81-
82-
89+
// NTP Time settings
90+
#define USDST
91+
// #define EUDST
92+
#define TIME_SERVER "0.us.pool.ntp.org" // NTP time server to use. If FQDN at least one DNS server is required to resolve name
93+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
94+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
95+
#define TIME_FETCHNTP 60 // Time, in minutes, between fetching time from NTP server
96+
#define TIME_PRINTTIME 15 // Time, in minutes, between printing local time to debug
97+
#define TIME_SEND_INTERVAL 10 // Time, in minutes, between sending out time to remote devices
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// FARM DATA RELAY SYSTEM
2+
//
3+
// Sensor Configuration
4+
5+
#include <fdrs_globals.h>
6+
7+
#define READING_ID 1 //Unique ID for this sensor
8+
#define GTWY_MAC 0x01 //Address of the nearest gateway
9+
10+
#define USE_ESPNOW
11+
//#define USE_LORA
12+
#define DEEP_SLEEP
13+
//#define POWER_CTRL 14
14+
#define FDRS_DEBUG
15+
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
16+
17+
// LoRa Configuration
18+
#define RADIOLIB_MODULE SX1276
19+
#define LORA_SS 18
20+
#define LORA_RST 14
21+
#define LORA_DIO 26
22+
#define LORA_BUSY 33
23+
//#define USE_SX126X
24+
25+
#define LORA_TXPWR 17 // LoRa TX power in dBm (: +2dBm - +17dBm (for SX1276-7) +20dBm (for SX1278))
26+
#define LORA_ACK // Request LoRa acknowledgment.
27+
28+
//#define USE_LR // Use ESP-NOW LR mode (ESP32 only)
29+
30+
// Time settings
31+
#define USDST
32+
// #define EUDST
33+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
34+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
35+
#define TIME_PRINTTIME 99 // Time, in minutes, between printing local time to debug

examples/Controller_examples/FastLED/fdrs_node_config.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,35 @@
1515
#define FDRS_DEBUG // Enable USB-Serial debugging
1616
//#define DBG_LEVEL 0 // 0 for minimal messaging, 1 for troubleshooting, 2 for development
1717

18+
// I2C - OLED or RTC
19+
#define I2C_SDA 5
20+
#define I2C_SCL 6
21+
22+
// OLED -- Displays console debugging messages on an SSD1306 I²C OLED
23+
// #define USE_OLED
24+
#define OLED_HEADER "FDRS"
25+
#define OLED_PAGE_SECS 30
26+
#define OLED_RST -1
27+
1828
// LoRa Configuration
1929
#define RADIOLIB_MODULE SX1276 //Tested on SX1276
2030
#define LORA_SS 18
2131
#define LORA_RST 14
2232
#define LORA_DIO 26
33+
#define LORA_BUSY 33
34+
//#define USE_SX126X
35+
36+
//#define CUSTOM_SPI
37+
#define LORA_SPI_SCK 5
38+
#define LORA_SPI_MISO 19
39+
#define LORA_SPI_MOSI 27
2340

2441
#define LORA_TXPWR 17 // LoRa TX power in dBm (: +2dBm - +17dBm (for SX1276-7) +20dBm (for SX1278))
2542
#define LORA_ACK // Request LoRa acknowledgment.
43+
44+
// Time settings
45+
#define USDST
46+
// #define EUDST
47+
#define STD_OFFSET (-6) // Local standard time offset in hours from UTC - if unsure, check https://time.is
48+
#define DST_OFFSET (STD_OFFSET + 1) // Local savings time offset in hours from UTC - if unsure, check https://time.is
49+
#define TIME_PRINTTIME 15 // Time, in minutes, between printing local time to debug

0 commit comments

Comments
 (0)