Reputation: 31
While I have been writing programs since high school (that's really old school to some of you) I am still quite new to [Micro]Python.
I understand this question may have been answered elsewhere, but no suitable MicroPython solutions have shown up in my searches (solutions are usually Arduino or full Python).
Current device and firmware:
Thonny IDE
ESP32 Lilygo TTGO T-Display
This model has a display and battery charger/monitor circuit onboard (reading it is another matter) - from Aliexpress.
Lilygo's documentation for MicroPython is non-existant.
MicroPython v1.18-128-g2ea21abae-dirty on 2022-02-12
TTGO T-Display driver included, russhughes version from github
Besides a salvaged flat lithium battery and using USB connection, no other devices added.
No datetime module in firmware (so no datetime.datetime.now() options)
utime seems to use the time from the COMPUTER(!!) to report time values (8 element tuple) when I use it in Thonny and not from the ESP32 (the tuple should all start at 0). And forget using help('[object name]'), it treats the term as a string and reports all the stringy things you can do (help('modules') is the only one that works otherwise "object [term] is of type str") <- I've tried with and without the quotes.
I am wanting to get the standalone uptime since the ESP32 started running the MicroPython program (I.E. 0 seconds starting point) so I can time how long the battery will last - I can't seem to find a reliable coding to read the battery charge (no two seem to fully agree on a method, appears to be great discussion on accuracy, and mostly in Arduino/C code). Lilygo's Arduino/C battery code is hidden in their demo firmware - both shipped and flashable from github, so no translation from that (at least by me).
I want to display the uptime on the tft display, serial monitor, and save it to a file every minute so I can retrieve it on next boot. The main program is a webserver (modified codemee github ESPWebServer). Once I have a decent average time, I can use that for a safe shutdown time.
If there is a transferable version of datetime.py (udatetime.py?) that I could put on the local file system and import, I think that this would be the best option but any option that lets me time the uptime is welcomed.
Upvotes: 0
Views: 2031
Reputation: 31
Simple solution for uptime timer
from machine import Timer
import time
timeInit = time.time()
def printTime(self):
timeDiff = time.time()-timeInit
(minutes, seconds) = divmod(timeDiff, 60)
(hours, minutes) = divmod(minutes, 60)
(days,hours) = divmod(hours, 24)
#what to do comes next, I serial printed it for now
print(str(days)+":"+f"{hours:02d}"+":"+f"{minutes:02d}"+":"+f"{seconds:02d}")
timer=Timer(-1)
delay = 10 #in seconds
timer.init(period=delay*1000, mode=Timer.PERIODIC, callback=printTime)
#default is a dummy placeholder,
#the timer callback sends a 'self' value which caused an error during testing
printTime('default')
Upvotes: 0
Reputation: 31
@nekomatic: When I used utime while Thonny was running, the tuple that was reported was based on the time from my computer (that day's information as of the time of processing)
As I stated earlier, I am quite new to [Micro]Python. I didn't know about machine.RTC.
help() is quite useless on the several firmwares I have tried ('offical' MicroPython and the variants that include various drivers). help('modules') lists the modules but help('[module name]') is treated as asking about a string, so no information is given about the modules. Without the quotes, it generates an error.
Thanks to your link, I added
import machine
rtc = machine.RTC()
print(rtc.datetime())
and it works (reports the ESP32's time relative to January 1st 2000), but rtc.now() generates an error
print(rtc.now())
AttributeError: 'RTC' object has no attribute 'now'
This is a start and thank you.
Note: after repetitive reruns/restarts, the information returned flip flops between the ESP32 and the PC (sometimes starts with (2000, 1, 1), others (2022, 7, 8) as of today). So as a standalone it should be only the ESP32's datetime info as it can't access the computer's data.)
Also appears time.time() is available.
Now to figure out how to call a write to file function every minute without sched, threading, and other useful full Python modules.
Upvotes: 0