From ccd5f1741e84621984aaf7bb117916197b46b051 Mon Sep 17 00:00:00 2001 From: kicer Date: Sat, 31 Jan 2026 11:26:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=97=A0=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rom/app.py | 20 +++++++++----------- src/rom/captive_http.py | 2 +- src/rom/config.py | 2 -- src/rom/display.py | 24 +++++++++++++----------- src/rom/wifi_manager.py | 6 +++--- src/rom/www/iwconfig.html | 3 ++- utils/convert_assets.sh | 2 +- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/rom/app.py b/src/rom/app.py index 0787711..28ac493 100644 --- a/src/rom/app.py +++ b/src/rom/app.py @@ -69,19 +69,17 @@ async def get_weather_data(city=None, force=False): import aiohttp # 从配置文件获取城市,如果没有提供则使用配置中的值 - if city is None: - city = config.get("city", "北京") + if not city: + city = config.get("city") or "北京" print(f"正在获取{city}天气数据...") # 从配置获取API基础URL,默认使用官方API - url = config.get("weather_api_url", "http://esp.tangofu.com/api/sd2/") - params={'uuid':uuid(), 'city':city} + url = config.get("weather_api_url", "http://esp.tangofu.com/api/ws2/") + params = {'uuid':uuid(), 'city':city} # 发送GET请求 - print('start http') async with aiohttp.ClientSession() as session: async with session.get(url, params=params) as response: - print('ACK=%s'%response.status) # 检查响应状态 if response.status == 200: # 解析JSON数据 @@ -99,7 +97,9 @@ async def get_weather_data(city=None, force=False): advice = wdata.get("advice", []) lunar = wdata.get("lunar", None) - display.update_ui(city, weather, advice, aqi, lunar, + ip = wifi_manager.get_ip() + + display.update_ui(city, weather, advice, aqi, lunar, ip, envdat={'t':t,'rh':rh,'co2':co2,'pm':pm,'ap':ap}) # 更新缓存 @@ -140,8 +140,7 @@ async def sysinfo_update_task(): task_id = "weather" await get_weather_data(force=True) last_weather = current_ticks - #todo:weather_ts = int(config.get("weather_ts", 600)) # 10min - weather_ts = int(config.get("weather_ts", 6)) # 10min + weather_ts = int(config.get("weather_ts", 600)) # 10min elif ntp_diff >= ntptime_ts * 1000: # 更新NTP时间 gc.collect() @@ -218,8 +217,7 @@ def cb_progress(data): def start(): # 初始化液晶屏 - #display.init_display(config.get("bl_mode") != "gpio") - display.init_display(False, buffer_size=5000) + display.init_display(config.get("bl_mode")=="pwm", 5120) display.brightness(int(config.get("brightness", 10))) cb_progress("WiFi connect ...") diff --git a/src/rom/captive_http.py b/src/rom/captive_http.py index 68b9756..af0c801 100644 --- a/src/rom/captive_http.py +++ b/src/rom/captive_http.py @@ -117,7 +117,7 @@ class HTTPServer(BaseServer): def login(self, params): # 从URL参数中提取表单数据 ssid = unquote(params.get(b"ssid", None)) - password = unquote(params.get(b"password", None)) + password = unquote(params.get(b"password", "")) city = unquote(params.get(b"city", None)) # 使用全局Config实例保存配置 diff --git a/src/rom/config.py b/src/rom/config.py index a3af15f..25fbd28 100644 --- a/src/rom/config.py +++ b/src/rom/config.py @@ -89,8 +89,6 @@ class Config: # 确保SSID和密码都是字符串类型且不为空 if not ssid: return False - if not password: - return False return True diff --git a/src/rom/display.py b/src/rom/display.py index 7b9f47b..85063c7 100644 --- a/src/rom/display.py +++ b/src/rom/display.py @@ -41,7 +41,7 @@ class Display: self.ui_data = {} self.ticks = 0 # 据此切换多行显示 - def init_display(self, bl_pwm=True, buffer_size=2048): + def init_display(self, bl_pwm=False, buffer_size=2048): """初始化液晶屏,默认2048够用且不易有内存碎片""" try: from machine import PWM, SPI, Pin @@ -67,7 +67,8 @@ class Display: # 初始化并清屏 self.tft.init() self.tft.fill(0) - display.show_jpg(self.bootimg, 80, 80) + self.show_jpg(self.bootimg, 80, 80) + self.message("WS2 v1.2.0 (20260131)") _print_mem() return True @@ -145,7 +146,7 @@ class Display: self.window("配置设备网络连接", tips, "portal ip: 192.168.4.1") # 更新ui数据 - def update_ui(self, city=None, weather=None, advice=None, aqi=None, lunar=None, envdat=None): + def update_ui(self, city=None, weather=None, advice=None, aqi=None, lunar=None, ip=None, envdat=None): self.ticks += 1 if self.ui_type == 'default': # 中文的城市名称 @@ -156,10 +157,11 @@ class Display: self.tft.write(self.cn_font, '??', 15,10) # 天气符号: 0-7 (未知、晴、云、雨、雷、雪、雾、风) if weather is not None and weather != self.ui_data.get('weather'): - self.show_jpg(f"/rom/images/{weather}.jpg",165,10) + self.show_jpg(f"/rom/images/{weather}.jpg",175,10) self.ui_data['weather'] = weather # 建议信息可能有很多条,需要轮换展示 if advice is not None and advice != self.ui_data.get('advice'): + if ip is not None: advice.append(ip) self.ui_data['advice'] = advice # AQI等级分成0-5级,分别对应优、良、中、差、污、恶 if aqi is not None and aqi != self.ui_data.get('aqi'): @@ -180,15 +182,15 @@ class Display: if t is not None and t != self.ui_data.get('t'): self.ui_data['t'] = t self.tft.fill_rect(35,179,40,16,0) - self.tft.draw(self.vector_font, str(t), 35,187,0xFFFF,0.5) + self.tft.draw(self.vector_font, f"{str(t)}'C", 35,187,0xFFFF,0.5) if rh is not None and rh != self.ui_data.get('rh'): self.ui_data['rh'] = rh self.tft.fill_rect(110,179,40,16,0) - self.tft.draw(self.vector_font, str(rh), 110,187,0xFFFF,0.5) + self.tft.draw(self.vector_font, f' {str(rh)}%', 110,187,0xFFFF,0.5) if pm is not None and pm != self.ui_data.get('pm'): self.ui_data['pm'] = pm self.tft.fill_rect(35,213,40,16,0) - self.tft.draw(self.vector_font, str(pm), 35,221,0xFFFF,0.5) + self.tft.draw(self.vector_font, f'{str(pm):>4s}', 35,221,0xFFFF,0.5) if co2 is not None and co2 != self.ui_data.get('co2'): # 如果co2数据存在,优先显示co2 if self.ui_data.get('ap'): @@ -196,12 +198,12 @@ class Display: self.show_jpg("/rom/images/co2.jpg",85,209) self.ui_data['co2'] = co2 self.tft.fill_rect(110,213,40,16,0) - self.tft.draw(self.vector_font, str(co2), 110,221,0xFFFF,0.5) + self.tft.draw(self.vector_font, f'{str(co2):>4s}', 110,221,0xFFFF,0.5) elif self.ui_data.get('co2') is None and ap is not None and ap != self.ui_data.get('ap'): # 没co2时候才会显示大气压 self.ui_data['ap'] = ap self.tft.fill_rect(110,213,40,16,0) - self.tft.draw(self.vector_font, str(ap), 110,221,0xFFFF,0.5) + self.tft.draw(self.vector_font, f'{str(ap):>4s}', 110,221,0xFFFF,0.5) # 处理日期 from machine import RTC y,m,d,_w,H,M,*_ = RTC().datetime() @@ -235,8 +237,8 @@ class Display: i = self.ticks % len(advice) c = advice[i] w = self.tft.write(self.cn_font, advice[i], 15,45) - if w<22*6: # fill - self.tft.fill_rect(15+w,45,22*6-w,22,0) + if w<22*7: # fill + self.tft.fill_rect(15+w,45,22*7-w,22,0) # 打印内存信息 _print_mem() diff --git a/src/rom/wifi_manager.py b/src/rom/wifi_manager.py index cd24ab8..7f82934 100644 --- a/src/rom/wifi_manager.py +++ b/src/rom/wifi_manager.py @@ -56,10 +56,10 @@ class WiFiManager: return False ssid = self.config.get("ssid") - password = self.config.get("password") + password = self.config.get("password") or "" - if not ssid or not password: - print("SSID or password is empty") + if not ssid: + print("SSID is empty") return False print(f"Trying to connect to SSID: {ssid}") diff --git a/src/rom/www/iwconfig.html b/src/rom/www/iwconfig.html index 4efbdc5..01f9a13 100644 --- a/src/rom/www/iwconfig.html +++ b/src/rom/www/iwconfig.html @@ -213,9 +213,9 @@ />