v.0.5.4 weather_checker, more details
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from app.classes.weather import Weather
|
||||
from app.display.neopixel_64x64 import NeoPixel_64x64
|
||||
from app.web.wlan import Wlan
|
||||
import app.utils.colors as colors
|
||||
@@ -22,7 +23,7 @@ class Weather_Checker():
|
||||
self.setup_wlan()
|
||||
|
||||
self.display.clear()
|
||||
self.display.set_font(fonts.font_3x5)
|
||||
# self.display.set_font(fonts.font_3x5)
|
||||
ypos = 0
|
||||
self.display.write_text('wlan connected', 0, ypos, color=colors.RAINBOW[0])
|
||||
ypos += self.display.font_height + 1
|
||||
@@ -30,15 +31,18 @@ class Weather_Checker():
|
||||
ypos += self.display.font_height + 1
|
||||
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()
|
||||
|
||||
ypos = 0
|
||||
self.display.write_text(f'{str(w_resp.location.name)}', 0, ypos, color=colors.RAINBOW[0])
|
||||
ypos += self.display.font_height + 1
|
||||
# self.display.set_font(fonts.font_8x8)
|
||||
self.display.set_font(fonts.font_5x7)
|
||||
self.display.write_text(f'{str(w_resp.location.region)}', 0, ypos, color=colors.RAINBOW[0])
|
||||
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])
|
||||
ypos += self.display.font_height + 1
|
||||
self.display.write_text(f'{str(w_resp.current.condition.text)}', 0, ypos, color=colors.RAINBOW[2])
|
||||
|
||||
@@ -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}'
|
||||
class Wlan:
|
||||
def __init__(self, ssid, password):
|
||||
self.wlan = network.WLAN(network.STA_IF)
|
||||
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
|
||||
}
|
||||
}
|
||||
}"""
|
||||
return json.loads(data)
|
||||
|
||||
filepath = 'restapi/mock-weather.json'
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as file:
|
||||
@@ -88,21 +29,22 @@ class Wlan:
|
||||
return None
|
||||
|
||||
def connect(self):
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
wlan.connect(self.ssid, self.password)
|
||||
|
||||
self.wlan.active(True)
|
||||
self.wlan.connect(self.ssid, self.password)
|
||||
|
||||
while not wlan.isconnected:
|
||||
while not self.wlan.isconnected:
|
||||
print('connecting, please wait ...')
|
||||
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:
|
||||
weather_url = ACTUAL_WEATHER_URL.format(API_KEY=API_KEY, city=city, lang=lang)
|
||||
|
||||
if not test_mode:
|
||||
self.connect()
|
||||
if not self.wlan.isconnected:
|
||||
self.connect()
|
||||
|
||||
print(f'query: {weather_url}')
|
||||
r = urequests.get(weather_url)
|
||||
|
||||
Reference in New Issue
Block a user