1
- /* Copyright (C) 2019-2020 Lee C. Bussy (@LBussy)
1
+ /* Copyright (C) 2019-2021 Lee C. Bussy (@LBussy)
2
2
3
3
This file is part of Lee Bussy's Brew Bubbles (brew-bubbles).
4
4
@@ -20,34 +20,56 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
SOFTWARE. */
22
22
23
+ // NTP Examples in:
24
+ // https://github.com/esp8266/Arduino/blob/master/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
25
+
23
26
#include " ntp.h"
24
27
28
+ #if LWIP_VERSION_MAJOR == 1
29
+ #warning "Remember: You are using lwIP v1.x and this causes ntp time to act weird."
30
+ #endif
31
+
32
+ static const int __attribute__ ((unused)) SECS_1_1_2019 = 1546300800; // 1546300800 = 01/01/2019 @ 12:00am (UTC)
33
+ static const char __attribute__ ((unused)) * DAY_OF_WEEK[] = {" Sunday" , " Monday" , " Tuesday" , " Wednesday" , " Thursday" , " Friday" , " Saturday" };
34
+ static const char __attribute__ ((unused)) * DAY_OF_WEEK_SHORT[] = {" Sun" , " Mon" , " Tue" , " Wed" , " Thu" , " Fri" , " Sat" };
35
+
25
36
void setClock ()
26
37
{
27
38
Ticker blinker;
28
39
Log.notice (F (" Entering blocking loop to get NTP time." ));
29
40
blinker.attach_ms (NTPBLINK, ntpBlinker);
30
- time_t startSecs = time ( nullptr ) ;
41
+ unsigned long startSecs = millis () / 1000 ;
31
42
int cycle = 0 ;
32
- while (time (nullptr ) < EPOCH_1_1_2019)
43
+ #if LWIP_VERSION_MAJOR == 1
44
+ sntp_setservername (0 , (char *)" pool.ntp.org" );
45
+ sntp_setservername (0 , (char *)" time.nist.gov" );
46
+ sntp_set_timezone (0 );
47
+ while (sntp_get_current_timestamp () == 0 )
48
+ {
49
+ sntp_stop ();
50
+ sntp_init ();
51
+ #else
52
+ while (time (nullptr ) < SECS_1_1_2019)
33
53
{
34
- configTime (" GMT" , " pool.ntp.org" , " time.nist.gov" );
35
- if (time (nullptr ) - startSecs > 9 )
54
+ configTime (THISTZ, TIMESERVER);
55
+ #endif
56
+ if ((millis () / 1000 ) - startSecs > 9 )
36
57
{
37
58
if (cycle > 9 )
38
59
{
39
60
#ifdef LOG_LEVEL
40
61
myPrintln ();
41
62
#endif
42
- Log.warning (F (" Unable to get time hack from %s, starting with epoch ." CR), TIMESERVER );
63
+ Log.warning (F (" Unable to get time hack from server, restarting ." CR));
43
64
blinker.detach ();
65
+ ESP.restart ();
44
66
return ;
45
67
}
46
68
#ifdef LOG_LEVEL
47
69
myPrintln ();
48
70
#endif
49
71
Log.verbose (F (" Re-requesting time hack." ));
50
- startSecs = time ( nullptr ) ;
72
+ startSecs = millis () / 1000 ;
51
73
cycle++;
52
74
}
53
75
#ifdef LOG_LEVEL
@@ -60,18 +82,13 @@ void setClock()
60
82
#ifdef LOG_LEVEL
61
83
myPrintln ();
62
84
#endif
63
- lastNTPUpdate = millis ();
64
85
Log.notice (F (" NTP time set." CR));
65
- struct tm timeinfo;
66
- time_t nowSecs = time (nullptr );
67
- gmtime_r (&nowSecs, &timeinfo);
68
86
}
69
87
70
88
String getDTS ()
71
89
{
72
90
// Returns JSON-type string = 2019-12-20T13:59:39Z
73
- // / Also:
74
- // sprintf(dts, "%04u-%02u-%02uT%02u:%02u:%02uZ", getYear(), getMonth(), getDate(), getHour(), getMinute(), getSecond());
91
+ // Also: sprintf(dta, "%04u-%02u-%02uT%02u:%02u:%02uZ", getYear(), getMonth(), getDate(), getHour(), getMinute(), getSecond());
75
92
time_t now;
76
93
time_t rawtime = time (&now);
77
94
struct tm ts;
@@ -100,7 +117,7 @@ int getMonth()
100
117
struct tm *ts;
101
118
time (&rawtime);
102
119
ts = gmtime (&rawtime);
103
- int month = ts->tm_mon ;
120
+ int month = ts->tm_mon + 1 ;
104
121
return month;
105
122
}
106
123
@@ -126,6 +143,16 @@ int getWday()
126
143
return wday;
127
144
}
128
145
146
+ String getDayofWeek ()
147
+ {
148
+ return DAY_OF_WEEK[getWday ()];
149
+ }
150
+
151
+ String getShortDayofWeek ()
152
+ {
153
+ return DAY_OF_WEEK_SHORT[getWday ()];
154
+ }
155
+
129
156
int getHour ()
130
157
{
131
158
// tm_hour = hours since midnight (0-23)
0 commit comments