v.0.8.2 sync_ntp_time_task

This commit is contained in:
tiijay
2025-11-18 16:45:13 +00:00
parent a748117ac5
commit c817f0330a
3 changed files with 145 additions and 99 deletions

View File

@@ -17,7 +17,6 @@
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-python.python",
"frhtylcn.pythonsnippets",
"kevinrose.vsc-python-indent",

View File

@@ -1,95 +1,129 @@
import time
import ntptime # type: ignore
import ntptime # type: ignore
_UTC_OFFSET: int = 1 * 3600 # +1 or +2 hours for CEST, adjust for your timezone
def local_time_with_offset():
# Set timezone offset (in seconds)
current_time = time.time() + _UTC_OFFSET
return time.localtime(current_time)
# Set timezone offset (in seconds)
current_time = time.time() + _UTC_OFFSET
return time.localtime(current_time)
def get_datetime_string(format='full'):
"""
Return date/time as string with different formats
def get_datetime_string(format="full"):
"""
Return date/time as string with different formats
Args:
format: "full", "date", "time", "short", "ticks"
"""
try:
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
Args:
format: "full", "date", "time", "short", "ticks"
"""
try:
year, month, day, hour, minute, second, weekday, yearday = (
local_time_with_offset()
)
if format == 'full':
return f'{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}'
elif format == 'date':
return f'{year:04d}-{month:02d}-{day:02d}'
elif format == 'time':
return f'{hour:02d}:{minute:02d}:{second:02d}'
elif format == 'short':
return f'{month:02d}/{day:02d} {hour:02d}:{minute:02d}'
else:
return f'Ticks: {time.ticks_ms()} ms'
if format == "full":
return (
f"{year:04d}-{month:02d}-{day:02d} {hour:02d}:{minute:02d}:{second:02d}"
)
elif format == "date":
return f"{year:04d}-{month:02d}-{day:02d}"
elif format == "time":
return f"{hour:02d}:{minute:02d}:{second:02d}"
elif format == "short":
return f"{month:02d}/{day:02d} {hour:02d}:{minute:02d}"
else:
return f"Ticks: {time.ticks_ms()} ms"
except:
return f'Ticks: {time.ticks_ms()} ms'
except:
return f"Ticks: {time.ticks_ms()} ms"
def get_german_datetime():
"""Return German date and time"""
try:
year, month, day, hour, minute, second, weekday, yearday = local_time_with_offset()
"""Return German date and time"""
try:
year, month, day, hour, minute, second, weekday, yearday = (
local_time_with_offset()
)
weekdays = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']
weekday_name = weekdays[weekday]
weekdays = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]
weekday_name = weekdays[weekday]
return f'{weekday_name}.{day:02d}.{month:02d}.{year} {hour:02d}:{minute:02d}'
return f"{weekday_name}.{day:02d}.{month:02d}.{year} {hour:02d}:{minute:02d}"
except:
ticks = time.ticks_ms() // 1000
return f'Zeit: {ticks} sek'
except:
ticks = time.ticks_ms() // 1000
return f"Zeit: {ticks} sek"
def get_german_timestamp_short():
"""Get German timestamp with short months (for Wokwi)"""
ticks = time.ticks_ms()
day = (ticks // 86400000) % 31 + 1
month = (ticks // 2592000000) % 12 + 1
hour = (ticks // 3600000) % 24
minute = (ticks // 60000) % 60
"""Get German timestamp with short months (for Wokwi)"""
ticks = time.ticks_ms()
day = (ticks // 86400000) % 31 + 1
month = (ticks // 2592000000) % 12 + 1
hour = (ticks // 3600000) % 24
minute = (ticks // 60000) % 60
months = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez']
month_name = months[month - 1]
months = [
"Jan",
"Feb",
"Mär",
"Apr",
"Mai",
"Jun",
"Jul",
"Aug",
"Sep",
"Okt",
"Nov",
"Dez",
]
month_name = months[month - 1]
return f'{day}.{month_name} {hour:02d}:{minute:02d}'
return f"{day}.{month_name} {hour:02d}:{minute:02d}"
def get_german_time_ticks():
"""Get German time using ticks (works in Wokwi)"""
ticks = time.ticks_ms()
"""Get German time using ticks (works in Wokwi)"""
ticks = time.ticks_ms()
# Simulate time progression
total_seconds = ticks // 1000
hours = (total_seconds // 3600) % 24
minutes = (total_seconds // 60) % 60
seconds = total_seconds % 60
# Simulate time progression
total_seconds = ticks // 1000
hours = (total_seconds // 3600) % 24
minutes = (total_seconds // 60) % 60
seconds = total_seconds % 60
return f'{hours:02d}:{minutes:02d}:{seconds:02d}'
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
def get_german_date_ticks():
"""Get German date using ticks"""
ticks = time.ticks_ms()
"""Get German date using ticks"""
ticks = time.ticks_ms()
# Simulate date progression (starting from Jan 15, 2024)
days_passed = ticks // (24 * 3600 * 1000)
day = 15 + (days_passed % 28) # Simple month simulation
month = 1 + (days_passed // 28) % 12
year = 2024 + (days_passed // (28 * 12))
# Simulate date progression (starting from Jan 15, 2024)
days_passed = ticks // (24 * 3600 * 1000)
day = 15 + (days_passed % 28) # Simple month simulation
month = 1 + (days_passed // 28) % 12
year = 2024 + (days_passed // (28 * 12))
months = ['JAN', 'FEB', 'MAR', 'APR', 'MAI', 'JUN', 'JUL', 'AUG', 'SEP', 'OKT', 'NOV', 'DEZ']
month_name = months[month - 1]
months = [
"JAN",
"FEB",
"MAR",
"APR",
"MAI",
"JUN",
"JUL",
"AUG",
"SEP",
"OKT",
"NOV",
"DEZ",
]
month_name = months[month - 1]
return f"{day:02d}.{month_name}.{str(year)[-2:]}"
return f'{day:02d}.{month_name}.{str(year)[-2:]}'
def sync_ntp_time():
try:

77
main.py
View File

@@ -2,59 +2,72 @@ from app.display import NeoPixel_64x64
from app.display.fonts import font_5x7
from app.tryout import Font_Checker, Weather_Checker, Emoji_Checker
from app.utils import show_system_load
from app.utils import sync_ntp_time, get_datetime_string, get_german_datetime # Time-related functions
from app.utils import (
sync_ntp_time,
get_datetime_string,
get_german_datetime,
) # Time-related functions
from app.utils import SimpleCounter
from app.web import Wlan
import time
import uasyncio as asyncio # type: ignore
import uasyncio as asyncio # type: ignore
from app.web import Wlan
CITY_LIST: list[str]= sorted(["Großhansdorf","Columbus", "London", "Ebeltoft", "Tokio"])
CITY_LIST: list[str] = sorted(
["Großhansdorf", "Columbus", "London", "Ebeltoft", "Tokio"]
)
async def weather_check_task(weather_checker:Weather_Checker):
async def weather_check_task(weather_checker: Weather_Checker):
while True:
for city in CITY_LIST:
weather_checker.check(city=city, lang="de", test_mode=False)
print(f"Checked {city}")
await asyncio.sleep(3 * 60) # Non-blocking sleep
async def print_time_task() -> None:
simpleCnt: SimpleCounter = SimpleCounter()
while True:
dt1:int = get_datetime_string()
dt2:int = get_german_datetime()
simpleCnt+=1
print(f"print_time_task running... {simpleCnt.value%16} {dt1} {dt2}")
dt1: int = get_datetime_string()
dt2: int = get_german_datetime()
simpleCnt += 1
print(f"print_time_task running... {simpleCnt.value % 16} {dt1} {dt2}")
await asyncio.sleep(10)
async def main(weather_checker:Weather_Checker)->None:
async def sync_ntp_time_task() -> None:
while True:
sync_ntp_time()
await asyncio.sleep(60)
async def main(weather_checker: Weather_Checker) -> None:
# Run both tasks concurrently
await asyncio.gather(weather_check_task(weather_checker), print_time_task())
await asyncio.gather(
sync_ntp_time_task(), weather_check_task(weather_checker), print_time_task()
)
# Programm Startpunkt
if __name__ == '__main__':
display = NeoPixel_64x64()
wlan: Wlan = Wlan( )
wlan.connect( ssid="Wokwi-Wlan", password="12345678")
if __name__ == "__main__":
display = NeoPixel_64x64()
wlan: Wlan = Wlan()
wlan.connect(ssid="Wokwi-Wlan", password="12345678")
sync_ntp_time()
# font_checker : Font_Checker = Font_Checker( display)
# font_checker.fonts_check(pretty=False)
# font_checker : Font_Checker = Font_Checker( display)
# font_checker.fonts_check(pretty=False)
# emoji_checker : Emoji_Checker = Emoji_Checker(display)
# emoji_checker.check()
# emoji_checker : Emoji_Checker = Emoji_Checker(display)
# emoji_checker.check()
# tryout.weather_check(display, test_mode=False)
display.set_font(font_5x7)
weather_checker: Weather_Checker = Weather_Checker(display=display)
# tryout.weather_check(display, test_mode=False)
display.set_font(font_5x7)
weather_checker: Weather_Checker = Weather_Checker( display=display)
# while True:
# for city in sorted(CITY_LIST):
# weather_checker.check(city=city, lang="de", test_mode=False)
# time.sleep(5*60)
# while True:
# for city in sorted(CITY_LIST):
# weather_checker.check(city=city, lang="de", test_mode=False)
# time.sleep(5*60)
# show_system_load()
asyncio.run(main(weather_checker))
# show_system_load()
asyncio.run(main(weather_checker))