Revert to last known state after shutdown/power cut (RTC1307, LEDS, RELAYS)

Hello,

This is my first post here, so please forgive me if I'm supposed to be posting this elsewhere.

Over the passed couple of months I have been working on an indoor gardening project that has been a fun learning experience, however I have ran into a situation where my lack of knowledge is beginning to show it's consequences.

I am using a DS1307 RTC as a source for an alarm function in order to turn on and off relays and LEDS, so far so good. My problem is that I need a way to revert to the last known state of LEDS and relays controlling lights and fans in case of a power cut.

There will be 2 main alarms for the lighting, Alarm 1 to turn on the lighting at given time, Alarm 2 to switch it off. In case of a power cut I need to revert to the last known state of the light and not have to wait for the next known alarm function.

I have read that I can save the last known state to the EEPROM but that will quickly become a problem especially if I need to save every minute or so, can an SD card module be used or an ESP32 ?

I am not expecting anyone to write the code for me, but any sketches or helpful information concerning the code and hardware would be highly appreciated.

can an SD card module be used or an ESP32 ?

I don't see why not as it can be used on more humble Arduinos

Another possibility may be to use SPIFFS for filing the data. You should get plenty of hits on Google if you search for it

Over the passed couple of months I have been working on an indoor gardening project

Indoor gardening sounds like something that is illegal where I live... :wink:

I am using a DS1307 RTC as a source for an alarm function in order to turn on and off relays and LEDS, so far so good. My problem is that I need a way to revert to the last known state of LEDS and relays controlling lights and fans in case of a power cut.

The DS1307 has 56 bytes of battery backed RAM. Should be plenty of storage space for what you need.

I use EEPROM in exactly the way you want.
The trick is only to write when you ‘need’ to.... (to save write cycles)

I run all the values in RAM until power fails or the unit is shut down.
Then I write to EEPROM, and read the values back when the board restarts.
In many sites this may be ,o this, weeks or at least a couple of days between (restarts) writes.

There are several posts about this on the forum, dig around. a diode, electrolytic capacitor and three resistors on an analog input pin for you to monitor the raw supply voltage. Voila. A 470uF capacitor gives me enough time to save >2K of data when the supply fails.

I run all the values in RAM until power fails or the unit is shut down.
Then I write to EEPROM, and read the values back when the board restarts.
In many sites this may be ,o this, weeks or at least a couple of days between (restarts) writes.

This concept doesn't work if you have power failures (not a controlled shut down) because in that case you don't have the power to write anything to the EEPROM.

Power-fail save to EEPROM works fine... as I said with a complete power loss (pull the power lead), I have time to save > 2K to EEPROM with a 470uF electrolytic & voltage divider before the regulators)

I have more than a hundred units in the field using this hardware & code every day (in rural applicrions with flakey power and randy cows that rip out customer's cable etc !
Here's a demo board running on my desk...

>                     (HERE IS WHERE THE POWER INPUT IS SIMPLY PULLED FROM THE BOARD)
                      (POLLing the ADC input in loop() sees the input voltage has gone below a threshold)
EEPROM SAVED          (The magic happens here)
------------------------
*** CYCLE POWER TO ***         (Information only on the console port)
*** RESTART SYSTEM ***
------------------------
                               (This is just part of the normal 'crash stop' sequence )
Modem power-off...             (irrelevant if power is already gone!)
Build: Jan 14 2020
------------------------       (### RESTORE POWER CABLE HERE ###)
EEPROM 0x7, 2827b              (Reade power-up states here (2.8 KB of EEPROM data)
PASSCODE is BLANK
------------------------       (This is a battery backed 3G cellular project...)
Modem power-on, init, Registered
SIM: READY
WCDMA 850 Telstra Mobile
Use carrier time               (no RTC needed here - DST also handled autromatically !)
Modem is awake
00:00 Out 1 ON                 (These two relay outputs were re-instated from a LAST-KNOWN status )
00:00 Out 3 ON
========================
ControlMate v200112            (Firmware version announcement
------------------------
Sold To [factory]              (SIte/customer info - from EEPROM)
SITE: Default Site
Panel Mode:100 (LCD enabled)
Using Last known               ( <<--- User selectable in the configuration )
SIM5320-A                      (info for support)
IMEI: 01xxxxxxxxxx071
PASSCODE []
========================
System Ready
CLK: Wed 200115 08:38          (cell time has been set INTO the controller)

OK, but that doesn't work with standard hardware (e.g. UNO).

Hmm, ok if you don’t have a shield or breadboard.
OP was asking for a solution to add to his DS1307 RTC based project. My UNOs don’t have an RTC or I2C pull-ups either.

OP was asking for a solution to add to his DS1307 RTC based project. My UNOs don't have an RTC or I2C pull-ups either.

A voltage divider before the regulator isn't that easy even if have a breadboard. If OP powers it's setup by USB for example.
And the code must be very responsive as your capacitor powers the Arduino 20ms under ideal conditions.

All I wanted to say: It's not a solution valid for everyone. It seems to work for you but fails in a lot of situations.