Home AC Power Monitor & Outage Data Logging

Howdy,

I live in a remote rural area of the Ozark Mountains here in north central Arkansas.
Unfortunately we experience power outages fairly often due to weather events and also
have momentary brief outages lasting only a few seconds to a minute or so.

I am wanting to setup a microprocessor to monitor the incoming AC line voltage and
record a date/time stamp that shows time and duration of the outage. Retrieving the data either through the serial monitor or passing it through my LAN to a web page. I am already web page monitoring some Unos and ESPs on my local LAN.

I have perused the Arduino forum, YouTube, and other sites related to accomplishing this task. My takeaway is I need to incorporate an UNO/ESP processor with an RTC module, an SD card module, possibly a DC-DC converter, and a switching battery supply.

There are a ton of RTC modules available and if anyone has recommendations I am open to suggestions. Some are $3 or $4 bucks while others go for $18 dollars or more. I realize the cheapest product is not always the best path to go down.

This switching module from eBay could work to keep the processor online when power drops:

The sketch code required is a little fuzzy to me as I have never never requested data from an
RTC module or wrote data to an SD card. My intent is for the processor to watch the power failure switching module through a digital input and use it to trigger a time stamp and record it to the SD card. Then when the utility power returns record another time stamp. Wash & repeat.

I have installed the Arduino IDE library file DS3232RTC by Jack Christensen. I am assuming that
is the correct file for accomplishing my goal.

If anybody has specific references to existing sketch code that covers writing/retrieval of data to an SD card I would be grateful. I did examine Mr. Christensen's power monitor project on Github [GitHub - JChristensen/PowerOutageMonitor_SW: Sketch for an Arduino-based Power Outage Logger] where he uses a small RGB screen tied to a RTC module and interface buttons but it is much more sophisticated than anything I need.

thanks,

Mister Ed

If you use an ESP module, then you don't need a RTC or SD card.

The ESP keeps perfect time with occasional NTP updates from the internet,
and it's 4MB flash can store many events that you can retrieve from the module via WiFi.

Hardware could be a WeMos D1 mini,
connected to a cellphone powerbank with the USB lead,
connected to mains power.
Nothing else needed, apart from a circuit to detect voltage of the charger.
Leo..

1 Like

In the IDE use the tag file and example. I find an SD example code. From that You could get comfortable wit using SD.
Do the similar for one more peripheral and make it work. The same for the RTC. Installing the RTC lib there should example code for that as well.

Add one new circuit when the others work. Don't smash all circuitry together. That makes fault finding a lot more difficult.

How often would the NTP updates be needed to keep correct time? I imagine if the power is going out frequently or for long periods, how disruptive would that be?

I've been interested and looking into using Flash memory for non volatile storage. I've always worried I'll overwrite part of the bootloader or something stupid though. Do you have a lot of experience in this? If you can help the OP with it I'd be interested in following along.

1 Like

I would recommend a 3.3/5 volt transformer depending on your board voltage, a full bridge rectifier, and some capacitors. You could have a brown out that the board wouldn't detect but if using a battery backup and monitoring the rectified transformer output, you'd be able to distinguish between a brown out and black out.

ESP modules have a crystal based clock, needed for the WiFi part.
I expect it to be as accurate as a cheap RTC module.
A daily update would be fine, hourly updates even better.

I made once a logger that wrote temp and timestamps to SPIFFS every 15min.
Was still going after 4 months, without overwriting things.
I don't see how you can fill up 4MB with the occasional power outage events.

A powerbank is a much easier way to add battery backup.
My WeMos D1 mini ran 36hours on a cheap 5Ah model.

An opto coupler between powerbank mains supply and a WeMos input would be an easy way to detect an outage. Or just detect if the WiFi router is down.
Leo..

Would you be willing to share that code?

Did that project several years ago, when "A beginner's guide to the ESP8266" was a recommended read.

I try to include WiFiManager in my current projects, which has NTP with timezones included.
Some of the lines I use in setup() that forces a certain NTP server and location for timezone/DST.
This must of course be changed for your location.

  session.time.ntp_server = ("nz.pool.ntp.org"); // https://www.pool.ntp.org/zone/nz
  session.time.timezone_env_string = "NZST-12NZDT,M9.5.0,M4.1.0/3"; // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv

Leo..

With the WeMos D1 could the time/date be set initially, say through the serial monitor,or at the time of sketch upload, and not necessarily give it web access? The reason I ask is the processor will reside in my computer room and can be be easily interfaced with a serial monitor link periodically. If the time drift/accuracy is negligible that would be ok as precision is not uber important as far as the time stamp is concerned.

Your suggestion is appreciated!

thanks for the leads.... :+1:

I located the SD test code you referenced in the IDE Examples.
Question: Do you have to setup the needed directory/files in the SD card prior to connecting it to the processor? Or is this something you can accomplish through an upload?

#include "FS.h"
#include "SD.h"
#include "SPI.h"

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("Failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println("Not a directory");
        return;
    }

Start by using the root directory. I've not been playing with SD and don't know if each folder ust be created prior to referencing it or if it's possible to give the path as a part of the filename. Look for documentation like "Read me" and such.

Why wouldn't you give an ESP WiFi board web access.
All it does is contact the NTP server hourly or daily for a millisecond.
No other code has to reside on that board, apart from a local IP local access to extract the log.
Leo..

I was only thinking about not necessarily having a wifi capable processor. Honestly though the cost for having one w/ wifi ability is not a concern w/ these things.

Is this the correct link to get setup for using SPIFFS:

And is it IDE 2.0 compatible?

ESP32 is overkill.
The WeMos D1 mini is cheap and capable enough.

Haven't used SPIFFS in a while, but you get all the examples in the IDE after installing the ESP8266 board files. Don't know if they are now included in IDE 2.x
I still use 1.8.19 because of some features lacking in 2.x
Leo..

Gotcha... I need to go back and re-read the info on the WeMos D1 mini... :+1:

I really think I am going to commit to your suggestion using the WeMos D1 mini as no SD card or RTC is required. And super inexpensive!

Are you familiar with or used the ESP8266 library files:

This library is on both the IDE 1.89 & 2.0 platforms. I know since I have 1.89 installed on this
Linux PC and IDE 2.0 on a Windows Desktop; I do all uploads through Windows though as Linux has continual port issues.

Otherwise I am not for certain which library is going to meet my needs (and be the easiest to
implement).

I might have to pass that to others as to which approach is the easiest to store/retrieve with SPIFF.
For now I would just start with NTP, or the WiFiManager I talked about. That one should be included in the Library Manager of the IDE.
If you have never used an ESP8266 board, then start adding that to the boards manager first.
Leo..

Ok, I got my little, and I mean little, Wemos D1 Mini processor delivered to me.

I have successfully loaded sketches (like Blink) into the D1 and used some example ino files to
write data to the 8266 SPIFFS using the "Effortless-SPIFFS" custom library example. But admittedly understand the code only in a cursory manner.

I first want to establish communication with the D1 thru a WiFi link but am not sure which
code example to reference. The Examples listing from the IDE library is overwhelming in its number of different choices. I tried the Basic.ino from WifiManager under the custom libraries but the result was mixed at best.
If someone has a specific suggestion as to which sketch code to go with I am definitely open to suggestions.

I am not sure if it is the best/correct choice but I chose the Lolin(WEMOS) D1 mini (clone) as the IDE Board choice.

Goals:

  1. establish WiFi contact through my local LAN and retrieve an NTP timestamp from a server and periodic updates; placing it into the 8266 as the time/date reference.
  2. create a text file inside the 8266 SPIFFS and add timestamp variable to the file as a function of an analog/digital input change of state (ie loss of power).
  3. retrieve the file data through a web server/client routine to display on my desktop PC.

Note: I am using the IDE 2.0.4 platform.

Update - Goal 1 Part A successful using : GitHub - arduino-libraries/NTPClient: Connect to a NTP server.
Now need to figure out how to write timestamp to SPIFFS