Timer

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Nikunj_Vadher
Posts: 14
Joined: Mon Dec 10, 2018 6:37 am

Timer

Post by Nikunj_Vadher » Fri Mar 01, 2019 8:06 am

I am exploring micropython.
I came across a task I need to perform 2 separate operation at different interval.
For example:

1st task at interval of 5 second
2nd task at interval of 45 minutes.

My question is how to create 2 different timers running on different operation and how to put resolution of minutes in timer ?

Thank You.

larsks
Posts: 22
Joined: Mon Feb 13, 2017 5:27 pm

Re: Timer

Post by larsks » Fri Mar 01, 2019 1:06 pm

You can create multiple virtual timers as described in the docs at https://docs.micropython.org/en/latest/ ... tml#timers. For a 5-second timer that would look something like:

Code: Select all

from machine import Timer

def t1_callback_function(t):
	print('this is timer1')

t1 = Timer(-1)
t1.init(period=5000, mode=Timer.PERIODIC, callback=t1_callback_function)
For a 45 minute timer, you're going to need to count minutes by yourself. Something like:

Code: Select all

from machine import Timer

def t2_callback_function(t):
	global t2_counter
	t2_counter -= 1
	if t2_counter == 0:
		t2_counter = 45
		print('this is timer2')

t2_counter = 45
t2 = Timer(-1)
t2.init(period=60000, mode=Timer.PERIODIC, callback=t2_callback_function)
In the above, we are calling the callback function once/minute. It decrements the global t2_counter variable until it reaches 0, then it resets the counter and performs the target action.

User avatar
emtee
Posts: 15
Joined: Thu Jun 14, 2018 4:55 pm
Location: West Kootenay, BC, Canada

Re: Timer - periodic limit

Post by emtee » Wed Mar 06, 2019 10:07 pm

Hey larsks,
You seem to be implying that there is a limit for the length of the Timer period. I am trying to use a Timer with a period of 60 minutes and I am getting inconsistent results. Double-checked the documentation and there doesn't appear to be any mention that a Timer has a limit of (for example) 600,000 milliseconds (i.e. 10 minutes).

Is there a limit for the Timer? If so, how many milliseconds?

Post Reply