99// MFRC522 (https://www.tinytronics.nl/shop/nl/communicatie-en-signalen/draadloos/rfid/rfid-kit-mfrc522-s50-mifare-met-kaart-en-key-tag)
1010// 7 segment display ( https://www.hobbyelectronica.nl/product/4-digit-klok-display-module)
1111//
12+ // Heater module
13+ // ESP32 Dev Module
14+ // MFRC522
15+ // 7 segment display
16+ // NPN transistor, 10k, 1k resistor
17+ // 25Ampere, 5volt relay
18+ //
1219// Additional Librariries (via Sketch -> library manager):
1320// MFRC522-spi-i2c-uart-async
1421// TM1637TinyDisplay
4552// two wires. See https://www.mcielectronics.cl/website_MCI/static/documents/Datasheet_TM1637.pdf
4653// and https://create.arduino.cc/projecthub/ryanchan/tm1637-digit-display-arduino-quick-tutorial-ca8a93
4754//
48- #define DISPLAY_CLK 25
49- #define DISPLAY_DIO 26
55+ #define DISPLAY_CLK ( 25 )
56+ #define DISPLAY_DIO ( 26 )
5057
5158TM1637TinyDisplay display (DISPLAY_CLK, DISPLAY_DIO);
5259
60+ // For the heater
61+ #define RELAY (12 )
62+
5363// Very ugly global vars - used to communicate between the REST call and the rest.
5464//
5565char terminalName[64 ] = TERMINAL_NAME;
@@ -75,6 +85,21 @@ void setupDisplay() {
7585 Log.println (" Display set to " VERSION);
7686}
7787
88+ void setupRelay () {
89+ // relay is high-active; but there is a 10k
90+ // pulldown past the 1k driving resistor to
91+ // not let HiZ engage the relay.
92+ //
93+ digitalWrite (RELAY, 0 );
94+ pinMode (RELAY, OUTPUT);
95+ digitalWrite (RELAY, 0 );
96+ }
97+
98+
99+ void heatingOnOff (bool onOff) {
100+ digitalWrite (RELAY, onOff ? HIGH : LOW);
101+ }
102+
78103void setup ()
79104{
80105 const char * p = (const char *) __FILE__;
@@ -91,6 +116,7 @@ void setup()
91116 Serial.print (" \n\n Build: " __DATE__ " " __TIME__ " \n Unit: " );
92117 Serial.println (terminalName);
93118
119+ setupRelay ();
94120 setupDisplay ();
95121 setupAuth (terminalName);
96122
@@ -151,10 +177,13 @@ void setup()
151177 Log.println (" OTA started" );
152178 })
153179 .onEnd ([]() {
180+ Log.println (" OTA completed" );
154181 // prevent a cleversod uplading a special binary. ignore the hardware/serial angle
155182 Log.println (" OTA completed OK - need to wipe keystore before rebooting into new code." );
156183 wipekeys ();
157184 display.showString (" Done" );
185+ log_stop ();
186+ heatingOnOff (false );
158187 })
159188 .onProgress ([](unsigned int progress, unsigned int total) {
160189 static int l = -100 ;
@@ -168,15 +197,18 @@ void setup()
168197 })
169198 .onError ([](ota_error_t error) {
170199 const char * label;
171- if (error == OTA_AUTH_ERROR) label = (const char *) " Auth Failed " ;
172- else if (error == OTA_BEGIN_ERROR) label = (const char *) " Begin Failed " ;
173- else if (error == OTA_CONNECT_ERROR) label = (const char *) " Connect Failed " ;
174- else if (error == OTA_RECEIVE_ERROR) label = (const char *) " Receive Failed " ;
175- else if (error == OTA_END_ERROR) label = (const char *) " End Failed " ;
176- else label = " Uknown error " ;
200+ if (error == OTA_AUTH_ERROR) label = (const char *) " FO:AF " ;
201+ else if (error == OTA_BEGIN_ERROR) label = (const char *) " FO:BF " ;
202+ else if (error == OTA_CONNECT_ERROR) label = (const char *) " FO:CF " ;
203+ else if (error == OTA_RECEIVE_ERROR) label = (const char *) " FO:RF " ;
204+ else if (error == OTA_END_ERROR) label = (const char *) " FO:EF " ;
205+ else label = " FO:OF " ;
177206 display.showString (label);
178- Log.print (" OTA error" );
207+ Log.print (" OTA error: E=" );
208+ Log.print (error);
209+ Log.print (" - " );
179210 Log.println (label);
211+ delay (10 * 1000 );
180212 });
181213
182214 Debug.println (" Starting loop" );
@@ -205,10 +237,10 @@ static void loop_RebootAtMidnight() {
205237 // "Thu Nov 4 09:47:43\n\0" -> 09:47\0
206238 p += 11 ;
207239 p[5 ] = 0 ;
208- Debug.printf (" strncmp(\" %s\" ,\" %s\" ,%d), %ld, %lu\n " , p, AUTO_REBOOT_TIME, strlen (AUTO_REBOOT_TIME), now, millis ());
209240
210- if (strncmp (p, AUTO_REBOOT_TIME, strlen (AUTO_REBOOT_TIME)) == 0 && millis () > 3600 ) {
211- Log.println (" Nightly reboot - also to fetch new pricelist and fix any memory eaks." );
241+ if (strncmp (p, AUTO_REBOOT_TIME, strlen (AUTO_REBOOT_TIME)) == 0 && millis () > 3600 * 1000 ) {
242+ Log.println (" Nightly reboot - also to fetch new pricelist and fix any memory leaks." );
243+ heatingOnOff (false );
212244 ESP.restart ();
213245 }
214246#endif
@@ -265,6 +297,16 @@ void loop()
265297 ArduinoOTA.handle ();
266298 loop_RebootAtMidnight ();
267299
300+ // flap relay every 5 seconds for testing.
301+ //
302+ if (0 ) {
303+ static unsigned long t = millis ();
304+ if (millis () - t > 5000 ) {
305+ digitalWrite (RELAY, !digitalRead (RELAY));
306+ t = millis ();
307+ }
308+ }
309+
268310 switch (md) {
269311 case WAITING_FOR_NTP:
270312 display.showString (" ntp" );
@@ -315,20 +357,23 @@ void loop()
315357 lst += 1000 ;
316358 }
317359
318- // flash display 1.5 times a second in last minute.
319- if (countdown < 60 ) {
320- display.setBrightness (((millis () / 300 ) & 1 ) ? BRIGHT_HIGH : BRIGHT_HIGH / 2 );
360+ // flash display 1.5 times towards the end
361+ if (countdown <= 60 ) {
362+ display.setBrightness (((millis () / 300 ) & 1 ) ? BRIGHT_HIGH : BRIGHT_1 );
321363 };
322364
323365 if (countdown <= 0 ) {
366+ heatingOnOff (false );
324367 countdown = 0 ;
325- display.showString (" 00:00" );
326368
327369 // flash aggressivily once time is up.
328370 //
329- for (int i = 0 ; i < 20 ; i++) {
330- display.setBrightness ((i & 1 ) ? BRIGHT_HIGH : 0 );
331- delay (200 );
371+ for (int i = 0 ; i < 30 ; i++) {
372+ display.setBrightness ((i & 1 ) ? BRIGHT_HIGH : BRIGHT_LOW);
373+ display.showString (" 0000" );
374+ delay (100 );
375+ display.showString (" ----" );
376+ delay (100 );
332377 };
333378
334379 // go back to normal
@@ -340,8 +385,9 @@ void loop()
340385 countdown += checkcardswipe ();
341386
342387 char timeleft[32 ];
343- snprintf (timeleft, sizeof (timeleft) - 1 , " %02d:%02d" , countdown / 60 , countdown % 60 );
344- display.showString (timeleft);
388+ snprintf (timeleft, sizeof (timeleft) - 1 , " %02d%02d" , countdown / 60 , countdown % 60 );
389+ display.showString (timeleft, 4 , 0 /* pos*/ , 0b01000000 /* dots */ );
390+ heatingOnOff (true );
345391 break ;
346392 }
347393 case WIFI_FAIL_REBOOT:
0 commit comments