"machine.RTC" alarm + set irq

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Jerremy09
Posts: 22
Joined: Wed May 08, 2019 7:40 am

"machine.RTC" alarm + set irq

Post by Jerremy09 » Tue Jan 21, 2020 6:45 pm

Hello All,

from my unknown reason I cannot set time based "irq" function. I red in documentaiton: "http://docs.micropython.org/en/latest/l ... e.RTC.html" that "Create an irq object triggered by a real time clock alarm.". so I tried to set allarm in boot.py, but always receive this error in bot files.

Purpose of code: to test time based alarm only - that is why it is so simple.
i thought taht I create several alarms like:
Alarm0 - every 60 seconds
Alarm1 - every 20 seconds
Alarm2 - every 15 seconds
Alarm3 - every 5 seconds

and let them be triggered in main.py to do some code (blink - for Alarm0), but I´m not even build a single one.

Board: Lolin d32 Pro V2.0
Micropython firmware: esp32-idf3-20191220-v1.12.bin

boot.py:

Code: Select all

import gc
import machine
import utime

#/-----------------------------RTC-----------------------------/#

rtc = machine.RTC()
rtc.init((2020, 1, 1, 2, 12, 00, 0, 0))
#Format: year, month, day, weekday, hour, minute, second, microseconds
rtc.alarm(0, 60000, repeat=True)

#/---------------------LED Confirmation---------------------/#
LED = machine.Pin(5,machine.Pin.OUT)
LED.value(0)
utime.sleep(0.1)
LED.value(1)
utime.sleep(0.1)
LED.value(0)
utime.sleep(0.1)
LED.value(1)
Error: "AttributeError: 'RTC' object has no attribute 'alarm'"

mani.py:

Code: Select all

def RTC_Timer(pin):
    LED.value(0)
    time.sleep(0.5)
    LED.value(1)
    time.sleep(0.5)
    LED.value(0)
    time.sleep(0.5)
    LED.value(1)
    time.sleep(0.5)
    LED.value(0)
    time.sleep(0.5)
    LED.value(1)


#Main program
import machine
import time

LED = machine.Pin(5,machine.Pin.OUT)

rtc = machine.RTC()
rtc.irq(trigger=0, handler=RTC_Timer, wake=machine.DEEPSLEEP)
Error: AttributeError: 'RTC' object has no attribute 'irq'

thank you

User avatar
pythoncoder
Posts: 3935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: "machine.RTC" alarm + set irq

Post by pythoncoder » Tue Jan 21, 2020 7:00 pm

Unfortunately the machine.RTC documentation is misleading in that some methods are unavailable on some platforms. The ESP32 currently only supports methods datetime, init and memory.
Peter Hinch

Jerremy09
Posts: 22
Joined: Wed May 08, 2019 7:40 am

Re: "machine.RTC" alarm + set irq

Post by Jerremy09 » Tue Jan 21, 2020 8:51 pm

Hello,

thank you for answer. Unfortuantelly I didnt want to hear this, never the less, is there a way to have something similar to give me at least similar result?

Dont you know for which type of board the those RTC functions are available?

Thank you

User avatar
pythoncoder
Posts: 3935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: "machine.RTC" alarm + set irq

Post by pythoncoder » Wed Jan 22, 2020 10:02 am

I think the best approach is if you could explain what you are trying to achieve. Are you aiming to wake a board from a sleep state or merely to schedule events (e.g. callbacks) at certain future times? If the latter I can point you to simple solutions which will work on any platform and which don't involve the RTC.
Peter Hinch

Jerremy09
Posts: 22
Joined: Wed May 08, 2019 7:40 am

Re: "machine.RTC" alarm + set irq

Post by Jerremy09 » Mon Jan 27, 2020 1:56 pm

Hello all,

thank you for reaction. I need to create system which will be doing some function in scheduled way. Those functions are external modules (created by me). So for example,

I would like to do these kind of things:
1) every 1 hour read GPRS signal - run external module
2) every 1 hour read WiFi signal - run external module
3) once per day run special function - run external module
...
- it may happen that those function have to wake up board

what I already have:
an IRQ written for touch screen to be able to operate system.

Using:
Board: Lolin D32 Pro 2.0
Display: Lolin TFT 2.4

thank you

User avatar
pythoncoder
Posts: 3935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: "machine.RTC" alarm + set irq

Post by pythoncoder » Mon Jan 27, 2020 6:21 pm

Why not use the utime module? For example to schedule an operation once an hour and another once per day you might do something like this:

Code: Select all

import utime

old_hr = -1
old_day = -1
while True:
    t = utime.localtime()
    hr = t[3]
    day = t[2]
    if hr != old_hr:
        old_hr = hr
        hourly_function()
    if day != old_day:
        old_day = day
        daily_function()
    utime.sleep(60)
There are much faster and more sophisticated ways of scheduling (see uasyncio) but for a simple application like this, hardly necessary.
Peter Hinch

Jerremy09
Posts: 22
Joined: Wed May 08, 2019 7:40 am

Re: "machine.RTC" alarm + set irq

Post by Jerremy09 » Mon Jan 27, 2020 8:22 pm

Hello Peter,

thank you for your post. Unfortunately I think that "Why loop" cannot be used, because there is more things in program. Those daily "Jobs" meant to run on background as User will operate touch display. So point is that I would like to build something like "Windows Scheduler" - there will be those time-based function and for User i build "IRQ" pin (when touch display then do something).

The ideal function is "RTC.irq(*, trigger, handler=None, wake=machine.IDLE)", but do not work on ESP32 (don't we have any news related to firmware? When it will be available/ it it will be available.)

Then I found this "micropython.schedule(func, arg)", but I don´t know wheather it works and how to write it (I didn't study yet).

Thank you

Jan Vaško

User avatar
pythoncoder
Posts: 3935
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: "machine.RTC" alarm + set irq

Post by pythoncoder » Tue Jan 28, 2020 10:11 am

In which case you need to use uasyncio. There is a tutorial in this repo. Scheduling activities to run at specific times while running a GUI is easy. You will find GUI solutions for various types of display (all based on uasyncio) in my other repos.
Peter Hinch

Post Reply