1
- // Use "NodeMCU 0.9 (ESP-12) to program (NOT Generic8266)
1
+ // Use "NodeMCU 0.9 (ESP-12)" or "WEMOS mini D1 (clone)" to program (NOT Generic8266)
2
2
#include " config.h"
3
+
3
4
#include < ESP8266WiFi.h>
5
+ #include < ESP8266mDNS.h>
6
+ #include < WiFiUdp.h>
7
+ #include < ArduinoOTA.h>
8
+
4
9
#include < time.h>
5
10
#include < coredecls.h> // required for settimeofday_cb()
6
11
@@ -13,9 +18,11 @@ const char* pass = SECRET_PWD;
13
18
// Timing parameters
14
19
time_t present_timestamp;
15
20
21
+ int progressStatus = 0 ;
22
+
16
23
// Display
17
24
#define NUM_DIGITS 8
18
- const byte dataPin = D1 ;
25
+ const byte dataPin = D4 ;
19
26
const byte loadPin = D2;
20
27
const byte clockPin = D3;
21
28
@@ -32,19 +39,21 @@ void setup() {
32
39
33
40
Serial.begin (115200 );
34
41
35
- Serial.println (" Hello 7seg Clock serial" );
36
- display.showTextScroll (" Connecting" );
42
+ Serial.println (" Hello 7seg Clock serial d1 " );
43
+ display.showTextScroll (" Connecting D1.... " );
37
44
38
45
WiFi.begin (ssid, pass); // send credentials
39
46
Serial.println (" Connecting" );
40
47
int dot = 0 ;
41
48
// wait for connection
42
- while (WiFi.status () != WL_CONNECTED) {
49
+ while (WiFi.waitForConnectResult () != WL_CONNECTED) {
43
50
delay (500 );
44
51
display.showText (" ." , dot++, 1 );
45
52
}
46
53
Serial.println (" Connected" );
47
- display.showTextScroll (" Connected" );
54
+ Serial.print (" IP address: " );
55
+ Serial.println (WiFi.localIP ());
56
+ display.showTextScroll (" Connected!!! " );
48
57
delay (1000 );
49
58
50
59
// implement NTP update of timekeeping (with automatic hourly updates)
@@ -54,13 +63,60 @@ void setup() {
54
63
setenv (" TZ" , " EST+5EDT,M3.2.0/2:00:00,M11.1.0/2:00:00" , 1 );
55
64
56
65
// register a callback (execute whenever an NTP update has occurred)
57
- // DBP: Unclear if this is needed.
58
66
settimeofday_cb (time_is_set);
67
+
68
+ // All sorts of OTA (over-the-air) updates.
69
+ // Code mostly copied from https://randomnerdtutorials.com/esp8266-ota-updates-with-arduino-ide-over-the-air/
70
+ ArduinoOTA.setPort (OTA_PORT);
71
+ ArduinoOTA.setHostname (" sevensegwordd1" );
72
+ ArduinoOTA.setPassword (OTA_PASSWORD);
73
+ ArduinoOTA.onStart ([]() {
74
+ progressStatus = 0 ;
75
+ display.showText (" Start" );
76
+ digitalWrite (LED_BUILTIN, LOW);
77
+ Serial.println (" Start" );
78
+ });
79
+ ArduinoOTA.onEnd ([]() {
80
+ digitalWrite (LED_BUILTIN, HIGH);
81
+ display.showText (" End" );
82
+ Serial.println (" \n End" );
83
+ });
84
+ ArduinoOTA.onProgress ([](unsigned int progress, unsigned int total) {
85
+ if (progressStatus == 0 ) {
86
+ display.showText (" Updating" );
87
+ }
88
+ digitalWrite (LED_BUILTIN, progressStatus % 2 );
89
+ progressStatus++;
90
+ Serial.printf (" Progress: %u%%\r " , (progress / (total / 100 )));
91
+ });
92
+ ArduinoOTA.onError ([](ota_error_t error) {
93
+ digitalWrite (LED_BUILTIN, LOW);
94
+ Serial.printf (" Error[%u]: " , error);
95
+ if (error == OTA_AUTH_ERROR) {
96
+ display.showTextScroll (" Auth failed" );
97
+ Serial.println (" Auth Failed" );
98
+ } else if (error == OTA_BEGIN_ERROR) {
99
+ display.showTextScroll (" Begin failed" );
100
+ Serial.println (" Begin Failed" );
101
+ } else if (error == OTA_CONNECT_ERROR) {
102
+ display.showTextScroll (" Connect failed" );
103
+ Serial.println (" Connect Failed" );
104
+ } else if (error == OTA_RECEIVE_ERROR) {
105
+ display.showTextScroll (" Receive failed" );
106
+ Serial.println (" Receive Failed" );
107
+ } else if (error == OTA_END_ERROR) {
108
+ display.showTextScroll (" End failed" );
109
+ Serial.println (" End Failed" );
110
+ }
111
+ delay (2000 );
112
+ });
113
+ ArduinoOTA.begin ();
59
114
}
60
115
61
116
void loop () {
62
117
// loop2();
63
118
loopOrig ();
119
+ ArduinoOTA.handle ();
64
120
}
65
121
66
122
void loopOrig () {
0 commit comments