[teensy 41.] time() function problem when booting from usb

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
wbode
Posts: 2
Joined: Mon Mar 14, 2022 6:41 pm

[teensy 41.] time() function problem when booting from usb

Post by wbode » Mon Mar 14, 2022 7:13 pm

Hi, I'm new here, I just some days ago started to do some tests with micropython on teensy 4.1.
I successfully burned latest hex. I then started testing with Thonny. But now have serious problem with time() function.
My code uses WDT, reads an ADS1115 and sends mqtt data in intervals. This works perfect when starting main from Thonny IDE. But after disconnecting und reconnecting USB Port, I found that time is not updating. Here is a small testcode without mqtt and other overhead. Starting this code from IDE, if-clause enters correctly, but disconnecting und reconnecting usb, if-clause never gets True!
Is this a firmware bug, or maybe a problem with usb power?.....
Would be fine to get some help on this!

Code: Select all


from utime import time, sleep
from machine import Pin, Signal, PWM, ADC, I2C, SPI, WDT, reset
import network

wdtfeedtick=3

wdt=WDT(timeout=20000)
wdt.feed() 
led = Pin(8, Pin.OUT)
led.value(1)
sleep(3)
led.value(0)

def main():
    wdt.feed()
#     print("LAN aktivieren")
#     lan=network.LAN()
#     lan.active(True)
#     sleep(3)
#     print(lan.ifconfig())
    
    wdtfeedevent=time()+wdtfeedtick
    print(wdtfeedevent)
    while True:
        led.value(0)
        sleep(.15)
        if time() > wdtfeedevent:
            led.value(1)
            wdtfeedevent= time()+wdtfeedtick
            print("wdt.feed")
            sleep(1)
            wdt.feed()
            led.value(0)
        led.value(1)
        sleep(.15)
        print(time())


if __name__ == '__main__':
      #Catch exceptions,stop program if interrupted accidentally in the 'try'
  main()


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

Re: [teensy 41.] time() function problem when booting from usb

Post by Roberthh » Mon Mar 14, 2022 7:31 pm

I can confirm this situation, which seems a little bit odd. The value for time() is taken from the RTC, which is started by Thonny. You can work around it by creating a RTC object yourself.

from machine import RTC
rtc=RTC()

I can add a mechanism to start the RTC at system start.

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

Re: [teensy 41.] time() function problem when booting from usb

Post by Roberthh » Mon Mar 14, 2022 8:51 pm

I just noticed that there is a little glitch. If you do not set the RTC time, time.time() delivers strange large numbers. If you set the time with rtc.datetime(), numbers are more reasonable.

wbode
Posts: 2
Joined: Mon Mar 14, 2022 6:41 pm

Re: [teensy 41.] time() function problem when booting from usb

Post by wbode » Tue Mar 15, 2022 7:29 pm

Thanks for your quick help :)

Post Reply