ESP32 and DS3231 RTC alarms

Leo Duivenvoorde's picture

Offline

Joined: Aug 24, 2019

Replies: 5
ESP32 and DS3231 RTC alarms
August 24, 2019 - 3:46pm

Hallo all,

First of all: I am a noob. Ican do quite lot, but some things are beyond me. (Reason for subscribing to this site.)

I am currently working with ESP32. I want to replace my Arduino Mega with an ESP32, doing the same work the Arduino is doing now.

The Arduino measures my Elec. Power and Gas consumption and posts this in MySQL every 5th minute. Data sent is: avg-W, hourly Wh, daily kWh, avg-dm3, hourly dm3 and daily m3. Measurements are reset at midnight. For this purpose I have a DS3231 attached to the Arduino.

I can get the ESP32 working with DS3231, time/date-wise. But I can not set 'alarms' for resetting. Because I have now clue how to set alarms, in combination with ESP32. (I can with Arduino.)

When I use the Arduino-script, I get all sort of fault-reports. It seems like that the libraries I use are not compatible with ESP32.(And I have used/downloaded a lot diverent ones.)

The only DS3231 librarie which seems to work is RTClib.h, but I have no clue how to set the alartms. I have Googled a lot, but no succes.

Can one of you experts help?

With best regards,

Leo

 

 

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 923
What do you mean by setting alarm?
August 25, 2019 - 7:30pm

Hi Leo, what do you mean by setting an alarm? Are you trying to display something on the screen when alarm time is equal to current time or do someting with hardware. /Also have you considered the idea that you will not be needing an RTC module with ESP32 as long as your board is connected to the internet. You can directly get the time from the internet 

Leo Duivenvoorde's picture

Offline

Joined: Aug 24, 2019

Replies: 5
ESP32 and DS3231 RTC alarms
August 26, 2019 - 2:33pm

The idea is to to take sensor measurements and send these at specifics times to my Synology-NAS which has MySQL (for Data) and Domoticz (for graphics).

For me an alarm is a point in time where something has to be done

I will give you little more background of current system using Arduino Mega - Ethernetshield, Light-sensor, IR-transmitter-receiver-sensor and DS3231 RTC.

Elec Power consumption is measured with the light sensitive sensor as there is a LED on the Elec-meter, which flashes 500 times for every kWh consumed. By measuring the time between flashes Watt usages can be calculated and by counting flashes (k)Wh is calculated.

Similar for Gas-consumption, only there the consumption-pulse is generated by shining IR-light onto the Liter-consumption-wheel of the gasmeter. The number 6 on this wheel has an reflector, the refected light is detected by the IR-receiver. Thus everery IR-light pulse represents 10 dm3 gas cosumption.. 

This system is quite accurate; error measured during 1 month of comsumption is less than 1%. The most important thing here is not to miss any pulses. (At peak times, elec-pulses can be less than 1 sec. apart.)

The reason for using DS3231 RTC in current system is:

-it is much more accurate compared to DS1307, which drifts too much (and DS3231 = almost at same price).

-it can be programmed with 2 different alarms at which  something has to be done, e.g. transmit data, or reset daily and hourly counters.

- time is kept during power failure due to on-board battery.

As such the system will spent less time on making for instance internat connection, etc.

My fear is that when I use an ESP32 and get time from internet, I will loose pulses. Also I will have to do all sorts of calculations and comparisons to estimate when data has to be transmitted or things has to be reset.

As the DS3231 RTC has to abbillity to hold to 2 älarm'-timings, I was wondering if some one knows how to set these alarms for ESP32. (Apparently the DS3231 libraries which I can use for the Arduino Mega can not be used for ESP32-boards.)

Regards,

Leo

Aswinth Raj's picture

Offline

Joined: Aug 16, 2016

Replies: 923
Interesting Project
August 27, 2019 - 9:39pm

Elec Power consumption is measured with the light sensitive sensor as there is a LED on the Elec-meter, which flashes 500 times for every kWh consumed. By measuring the time between flashes Watt usages can be calculated and by counting flashes (k)Wh is calculated.

Similar for Gas-consumption, only there the consumption-pulse is generated by shining IR-light onto the Liter-consumption-wheel of the gasmeter. The number 6 on this wheel has an reflector, the refected light is detected by the IR-receiver. Thus everery IR-light pulse represents 10 dm3 gas cosumption.. 

Thats one crude way to measure power consumption and Gas usage, but still smart. This was you can measure your electricty consumption without any additional sensors (like CT or hall sensor) and actually measure the Power consumption accurately without even tampeirng with the meter. I might as well try it some day ....

As the DS3231 RTC has to abbillity to hold to 2 älarm'-timings, I was wondering if some one knows how to set these alarms for ESP32. (Apparently the DS3231 libraries which I can use for the Arduino Mega can not be used for ESP32-boards.)

I am still not sure what you exactly want to do in your program during the alarm event. Anyway if you problem is with setting up the ESP32 with DS3231 then you might want to look into this project

https://circuitdigest.com/microcontroller-projects/esp32-real-time-clock-using-ds3231-module

As you said the Arduino librarey for DS3231 will not work for ESP32. Follow the tutorial above to download ans use relevant librarey 

Leo Duivenvoorde's picture

Offline

Joined: Aug 24, 2019

Replies: 5
Interesting Project
August 29, 2019 - 3:08pm

With the current Arduino/DS3231 system, alarm1 is set at Matching_second=00. Thus this alarms 'fires' every minute. When it does, a minute-counter is increased with 1. When the minute-counter equals 5, data is written to MySQL and the minute-counter is reset to 0. As such, data will be written every 5th minute. (I can not use a delay()-command as this will 'freeze' the system and no pulses are detected.)

The second alarm of the DS3231 'fires' every day at 23:55. That will trigger the writing of the total daily kWh and m3 Gas to MySQL. (Both with 1 decimal number.)

Why at 23:55? Two reasons:

a) At midnight 00:00, all counters are reset to 0. OK, I will loose 5 mniutes of data, but gas-consumption is 0.0 dm3 at that time of night; 5 minutes of not measuring Elec-Power consumption represents maximal 35 Wh; thus this will not be affecting the total of say 12.0 kWh.

b) Sometimes there is a delay when data is written to MySQL, due to other work the Synology-NAS has to do and due to 'traffic' on the LAN. As the data written to MySQL is time-stamped, I want to avoid that data is written at say 00:00:01. Because when that happens, the total daily consumption will 0 for that day and there might be 2 daily consumptions for the following day.

 

Leo Duivenvoorde's picture

Offline

Joined: Aug 24, 2019

Replies: 5
Interesting Project
August 29, 2019 - 3:52pm

I had seen your recommended link already. That brought me to the RTClib.h which works very well for EPS32, but no alarms. In the mean time, I have found this:

https://github.com/northernwidget/ds3231

This also works, and has alarms. But, I do not know how to set eg matching_seconds. Also, it is not clear to me how to detect that the alarms has fired. I might be giving them an email or so.