支持无密码路由器
This commit is contained in:
@@ -69,19 +69,17 @@ async def get_weather_data(city=None, force=False):
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
# 从配置文件获取城市,如果没有提供则使用配置中的值
|
# 从配置文件获取城市,如果没有提供则使用配置中的值
|
||||||
if city is None:
|
if not city:
|
||||||
city = config.get("city", "北京")
|
city = config.get("city") or "北京"
|
||||||
|
|
||||||
print(f"正在获取{city}天气数据...")
|
print(f"正在获取{city}天气数据...")
|
||||||
# 从配置获取API基础URL,默认使用官方API
|
# 从配置获取API基础URL,默认使用官方API
|
||||||
url = config.get("weather_api_url", "http://esp.tangofu.com/api/sd2/")
|
url = config.get("weather_api_url", "http://esp.tangofu.com/api/ws2/")
|
||||||
params={'uuid':uuid(), 'city':city}
|
params = {'uuid':uuid(), 'city':city}
|
||||||
|
|
||||||
# 发送GET请求
|
# 发送GET请求
|
||||||
print('start http')
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.get(url, params=params) as response:
|
async with session.get(url, params=params) as response:
|
||||||
print('ACK=%s'%response.status)
|
|
||||||
# 检查响应状态
|
# 检查响应状态
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
# 解析JSON数据
|
# 解析JSON数据
|
||||||
@@ -99,7 +97,9 @@ async def get_weather_data(city=None, force=False):
|
|||||||
advice = wdata.get("advice", [])
|
advice = wdata.get("advice", [])
|
||||||
lunar = wdata.get("lunar", None)
|
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})
|
envdat={'t':t,'rh':rh,'co2':co2,'pm':pm,'ap':ap})
|
||||||
|
|
||||||
# 更新缓存
|
# 更新缓存
|
||||||
@@ -140,8 +140,7 @@ async def sysinfo_update_task():
|
|||||||
task_id = "weather"
|
task_id = "weather"
|
||||||
await get_weather_data(force=True)
|
await get_weather_data(force=True)
|
||||||
last_weather = current_ticks
|
last_weather = current_ticks
|
||||||
#todo:weather_ts = int(config.get("weather_ts", 600)) # 10min
|
weather_ts = int(config.get("weather_ts", 600)) # 10min
|
||||||
weather_ts = int(config.get("weather_ts", 6)) # 10min
|
|
||||||
elif ntp_diff >= ntptime_ts * 1000:
|
elif ntp_diff >= ntptime_ts * 1000:
|
||||||
# 更新NTP时间
|
# 更新NTP时间
|
||||||
gc.collect()
|
gc.collect()
|
||||||
@@ -218,8 +217,7 @@ def cb_progress(data):
|
|||||||
|
|
||||||
def start():
|
def start():
|
||||||
# 初始化液晶屏
|
# 初始化液晶屏
|
||||||
#display.init_display(config.get("bl_mode") != "gpio")
|
display.init_display(config.get("bl_mode")=="pwm", 5120)
|
||||||
display.init_display(False, buffer_size=5000)
|
|
||||||
display.brightness(int(config.get("brightness", 10)))
|
display.brightness(int(config.get("brightness", 10)))
|
||||||
cb_progress("WiFi connect ...")
|
cb_progress("WiFi connect ...")
|
||||||
|
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ class HTTPServer(BaseServer):
|
|||||||
def login(self, params):
|
def login(self, params):
|
||||||
# 从URL参数中提取表单数据
|
# 从URL参数中提取表单数据
|
||||||
ssid = unquote(params.get(b"ssid", None))
|
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))
|
city = unquote(params.get(b"city", None))
|
||||||
|
|
||||||
# 使用全局Config实例保存配置
|
# 使用全局Config实例保存配置
|
||||||
|
|||||||
@@ -89,8 +89,6 @@ class Config:
|
|||||||
# 确保SSID和密码都是字符串类型且不为空
|
# 确保SSID和密码都是字符串类型且不为空
|
||||||
if not ssid:
|
if not ssid:
|
||||||
return False
|
return False
|
||||||
if not password:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class Display:
|
|||||||
self.ui_data = {}
|
self.ui_data = {}
|
||||||
self.ticks = 0 # 据此切换多行显示
|
self.ticks = 0 # 据此切换多行显示
|
||||||
|
|
||||||
def init_display(self, bl_pwm=True, buffer_size=2048):
|
def init_display(self, bl_pwm=False, buffer_size=2048):
|
||||||
"""初始化液晶屏,默认2048够用且不易有内存碎片"""
|
"""初始化液晶屏,默认2048够用且不易有内存碎片"""
|
||||||
try:
|
try:
|
||||||
from machine import PWM, SPI, Pin
|
from machine import PWM, SPI, Pin
|
||||||
@@ -67,7 +67,8 @@ class Display:
|
|||||||
# 初始化并清屏
|
# 初始化并清屏
|
||||||
self.tft.init()
|
self.tft.init()
|
||||||
self.tft.fill(0)
|
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()
|
_print_mem()
|
||||||
return True
|
return True
|
||||||
@@ -145,7 +146,7 @@ class Display:
|
|||||||
self.window("配置设备网络连接", tips, "portal ip: 192.168.4.1")
|
self.window("配置设备网络连接", tips, "portal ip: 192.168.4.1")
|
||||||
|
|
||||||
# 更新ui数据
|
# 更新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
|
self.ticks += 1
|
||||||
if self.ui_type == 'default':
|
if self.ui_type == 'default':
|
||||||
# 中文的城市名称
|
# 中文的城市名称
|
||||||
@@ -156,10 +157,11 @@ class Display:
|
|||||||
self.tft.write(self.cn_font, '??', 15,10)
|
self.tft.write(self.cn_font, '??', 15,10)
|
||||||
# 天气符号: 0-7 (未知、晴、云、雨、雷、雪、雾、风)
|
# 天气符号: 0-7 (未知、晴、云、雨、雷、雪、雾、风)
|
||||||
if weather is not None and weather != self.ui_data.get('weather'):
|
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
|
self.ui_data['weather'] = weather
|
||||||
# 建议信息可能有很多条,需要轮换展示
|
# 建议信息可能有很多条,需要轮换展示
|
||||||
if advice is not None and advice != self.ui_data.get('advice'):
|
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
|
self.ui_data['advice'] = advice
|
||||||
# AQI等级分成0-5级,分别对应优、良、中、差、污、恶
|
# AQI等级分成0-5级,分别对应优、良、中、差、污、恶
|
||||||
if aqi is not None and aqi != self.ui_data.get('aqi'):
|
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'):
|
if t is not None and t != self.ui_data.get('t'):
|
||||||
self.ui_data['t'] = t
|
self.ui_data['t'] = t
|
||||||
self.tft.fill_rect(35,179,40,16,0)
|
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'):
|
if rh is not None and rh != self.ui_data.get('rh'):
|
||||||
self.ui_data['rh'] = rh
|
self.ui_data['rh'] = rh
|
||||||
self.tft.fill_rect(110,179,40,16,0)
|
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'):
|
if pm is not None and pm != self.ui_data.get('pm'):
|
||||||
self.ui_data['pm'] = pm
|
self.ui_data['pm'] = pm
|
||||||
self.tft.fill_rect(35,213,40,16,0)
|
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'):
|
if co2 is not None and co2 != self.ui_data.get('co2'):
|
||||||
# 如果co2数据存在,优先显示co2
|
# 如果co2数据存在,优先显示co2
|
||||||
if self.ui_data.get('ap'):
|
if self.ui_data.get('ap'):
|
||||||
@@ -196,12 +198,12 @@ class Display:
|
|||||||
self.show_jpg("/rom/images/co2.jpg",85,209)
|
self.show_jpg("/rom/images/co2.jpg",85,209)
|
||||||
self.ui_data['co2'] = co2
|
self.ui_data['co2'] = co2
|
||||||
self.tft.fill_rect(110,213,40,16,0)
|
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'):
|
elif self.ui_data.get('co2') is None and ap is not None and ap != self.ui_data.get('ap'):
|
||||||
# 没co2时候才会显示大气压
|
# 没co2时候才会显示大气压
|
||||||
self.ui_data['ap'] = ap
|
self.ui_data['ap'] = ap
|
||||||
self.tft.fill_rect(110,213,40,16,0)
|
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
|
from machine import RTC
|
||||||
y,m,d,_w,H,M,*_ = RTC().datetime()
|
y,m,d,_w,H,M,*_ = RTC().datetime()
|
||||||
@@ -235,8 +237,8 @@ class Display:
|
|||||||
i = self.ticks % len(advice)
|
i = self.ticks % len(advice)
|
||||||
c = advice[i]
|
c = advice[i]
|
||||||
w = self.tft.write(self.cn_font, advice[i], 15,45)
|
w = self.tft.write(self.cn_font, advice[i], 15,45)
|
||||||
if w<22*6: # fill
|
if w<22*7: # fill
|
||||||
self.tft.fill_rect(15+w,45,22*6-w,22,0)
|
self.tft.fill_rect(15+w,45,22*7-w,22,0)
|
||||||
# 打印内存信息
|
# 打印内存信息
|
||||||
_print_mem()
|
_print_mem()
|
||||||
|
|
||||||
|
|||||||
@@ -56,10 +56,10 @@ class WiFiManager:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
ssid = self.config.get("ssid")
|
ssid = self.config.get("ssid")
|
||||||
password = self.config.get("password")
|
password = self.config.get("password") or ""
|
||||||
|
|
||||||
if not ssid or not password:
|
if not ssid:
|
||||||
print("SSID or password is empty")
|
print("SSID is empty")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
print(f"Trying to connect to SSID: {ssid}")
|
print(f"Trying to connect to SSID: {ssid}")
|
||||||
|
|||||||
@@ -213,9 +213,9 @@
|
|||||||
/><br />
|
/><br />
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
|
id="pwd"
|
||||||
placeholder="WiFi密码"
|
placeholder="WiFi密码"
|
||||||
name="password"
|
name="password"
|
||||||
required
|
|
||||||
/><br />
|
/><br />
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
@@ -298,6 +298,7 @@
|
|||||||
item.appendChild(signalContainer);
|
item.appendChild(signalContainer);
|
||||||
item.onclick = function () {
|
item.onclick = function () {
|
||||||
document.getElementById("ssid").value = network.ssid;
|
document.getElementById("ssid").value = network.ssid;
|
||||||
|
document.getElementById('pwd').focus();
|
||||||
};
|
};
|
||||||
listContainer.appendChild(item);
|
listContainer.appendChild(item);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
CN_TEXT='北京月日星期一二三四五六末优良中差污恶能见度配置设备网络连接热点自动进入页面0123456789 abcdefghijklmnopqrstuvwxyz:ABCDEFGHIJKLMNOPQRSTUVWXYZ!%*+,-./()[]\"<>=?℃'
|
CN_TEXT='天气北京市区邹城怀柔呼和浩特月日星期一二三四五六七八九十腊冬晴多云转雷雨雪雾霾风南东西级末优良中差污恶能见度配置设备网络连接热点自动进入页面0123456789 abcdefghijklmnopqrstuvwxyz:ABCDEFGHIJKLMNOPQRSTUVWXYZ!%*+,-./()[]\"<>=?℃'
|
||||||
TIME_TEXT='0123456789: .-'
|
TIME_TEXT='0123456789: .-'
|
||||||
|
|
||||||
SRC_DIR=./assets
|
SRC_DIR=./assets
|
||||||
|
|||||||
Reference in New Issue
Block a user