Skip to content

Commit 878d399

Browse files
committed
OTA updates for the Flip disc clock!
1 parent e3efb20 commit 878d399

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

4x7seg-flip-disc-clock/4x7seg-flip-disc-clock.ino

+68-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
2. Or any Arduino board + Pulse Shaper Power Supply - https://bit.ly/PSPS-FD
1515
----------------------------------------------------------------------------------*/
1616

17+
// Use "NodeMCU 0.9 (ESP-12)" or "WEMOS D1 (clone)" to program (NOT Generic8266)
18+
1719
/* The library <FlipDisc.h> uses SPI to control flip-disc displays.
1820
The user must remember to connect the display inputs marked:
1921
- DIN - data in - to the MOSI (SPI) output of the microcontroller,
@@ -25,23 +27,30 @@
2527
The declaration of DIN (MOSI) and CLK (SCK) is not necessary,
2628
because the SPI.h library handles the SPI hardware pins. */
2729

28-
// Use "NodeMCU 0.9 (ESP-12) to program (NOT Generic8266)
2930
#include <coredecls.h> // for settimeofday_cb()
3031
#include <time.h>
32+
3133
#include <ESP8266WiFi.h>
34+
#include <ESP8266mDNS.h>
35+
#include <WiFiUdp.h>
36+
#include <ArduinoOTA.h>
37+
3238
#include <FlipDisc.h> // https://github.com/marcinsaj/FlipDisc
3339

3440
// Local include for sekrits
3541
#include "config.h"
3642
const char* ssid = SECRET_SSID;
3743
const char* pass = SECRET_PWD;
3844

45+
// For OTA upload status
46+
int progressStatus = 0;
47+
3948
// Pin definitions for the flip clock
4049
#define EN_PIN D1
4150
#define CH_PIN D2
4251
#define PL_PIN D3
4352

44-
// Note, MOSI (DataIn), Clk (SCLK) default to D7 and D5, respectively, on the 8266 I have.
53+
// Note, MOSI (DataIn), Clk (SCLK) default to D7 and D5, respectively, on the ESP8266 I have.
4554
// I don't know how to change these, shrug.
4655

4756
void setup() {
@@ -84,12 +93,13 @@ void setup() {
8493
// send credentials
8594
Serial.println("Connecting");
8695
Flip.Matrix_7Seg(C, O, N, N);
96+
WiFi.mode(WIFI_STA);
8797
WiFi.begin(ssid, pass);
8898
int dot = 0;
8999
// Wait for connection
90-
while (WiFi.status() != WL_CONNECTED) {
100+
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
91101
delay(500);
92-
Flip.Disc_7Seg(1, (dot/2) % 23, dot % 2); // last argument can be 0 to turn off
102+
Flip.Disc_7Seg(1, (dot / 2) % 23, dot % 2); // last argument can be 0 to turn off
93103
dot++;
94104
Serial.print(".");
95105
}
@@ -113,6 +123,55 @@ void setup() {
113123
settimeofday_cb(timeUpdated);
114124

115125
digitalWrite(LED_BUILTIN, LOW);
126+
127+
// All sorts of OTA (over-the-air) updates.
128+
// Code mostly copied from https://randomnerdtutorials.com/esp8266-ota-updates-with-arduino-ide-over-the-air/
129+
ArduinoOTA.setPort(OTA_PORT);
130+
ArduinoOTA.setHostname("flipclock");
131+
ArduinoOTA.setPassword(OTA_PASSWORD);
132+
ArduinoOTA.onStart([]() {
133+
progressStatus = 0;
134+
Flip.Matrix_7Seg(S, T, R, T);
135+
digitalWrite(LED_BUILTIN, LOW);
136+
Serial.println("Start");
137+
});
138+
ArduinoOTA.onEnd([]() {
139+
digitalWrite(LED_BUILTIN, HIGH);
140+
Flip.Matrix_7Seg(E, N, D, CLR);
141+
Serial.println("\nEnd");
142+
});
143+
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
144+
if (progressStatus == 0) {
145+
Flip.Matrix_7Seg(P, R, O, G);
146+
}
147+
digitalWrite(LED_BUILTIN, progressStatus % 2);
148+
progressStatus++;
149+
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
150+
});
151+
ArduinoOTA.onError([](ota_error_t error) {
152+
digitalWrite(LED_BUILTIN, LOW);
153+
Flip.Matrix_7Seg(E, R, R, CLR);
154+
delay(2000);
155+
Serial.printf("Error[%u]: ", error);
156+
if (error == OTA_AUTH_ERROR) {
157+
Flip.Matrix_7Seg(A, U, T, H);
158+
Serial.println("Auth Failed");
159+
} else if (error == OTA_BEGIN_ERROR) {
160+
Flip.Matrix_7Seg(B, E, G, N);
161+
Serial.println("Begin Failed");
162+
} else if (error == OTA_CONNECT_ERROR) {
163+
Flip.Matrix_7Seg(C, O, N, N);
164+
Serial.println("Connect Failed");
165+
} else if (error == OTA_RECEIVE_ERROR) {
166+
Flip.Matrix_7Seg(R, E, C, V);
167+
Serial.println("Receive Failed");
168+
} else if (error == OTA_END_ERROR) {
169+
Flip.Matrix_7Seg(E, N, D, CLR);
170+
Serial.println("End Failed");
171+
}
172+
delay(2000);
173+
});
174+
ArduinoOTA.begin();
116175
}
117176

118177
int last_hour;
@@ -182,10 +241,13 @@ void loop() {
182241
int sec = now->tm_sec;
183242
if (sec != last_sec) {
184243
int hour = now->tm_hour;
244+
if (hour > 12) {
245+
hour -= 12;
246+
}
185247
int hr10 = hour / 10;
186248
// If hour is 10-12, turn bit 10 off, otherwise turn bit 10 on
187249
for (int i = 0; i < sec / 10; i++) {
188-
if (i == 0 && hr10 == 1) {
250+
if (i == 0 && hr10 == 0) {
189251
// Serial.println("turning *on* bit 10 of 1");
190252
Flip.Disc_7Seg(i + 1, 10, 1);
191253
} else {
@@ -232,4 +294,5 @@ void loop() {
232294
// this sets last_hour, last_min and last_sec
233295
showTime(now->tm_hour, now->tm_min, now->tm_sec);
234296
}
297+
ArduinoOTA.handle();
235298
}

0 commit comments

Comments
 (0)