v.0.1.0 some cleanup and sorting

This commit is contained in:
tiijay
2025-10-20 18:09:35 +02:00
parent b5a30adb27
commit fb418e8bd9
12 changed files with 188 additions and 106 deletions

3
app.py
View File

@@ -1,3 +0,0 @@
from app.utils.colors import RAINBOW
print(f'app says {RAINBOW}')

View File

@@ -6,4 +6,4 @@ class Location:
self.localtime = kwargs.get('localtime', '')
def __repr__(self):
return f"Location(name='{self.name}', region='{self.region}', country='{self.country}')"
return f"Location(name='{self.name}', region='{self.region}', country='{self.country}', localtime='{self.localtime}')"

View File

@@ -31,7 +31,12 @@ def show_system_load():
print('==================')
def go():
# Run periodically
while True:
show_system_load()
time.sleep(5)
if __name__ == '__main__':
go()

View File

@@ -3,74 +3,91 @@ import network
import urequests
from ..classes.location import Location
from ..classes.weather import CurrentCondition, Weather
import json
API_KEY = '3545ce42d0ba436e8dc164532250410'
ACTUAL_WEATHER = 'https://api.weatherapi.com/v1/current.json?key={{key}}&q={{city}}&aqi=yes'
WEATHER_QUERY_MOCK = {
'location': {
'name': 'Grosshansdorf',
'region': 'Schleswig-Holstein',
'country': 'Germany',
'lat': 53.6667,
'lon': 10.2833,
'tz_id': 'Europe/Berlin',
'localtime_epoch': 1760779328,
'localtime': '2025-10-18 11:22',
},
'current': {
'last_updated_epoch': 1760778900,
'last_updated': '2025-10-18 11:15',
'temp_c': 8.3,
'temp_f': 46.9,
'is_day': 1,
'condition': {
'text': 'Sonnig',
'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png',
'code': 1000,
},
'wind_mph': 2.5,
'wind_kph': 4.0,
'wind_degree': 31,
'wind_dir': 'NNE',
'pressure_mb': 1029.0,
'pressure_in': 30.39,
'precip_mm': 0.0,
'precip_in': 0.0,
'humidity': 76,
'cloud': 0,
'feelslike_c': 8.2,
'feelslike_f': 46.8,
'windchill_c': 10.0,
'windchill_f': 50.0,
'heatindex_c': 9.9,
'heatindex_f': 49.8,
'dewpoint_c': 2.2,
'dewpoint_f': 35.9,
'vis_km': 10.0,
'vis_miles': 6.0,
'uv': 0.9,
'gust_mph': 2.8,
'gust_kph': 4.6,
'air_quality': {
'co': 163.678,
'no2': 7.278,
'o3': 47.0,
'so2': 1.178,
'pm2_5': 5.778,
'pm10': 7.178,
'us-epa-index': 1,
'gb-defra-index': 1,
},
},
}
class Wlan:
def __init__(self, ssid, password):
self.ssid = ssid
self.password = password
def mock_weather_data(self):
data = """
{
"location": {
"name": "Grosshansdorf",
"region": "Schleswig-Holstein",
"country": "Germany",
"lat": 53.6667,
"lon": 10.2833,
"tz_id": "Europe/Berlin",
"localtime_epoch": 1760779328,
"localtime": "2025-10-18 11:22"
},
"current": {
"last_updated_epoch": 1760778900,
"last_updated": "2025-10-18 11:15",
"temp_c": 8.3,
"temp_f": 46.9,
"is_day": 1,
"condition": {
"text": "Sonnig",
"icon": "//cdn.weatherapi.com/weather/64x64/day/113.png",
"code": 1000
},
"wind_mph": 2.5,
"wind_kph": 4.0,
"wind_degree": 31,
"wind_dir": "NNE",
"pressure_mb": 1029.0,
"pressure_in": 30.39,
"precip_mm": 0.0,
"precip_in": 0.0,
"humidity": 76,
"cloud": 0,
"feelslike_c": 8.2,
"feelslike_f": 46.8,
"windchill_c": 10.0,
"windchill_f": 50.0,
"heatindex_c": 9.9,
"heatindex_f": 49.8,
"dewpoint_c": 2.2,
"dewpoint_f": 35.9,
"vis_km": 10.0,
"vis_miles": 6.0,
"uv": 0.9,
"gust_mph": 2.8,
"gust_kph": 4.6,
"air_quality": {
"co": 163.678,
"no2": 7.278,
"o3": 47.0,
"so2": 1.178,
"pm2_5": 5.778,
"pm10": 7.178,
"us-epa-index": 1,
"gb-defra-index": 1
}
}
}"""
filepath = 'restapi/mock-weather.json'
try:
with open(filepath, 'r', encoding='utf-8') as file:
return json.load(file)
except OSError: # Use OSError instead of FileNotFoundError
print(f"Error: File '{filepath}' not found.")
return None
except FileNotFoundError:
print(f"Error: File '{filepath}' not found.")
return None
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON in '{filepath}': {e}")
return None
def connect(self):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
@@ -89,16 +106,18 @@ class Wlan:
print(f'query: {weather_url}')
if not test_mode:
self.connect()
r = urequests.get(weather_url)
print('Status-Code:', r.status_code)
json_resp = r.json()
print(json_resp.keys())
r.close()
else:
print('Status-Code: test_mode')
json_resp = WEATHER_QUERY_MOCK
# json_resp = WEATHER_QUERY_MOCK
json_resp = self.mock_weather_data()
loc = Location(**json_resp['location'])
cc = CurrentCondition(**json_resp['current'])

View File

@@ -10,6 +10,7 @@ end
echo "1. Copying app directory to device..."
mpremote connect port:rfc2217://localhost:4000 cp -r app :
mpremote connect port:rfc2217://localhost:4000 cp -r restapi :
echo "2. Starting main.py..."
mpremote connect port:rfc2217://localhost:4000 run main.py

View File

@@ -7,8 +7,7 @@
"type": "board-pi-pico-w",
"id": "picow",
"top": 1014.35,
"left": 3.55,
"attrs": { "env": "micropython-20241129-v1.24.1" }
"left": 3.55
},
{
"type": "wokwi-led-matrix",

Binary file not shown.

Binary file not shown.

21
main.py
View File

@@ -14,6 +14,7 @@ from app.display.fonts.font_16x16 import font_16x16
print('Hello, Pi Pico!')
def emoji_test():
display = NeoPixel_64x64()
display.clear()
# display.set_font(font_8x8)
@@ -23,7 +24,6 @@ display.clear()
display.set_font(font_16x16)
display.draw_text('ABCD', 1, 1, color=GOLD)
# try emoji
display.set_font(emoji_8x8)
display.draw_text('😀', 1, 20, color=GREEN)
@@ -38,23 +38,21 @@ display.draw_text('💛', 28, 47)
# display.set_font(emoji_16x16)
# display.draw_text('😀', 10, 20, color=ORANGE)
display.write()
show_system_load()
def main():
def weather_check():
wlan = Wlan('WOKWI-Guest', '12345678')
try:
display = NeoPixel_64x64()
display.clear()
display.draw_text('search wlan', 0, 0, color=YELLOW)
display.write()
wlan.connect()
display.clear_row(0, effect=True)
display.clear_row(0, effect=False)
# display.clear()
display.draw_text('wlan connected', 0, 0, color=RAINBOW[1])
@@ -62,14 +60,14 @@ def main():
display.draw_text('weather data', 0, 16, color=RAINBOW[1])
display.write()
w_resp = wlan.actual_weather(test_mode=False)
w_resp = wlan.actual_weather(test_mode=True)
display.clear()
display.draw_text(f'{str(w_resp.location.name)}', 0, 0, color=RAINBOW[0])
display.draw_text(f'{str(w_resp.current.temp_c)}°C', 0, 8, color=RAINBOW[1])
display.draw_text(f'{str(w_resp.current.last_updated)[-5:]}', 0, 16, color=RAINBOW[2])
display.draw_text(f'{str(w_resp.location.localtime)[-5:]}', 0, 24, color=RAINBOW[3])
display.draw_text(f'upd: {str(w_resp.current.last_updated)[-5:]}', 0, 16, color=RAINBOW[2])
display.draw_text(f'cur: {str(w_resp.location.localtime)[-5:]}', 0, 24, color=RAINBOW[3])
display.write()
@@ -80,3 +78,8 @@ def main():
print(f'Error: connection closed - {e}')
finally:
print('finally done.')
if __name__ == '__main__':
# emoji_test()
weather_check()

57
restapi/mock-weather.json Normal file
View File

@@ -0,0 +1,57 @@
{
"location": {
"name": "Grosshansdorf",
"region": "Schleswig-Holstein",
"country": "Germany",
"lat": 53.6667,
"lon": 10.2833,
"tz_id": "Europe/Berlin",
"localtime_epoch": 1760977041,
"localtime": "2025-10-20 18:17"
},
"current": {
"last_updated_epoch": 1760976900,
"last_updated": "2025-10-20 18:15",
"temp_c": 12.3,
"temp_f": 54.1,
"is_day": 0,
"condition": {
"text": "leichter Regenfall",
"icon": "//cdn.weatherapi.com/weather/64x64/night/296.png",
"code": 1183
},
"wind_mph": 12.1,
"wind_kph": 19.4,
"wind_degree": 151,
"wind_dir": "SSE",
"pressure_mb": 999.0,
"pressure_in": 29.5,
"precip_mm": 0.0,
"precip_in": 0.0,
"humidity": 77,
"cloud": 100,
"feelslike_c": 10.3,
"feelslike_f": 50.6,
"windchill_c": 10.1,
"windchill_f": 50.1,
"heatindex_c": 12.1,
"heatindex_f": 53.8,
"dewpoint_c": 7.7,
"dewpoint_f": 45.8,
"vis_km": 10.0,
"vis_miles": 6.0,
"uv": 0.0,
"gust_mph": 19.8,
"gust_kph": 31.9,
"air_quality": {
"co": 171.678,
"no2": 9.778,
"o3": 54.0,
"so2": 1.678,
"pm2_5": 10.078,
"pm10": 12.578,
"us-epa-index": 1,
"gb-defra-index": 1
}
}
}

View File

@@ -1,5 +1,6 @@
[wokwi]
version = 1
elf = "firmware/RPI_PICO-20250911-v1.26.1.uf2"
firmware = "firmware/RPI_PICO-20250911-v1.26.1.uf2"
elf = "firmware/Pico/RPI_PICO_W-20250911-v1.26.1.uf2"
firmware = "firmware/Pico/RPI_PICO_W-20250911-v1.26.1.uf2"
rfc2217ServerPort = 4000
gdbServerPort = 3333