v.0.5.4 weather_checker, more details

This commit is contained in:
tiijay
2025-11-12 13:59:07 +01:00
parent 2737b043c1
commit bf1a3ac36c
4 changed files with 61 additions and 115 deletions

View File

@@ -1,3 +1,4 @@
from app.classes.weather import Weather
from app.display.neopixel_64x64 import NeoPixel_64x64 from app.display.neopixel_64x64 import NeoPixel_64x64
from app.web.wlan import Wlan from app.web.wlan import Wlan
import app.utils.colors as colors import app.utils.colors as colors
@@ -22,7 +23,7 @@ class Weather_Checker():
self.setup_wlan() self.setup_wlan()
self.display.clear() self.display.clear()
self.display.set_font(fonts.font_3x5) # self.display.set_font(fonts.font_3x5)
ypos = 0 ypos = 0
self.display.write_text('wlan connected', 0, ypos, color=colors.RAINBOW[0]) self.display.write_text('wlan connected', 0, ypos, color=colors.RAINBOW[0])
ypos += self.display.font_height + 1 ypos += self.display.font_height + 1
@@ -30,15 +31,18 @@ class Weather_Checker():
ypos += self.display.font_height + 1 ypos += self.display.font_height + 1
self.display.write_text('weather data', 0, ypos, color=colors.RAINBOW[2]) self.display.write_text('weather data', 0, ypos, color=colors.RAINBOW[2])
w_resp = self.wlan.actual_weather(lang='de', test_mode=test_mode) w_resp: Weather = self.wlan.actual_weather(lang='de', test_mode=test_mode)
self.display.clear() self.display.clear()
ypos = 0 ypos = 0
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0]) self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
ypos += self.display.font_height + 1 ypos += self.display.font_height + 1
# self.display.set_font(fonts.font_8x8) self.display.write_text(f'{str(w_resp.location.region)}', 0, ypos, color=colors.RAINBOW[0])
self.display.set_font(fonts.font_5x7) ypos += self.display.font_height + 1
self.display.write_text(f'{str(w_resp.location.localtime)[:10]}', 0, ypos, color=colors.RAINBOW[3])
ypos += self.display.font_height + 1
self.display.write_text(f'{str(w_resp.current.temp_c)}°C', 0, ypos, color=colors.RAINBOW[1]) self.display.write_text(f'{str(w_resp.current.temp_c)}°C', 0, ypos, color=colors.RAINBOW[1])
ypos += self.display.font_height + 1 ypos += self.display.font_height + 1
self.display.write_text(f'{str(w_resp.current.condition.text)}', 0, ypos, color=colors.RAINBOW[2]) self.display.write_text(f'{str(w_resp.current.condition.text)}', 0, ypos, color=colors.RAINBOW[2])

View File

@@ -9,70 +9,11 @@ API_KEY = '3545ce42d0ba436e8dc164532250410'
ACTUAL_WEATHER_URL = 'http://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}&aqi=yes&lang={lang}' ACTUAL_WEATHER_URL = 'http://api.weatherapi.com/v1/current.json?key={API_KEY}&q={city}&aqi=yes&lang={lang}'
class Wlan: class Wlan:
def __init__(self, ssid, password): def __init__(self, ssid, password):
self.wlan = network.WLAN(network.STA_IF)
self.ssid = ssid self.ssid = ssid
self.password = password self.password = password
def mock_weather_data(self): 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
}
}
}"""
return json.loads(data)
filepath = 'restapi/mock-weather.json' filepath = 'restapi/mock-weather.json'
try: try:
with open(filepath, 'r', encoding='utf-8') as file: with open(filepath, 'r', encoding='utf-8') as file:
@@ -88,21 +29,22 @@ class Wlan:
return None return None
def connect(self): def connect(self):
wlan = network.WLAN(network.STA_IF)
wlan.active(True) self.wlan.active(True)
wlan.connect(self.ssid, self.password) self.wlan.connect(self.ssid, self.password)
while not wlan.isconnected: while not self.wlan.isconnected:
print('connecting, please wait ...') print('connecting, please wait ...')
time.sleep(1) time.sleep(1)
print('connected! IP=', wlan.ifconfig()[0]) print('connected! IP=', self.wlan.ifconfig()[0])
def actual_weather(self, city='grosshansdorf', lang='de', test_mode=False) -> Weather: def actual_weather(self, city='grosshansdorf', lang='de', test_mode=False) -> Weather:
weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang) weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang)
if not test_mode: if not test_mode:
self.connect() if not self.wlan.isconnected:
self.connect()
print(f'query: {weather_url}') print(f'query: {weather_url}')
r = urequests.get(weather_url) r = urequests.get(weather_url)

View File

@@ -1,57 +1,57 @@
{ {
"location": { "location": {
"name": "Grosshansdorf", "name": "Columbus",
"region": "Schleswig-Holstein", "region": "Ohio",
"country": "Germany", "country": "United States of America",
"lat": 53.6667, "lat": 39.9611,
"lon": 10.2833, "lon": -82.9989,
"tz_id": "Europe/Berlin", "tz_id": "America/New_York",
"localtime_epoch": 1761383733, "localtime_epoch": 1762951576,
"localtime": "2025-10-25 11:15" "localtime": "2025-11-12 07:46"
}, },
"current": { "current": {
"last_updated_epoch": 1761383700, "last_updated_epoch": 1762951500,
"last_updated": "2025-10-25 11:15", "last_updated": "2025-11-12 07:45",
"temp_c": 10.2, "temp_c": 2.8,
"temp_f": 50.4, "temp_f": 37.0,
"is_day": 1, "is_day": 1,
"condition": { "condition": {
"text": "Overcast", "text": "Partly cloudy",
"icon": "//cdn.weatherapi.com/weather/64x64/day/122.png", "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
"code": 1009 "code": 1003
}, },
"wind_mph": 17.7, "wind_mph": 11.6,
"wind_kph": 28.4, "wind_kph": 18.7,
"wind_degree": 208, "wind_degree": 235,
"wind_dir": "SSW", "wind_dir": "SW",
"pressure_mb": 991.0, "pressure_mb": 1012.0,
"pressure_in": 29.26, "pressure_in": 29.88,
"precip_mm": 0.03, "precip_mm": 0.0,
"precip_in": 0.0, "precip_in": 0.0,
"humidity": 82, "humidity": 67,
"cloud": 100, "cloud": 75,
"feelslike_c": 6.9, "feelslike_c": -1.5,
"feelslike_f": 44.5, "feelslike_f": 29.2,
"windchill_c": 5.4, "windchill_c": -2.1,
"windchill_f": 41.8, "windchill_f": 28.2,
"heatindex_c": 9.0, "heatindex_c": 0.4,
"heatindex_f": 48.3, "heatindex_f": 32.6,
"dewpoint_c": 6.3, "dewpoint_c": -2.2,
"dewpoint_f": 43.4, "dewpoint_f": 28.0,
"vis_km": 10.0, "vis_km": 16.0,
"vis_miles": 6.0, "vis_miles": 9.0,
"uv": 0.1, "uv": 0.0,
"gust_mph": 25.3, "gust_mph": 18.3,
"gust_kph": 40.7, "gust_kph": 29.5,
"air_quality": { "air_quality": {
"co": 155.678, "co": 216.85,
"no2": 9.678, "no2": 8.35,
"o3": 45.0, "o3": 54.0,
"so2": 2.078, "so2": 2.75,
"pm2_5": 4.678, "pm2_5": 9.45,
"pm10": 6.178, "pm10": 9.55,
"us-epa-index": 1, "us-epa-index": 1,
"gb-defra-index": 1 "gb-defra-index": 1
} }
} }
} }

View File

@@ -1,4 +1,4 @@
@city = "grosshansdorf" @city = "columbus"
@lang = en @lang = en
@forecast_days = 3 @forecast_days = 3
# could be 60(mins) or 24(day) # could be 60(mins) or 24(day)