# luatos-Desktop weather station **Repository Path**: lc9911/luatos-desktop-weather-station ## Basic Information - **Project Name**: luatos-Desktop weather station - **Description**: 使用合宙Air640w搭建的桌面天气助手 - **Primary Language**: Lua - **License**: LGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2021-02-16 - **Last Updated**: 2024-12-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # luatos-Desktop weather station 基于合宙Air640w的天气桌面 ## 介绍 使用合宙Air640w搭建的桌面天气助手,屏幕使用的是迪文科技的串口屏。 先放上渲染图 ![428fda1daf3c543062fb639a1c035490.png](https://gitee.com/lc9911/luatos-desktop-weather-station/raw/master/readmeimg/Image%20%5B2%5D.png) ## 今日天气获取 通过http.get向百度地图开放平台获取天气情况,将返回的json数据进行解析,获得天气情况、温湿度,将天气情况与预存的对照表进行对比,获取天气的编码,通过串口屏编码函数进行数据编码,使用串口发送给串口屏。 ```lua http.get( "http://api.map.baidu.com/weather/v1/?district_id=130111&data_type=now&ak=**************************", nil, function(code, headers, body) local WeatherIdi = 1 -- 天气索引值 local MyJson,result = json.decode(body) if result then Temp = MyJson.result.now.temp -- 温度 Hum = MyJson.result.now.rh -- 相对湿度 IconNumShow(0x5001,Temp) -- 发送温度 IconNumShow(0x5003,Hum) -- 发送湿度 while MyJson.result.now.text ~= WeatherId[WeatherIdi] do --检索当前天气 WeatherIdi = WeatherIdi + 1 end WeatherIdi = WeatherSwitch(WeatherIdi) -- 获取串口屏天气序号 IconNumShow(0x5000,WeatherIdi) -- 串口屏显示天气图标 else end end ) ``` ## 预报天气获取 通过http.get向百度地图开放平台获取天气情况,将返回的json数据进行解析,获得天气情况、最高低温度,将天气情况与预存的对照表进行对比,获取天气的编码,通过串口屏编码函数进行数据编码,使用串口发送给串口屏。 ```lua http.get( "http://api.map.baidu.com/weather/v1/?district_id=130111&data_type=fc&ak=**************************", --获取预报天气 nil, function(code, headers, body) local WeatherIdFCi = 1 local FCJson,result = json.decode(body) if result then HTemp = FCJson.result.forecasts[1].high -- 最高温 LTemp = FCJson.result.forecasts[1].low -- 最低温 IconNumShow(0x500A,HTemp) -- 发送最高温 IconNumShow(0x500C,LTemp) -- 发送最低温 while FCJson.result.forecasts[1].text_day ~= WeatherId[WeatherIdFCi] do --检索当前天气 WeatherIdFCi = WeatherIdFCi + 1 end WeatherIdFCi = WeatherSwitch(WeatherIdFCi) -- 获取串口屏天气序号 IconNumShow(0x5009,WeatherIdFCi) -- 串口屏显示天气图标 else end end ) ``` ## 时间获取 通过http.get向苏宁时间接口发起请求获取时间,并通过串口屏时间设置函数发送到串口屏上。 ```lua http.get( "http://quan.suning.com/getSysTime.do", nil, function(code, headers, body) local TMJson,result = json.decode(body) if result then local TIME = TMJson.sysTime1 -- 时间 local t_year = string.sub(TIME,1,4)--取出年份 local t_month = string.sub(TIME,5,6)--取出月份 local t_day = string.sub(TIME,7,8)--取出日 local t_hour = string.sub(TIME,9,10)--取出时 local t_min = string.sub(TIME,11,12)--取出分 t_sec = string.sub(TIME,13,14)--取出秒 RTCShow(t_year,t_month,t_day,t_hour,t_min,t_sec) -- 时间发送 end end ) ``` ## 设备控制 设备控制主要依赖于MQTT,通过解析串口屏按键按下后的反馈,将预先设置好的数据发送到MQTT服务器上,再通过其他设备订阅即可实现。 ### 被控设备 控制设备:NODEMCU 编程环境:arduino ### 上报设备 数据上报设备:air800 编程环境:luat ### 相关函数 ```lua --MQTT发送函数 function MQTTSend(status,device) if _G.mqttc and _G.mqttc.stat == 1 then if device == 0 then --灯控制 if status == 1 then sys.taskInit(function() _G.mqttc:pub("Ldevice", 0, "{\"status\":1}")--开灯 end) else sys.taskInit(function() _G.mqttc:pub("Ldevice", 0, "{\"status\":0}")--关灯 end) end elseif device == 1 then --风扇控制 if status == 1 then sys.taskInit(function() _G.mqttc:pub("Fdevice", 0, "{\"status\":1}")--开灯 end) else sys.taskInit(function() _G.mqttc:pub("Fdevice", 0, "{\"status\":0}")--关灯 end) end end end end ``` ## 串口屏驱动 串口屏采用的是迪文科技串口屏,需要根据驱动手册编写串口屏的发送和接受。 ### 图标更改驱动代码: ```lua --更换图标函数 Add图标地址 ID显示序号 function IconNumShow(Add,ID) local Data = {0,0,0,0,0,0,0,0} Data[1] = 0x5A Data[2] = 0xA5 Data[3] = 0x05 --长度 Data[4] = 0x82 Data[5] = (Add & 0xFF00) >> 8 Data[6] = Add & 0xFF Data[7] = (ID & 0xFF00) >> 8 Data[8] = ID & 0xFF local Str = string.char(Data[1],Data[2],Data[3],Data[4],Data[5],Data[6],Data[7],Data[8]) uart.write(uartid,Str) end ``` ### 串口数据解析代码: ```lua --处理串口数据 local function proc(data) if not data then return end --寻找帧头0xAA local head = string.find(data,string.char(0x5A)) if not head then return false,data end local len,states,Add_H,Add_L,D_len,Data_H,Data_L = string.byte(data,head+2,-1) --从帧头到末尾分割 -- 进行地址 键值的判断操作 end ``` ## 主界面 ![主界面.png](https://gitee.com/lc9911/luatos-desktop-weather-station/raw/master/readmeimg/Image.png) ## 视频地址 [bilibili视频地址](https://www.bilibili.com/video/BV1dy4y1E7vo/) ## 改进方案 * 增加电池充放电路,可摆脱数据线。 * 可通过立创EDA进行PCB设计,将整块板子固定在屏幕上。 注:主界面设计来源慕客,相关素材来源iconfont,请勿用于商业用途。