[SOLVED] DS3231 uRTC set / read problems

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: [SOLVED] DS3231 uRTC set / read problems

Post by deshipu » Sun Aug 06, 2017 8:54 pm

I'm not sure if that helps, but you can always use the _register and _flag methods on the urtc object to talk to the chip directly, and thus set whatever settings you need.

vahithosan
Posts: 19
Joined: Wed Jul 26, 2017 5:15 pm

Re: [SOLVED] DS3231 uRTC set / read problems

Post by vahithosan » Thu Aug 10, 2017 9:55 pm

deshipu wrote:I'm not sure if that helps, but you can always use the _register and _flag methods on the urtc object to talk to the chip directly, and thus set whatever settings you need.
Thanks. I just started working on micropython. Your library file was very instructive
I made changes to the urtc file. Because I have the DS3231 IC on my hand, I just worked on it.

I seperated Alarm registers ,

I added a function named alarm_state() for contol the "control register"

Datasheet writes that
Alarm1 supports seconds, minutes, hours, days (or weekdays)
Alarm2 supports minutes, hours, days (or weekdays)

There are 3 conditions for the alarm to work

for Alarm1 >
INTCN = 1
A1IE = 1
A1F = 1

for Alarm2 >
INTCN = 1
A2IE = 1
A2F = 1

AxF bits in 0e register
AxIE bits in 0f register

for mute alarm, AxF must be 0

Code: Select all

rtc.alarm1(0)
rtc.alarm2(0)
for completely stop alarm, AxIE must be 0

Code: Select all

rtc.alarm1_state(0)
rtc.alarm2_state(0)
If you want to run it again

Code: Select all

rtc.alarm1_state(1)
rtc.alarm2_state(1)
For example: (set Alarm1 30. second in every minute)

Code: Select all

>>> rtc.alarm1_time((None,None,None,None,None,None,30,None))
>>> rtc.datetime()
DateTimeTuple(year=2017, month=8, day=11, weekday=4, hour=0, minute=25, second=5, millisecond=None)
>>> kutuk(14,16)
0e | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 
------------------------------------
0f | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 
------------------------------------
------------------------------------
>>> rtc.datetime()
DateTimeTuple(year=2017, month=8, day=11, weekday=4, hour=0, minute=25, second=31, millisecond=None)
>>> kutuk(14,16)
0e | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 
------------------------------------
0f | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 
------------------------------------
------------------------------------
Mute alarm

Code: Select all

>>> rtc.alarm1(0)
>>> kutuk(14,16)
0e | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 
------------------------------------
0f | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 
------------------------------------
------------------------------------
Disable alarm

Code: Select all

>>> rtc.alarm1_state(0)
>>> kutuk(14,16)
0e | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 
------------------------------------
0f | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 
------------------------------------
------------------------------------
Enable alarm

Code: Select all

>>> rtc.alarm1_state(1)
>>> kutuk(14,16)
0e | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 
------------------------------------
0f | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 
------------------------------------
------------------------------------
I've added the file
Attachments
DS3231.py.tar.gz
(1.47 KiB) Downloaded 371 times

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: [SOLVED] DS3231 uRTC set / read problems

Post by deshipu » Fri Aug 11, 2017 5:32 pm

Thank you for this. I will try to fix that in the library, but it can be long before I have the time to do it properly.

User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: [SOLVED] DS3231 uRTC set / read problems

Post by ioukos » Sun Sep 10, 2017 11:43 am

Hello deshipu, vahithosan,

@vahitosan : Is your modified version of the lib do the trick ? I mean, in your 1st post you wanted to use an external DS3231 in order to wake up an ESP8266 from deepsleep.
Did you succeed ?

This is exactly what i want to do.

@deshipu : can you confirm you didn't merge the modification in your branch on github ? (If not, just asking, don't be offended, I know you work a lot for the comunity)

Have a Nice Day,

vahithosan
Posts: 19
Joined: Wed Jul 26, 2017 5:15 pm

Re: [SOLVED] DS3231 uRTC set / read problems

Post by vahithosan » Sun Sep 10, 2017 2:14 pm

ioukos wrote:Hello deshipu, vahithosan,

@vahitosan : Is your modified version of the lib do the trick ? I mean, in your 1st post you wanted to use an external DS3231 in order to wake up an ESP8266 from deepsleep.
Did you succeed ?

This is exactly what i want to do.

@deshipu : can you confirm you didn't merge the modification in your branch on github ? (If not, just asking, don't be offended, I know you work a lot for the comunity)

Have a Nice Day,
I cant use ds3231 for wake up. Because alarm output continuous low level until muted. I need to use something like a monostable multivirator. Then you will have to use materials. I did not want to do that

I used internal rtc for awaking and ds3231 for sleeping

https://docs.micropython.org/en/latest/ ... interrupts

Code: Select all

import machine
import network
import socket
import os
import time
import utime
import ds3231

from ntptime import settime
from time import sleep
from machine import I2C, Pin

i2c = I2C(scl=Pin(5), sda=Pin(4))
rtc = machine.RTC()
rtc1 = ds3231.DS3231(i2c)
p2 = Pin(2, Pin.IN)

ssid='**********'			
wifikey='**********'		

sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
sta_if.active(True)
sta_if.connect(ssid, wifikey)

# for set time with ntp protocol
settime()
tampon1=utime.time()
tampon2=tampon1+10800
datetime = ds3231.seconds2tuple(tampon2)
rtc1.datetime(datetime)
rtc1.datetime()

# set alarm
rtc1.alarm1_time((None,None,d,None,h,m,s,None))

def calis(change):
        # mute alarm
        rtc1.alarm1(0)
        while True:
                print("Your scripts")
                rtc = machine.RTC()
                rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
                rtc.alarm(rtc.ALARM0, 600000) # 10 minutes
                rtc1.datetime()
                machine.deepsleep()
                

p2.irq(trigger=Pin.IRQ_RISING | Pin.IRQ_FALLING, handler=calis)


User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: [SOLVED] DS3231 uRTC set / read problems

Post by ioukos » Sun Sep 10, 2017 4:58 pm

Thank you for the answer,

What I want to do :
The ESP gets, with an API, next sunrise time
The ESP sets the alarm to be triggered at sunrise
The ESP goes to sleep
The alarm awakes the ESP
The ESP does some work
The ESP gets, with the API, next sunset time
The ESP goes to sleep
The alarm awakes the ESP
The ESP does some work
etc.....

It seems I can't do that as you mentioned. May be I'll use a Raspberry and MQTT Broker, or don't sleep the ESP at all. The ESP would just wait the alarm to be triggered by checking constantly on the DS3231.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: [SOLVED] DS3231 uRTC set / read problems

Post by deshipu » Mon Sep 11, 2017 11:32 am

ioukos wrote: @deshipu : can you confirm you didn't merge the modification in your branch on github ? (If not, just asking, don't be offended, I know you work a lot for the comunity)
I'm not sure what modification you mean exactly. In any case, you can check what changes have been made to the library by visiting https://github.com/adafruit/Adafruit-uR ... its/master

I need to sit down and test this chip, as it seems that just reading the datasheet and doing some simple command-line tests is not enough to make it all work properly. I had a lot of errors in my library due to the fact that I was reading three different datasheets at the same time, and made stupid assumptions that they would handle things in similar way. There may be more similar errors there, but before I change anything, I want to make sure it works, so that I don't break more things by making random changes. Unfortunately that requires some time, and I'm a bit pressed with other projects.

I don't understand what I would be offended by?

User avatar
ioukos
Posts: 34
Joined: Wed Oct 19, 2016 11:31 am
Location: Alsace, Europe

Re: [SOLVED] DS3231 uRTC set / read problems

Post by ioukos » Tue Sep 12, 2017 7:54 pm

Deshipu,

I wrote "don't be offended" as I didn't want to be rude, and I didn't want you to understand : "hey man it's been a month you said you'd do something and modifications are still pending, what a lazy man". That's all.

Bye

Post Reply