Time if times

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Time if times

Post by deatz » Mon Oct 04, 2021 9:25 pm

Good day,

I am new to MicroPython and Pico.

I got my test script to run in Python only to realize that MicroPython deals with dates and time differently, so I worked in my uPython IDE (Thonny) to get it working with those time constraints, moved to the Pico as the interpreter and now it doesn't like the time again.

I realize the Pico doesn't have realtime natively, my question is, once I get the RTC talking to the Pico, will it then address the time as I have it, or do I need to change it up for the Pico and if so, how do I know what the Pico will take or not that uPython offers? The code below works in the Thonny interpreter but not with the Pico interpreter.

Lastly, is there a better IDE to work with, I can't get the VS addon to work for some reason and while Thonny works, it gave me no warning of this issue.

TIA

from time import sleep
from time import localtime

i=0

while True:
now = localtime()
now_hour = now.tm_hour
now_min = now.tm_min
wd = now.tm_wday
start_h = 14
start_m = 48
end_h = 15
end_m = 50


if 0 <= wd < 6 and start_h <= now_hour <= end_h and start_m <= now_min < end_m:

if i <1:

print("LEDs STARTUP!")
sleep(5)
i = i + 1
else:
print("LEDs ON!!")

else:
continue
# print("LEDs OFF")

fdufnews
Posts: 76
Joined: Mon Jul 25, 2016 11:31 am

Re: Time if times

Post by fdufnews » Tue Oct 05, 2021 3:17 pm

The code below works in the Thonny interpreter but not with the Pico interpreter.
There is only one interpreter. When using Thonny, the interpreter is the one of the Pico that you are executing. Thonny is just a super console.
But, when you connect the Pico to Thonny, Thonny sends the current time to the Pico so it can sets is RTC to the "correct time".

As the Pico has no RTC, at startup its own RTC is set to 00:00. And later you'll work with a time relative to the moment you powered the Pico.

If you add an external RTC, you'll be able to have the correct time.

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Wed Oct 06, 2021 2:51 am

Good day, thank you for the reply, I need to clarify I think.

The code will work without the Pico connected and Thonny is using Python 3.9 as the interpreter (under "Run->Select Interpreter->The same interpreter which runs Thonny (default))

But when I connect the Pico and use it as the "interpreter", it will not run and throws the error:

Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'tuple' object has no attribute 'tm_hour'

Any help would be appreciated.

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Time if times

Post by hippy » Wed Oct 06, 2021 11:12 am

deatz wrote:
Wed Oct 06, 2021 2:51 am
The code will work without the Pico connected and Thonny is using Python 3.9 as the interpreter (under "Run->Select Interpreter->The same interpreter which runs Thonny (default))

But when I connect the Pico and use it as the "interpreter", it will not run and throws the error:

Traceback (most recent call last):
File "<stdin>", line 8, in <module>
AttributeError: 'tuple' object has no attribute 'tm_hour'

Any help would be appreciated.
So basically, runs on a host, Linux, Windows, etc, but not on a Pico. That's because the implementations of Python's time module and MicroPython's time/utime are different.

If you want to run the same code on both you will need to determine at run-time whether the code is running under Python or MicroPython and then have separate code or a means to provide the code with what it expects from what MicroPython provides.

There are a variety of ways to tell if code is running under Python or MicroPython. I have tended to favour -

Code: Select all

import sys
if len(sys.argv) == 0 : MicroPython
else                  : Python

Code: Select all

import sys; IS_MICROPYTHON = len(sys.argv) == 0

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Wed Oct 06, 2021 11:24 pm

That does make sense, and I really only want it to run on the Pico, but I have yet to find an explanation of Utime that will allow the arguments I need. Essentially, I need to pull the current date and time, determine if it is a weekday and then at a certain time of day, between two times. When those parameters are met, drop an action. I was working on it without the Pico when I didn't have one available and of course when loaded it wouldn't run. If I am correct I will need to use "Utime" on the Pico? If so, after I pull a current time/date how do I parse out the day of week and just time of day. I found out how to do it with "datetime" and "time" and "calendar" but I haven't found documentation for Utime to pull a parsed component like the .tm_hour, .tm_min, and tm_weekday equivalents in "Utime".

Ima keep after it, seems like I've solved this three times now until I get the Pico involved :lol:

Thanks for all the input, I love to learn!

DeatZ

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

Re: Time if times

Post by pythoncoder » Thu Oct 07, 2021 8:40 am

Perhaps my schedule module might help. This works in a similar way to Unix cron: you can do things like schedule a callback to run on weekdays at 10:30.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Time if times

Post by Roberthh » Thu Oct 07, 2021 10:01 am

@deatz You can use time.localtime(), time.gmtime() or from the machine.RTC module datetime(). All provide the full date & time and the weekday. See also https://docs.micropython.org/en/latest/ ... .localtime and https://docs.micropython.org/en/latest/ ... achine.RTC.
Since there is no battery backed Clock in the Pico, you have to set the proper time/date after boot.

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Time if times

Post by karfas » Thu Oct 07, 2021 8:10 pm

I might be completely wrong (I never used a Pico), but what is wrong with the time module that seems to be part of the standard library ?
https://docs.micropython.org/en/latest/ ... /time.html

For your application, it's also easy to calculate the time you need from the epoch time returned by time.time().

Regards,
Thomas
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

deatz
Posts: 10
Joined: Mon Oct 04, 2021 8:57 pm

Re: Time if times

Post by deatz » Thu Oct 07, 2021 10:34 pm

Finally got it, the Pico/uPython combination apparently is particular about how to parse/pull a snippet from the time function.

now = localtime()
now_hour = now[3]
now_min = now[4]
wd = now[6]

Why oh Why can't there be just a little more continuity between Python-family libraries..

Or, I'm just such a noob I can't put it all together yet, which is most likely the case.

Now, I have to figure out why it's a day behind for the day of week; year, month, date, time, year day, all good, but i thinks it's Wednesday....
Once I have that, I'll tie an RTC to it (thanks for the link Roberthh) and have what I need, I hope...

Thanks for all your input, each one helped me down the path.

Have a blessed day!
DeatZ

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

Re: Time if times

Post by pythoncoder » Sat Oct 09, 2021 9:48 am

You seem to have found a bug there. On a Pyboard, on the Unix build and on CPython the day of the week is 5 (Saturday, with 0 being Monday). On the Pico it is 4.

I have raised this issue.
Peter Hinch
Index to my micropython libraries.

Post Reply