广州大彩串口屏论坛_大彩开发者交流论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 7745|回复: 2

WiFi获取天气

[复制链接]

3

主题

3

帖子

31

积分

新手上路

Rank: 1

积分
31
发表于 2024-12-23 16:44:08 | 显示全部楼层 |阅读模式
WiFi怎么可以获取当地的天气
回复

使用道具 举报

64

主题

252

帖子

5551

积分

版主

大彩爱好者Q群14769485

Rank: 7Rank: 7Rank: 7

积分
5551
QQ
发表于 2024-12-23 18:54:19 | 显示全部楼层
wifi只是网络连接的一种,获取天气需要在应用层操作。
方式1:去百度搜索寻找一个天气接口(有免费和收费的)然后根据接口的参数使用http_request()函数post发起请求,天气数据将以json格式返回,使用cjson解析即可。
方式2:找一个能显示天气的网站,使用http_request()函数发起get请求,返回的是网站源码,使用lua的文本分割或正则表达式匹配出关于天气的文字即可。
大彩爱好者Q群14769485
点我下载→TFT【易模块】封装好的函数库
点我下载→TFT屏可中文lua编辑器【好用】
回复

使用道具 举报

0

主题

1

帖子

26

积分

新手上路

Rank: 1

积分
26
发表于 2025-6-16 09:04:22 | 显示全部楼层
本帖最后由 qilinchong 于 2025-6-16 09:21 编辑

我刚好做了一个,欢迎交流!
-- 配置区 --
local API_KEY = ""    此处替换你的心知KEY,免费注册使用
local LOCATION = "beijing"
local USE_HTTPS = false

-- 状态变量 --
local weather_data = {}

-- 英文到中文的映射表
local EN_TO_ZH_MAP = {
    -- 天气状态映射
    ["Sunny"] = "晴",
    ["Cloudy"] = "多云",
    ["Overcast"] = "阴",
    ["Rain"] = "雨",
    -- 城市名称映射
    ["Beijing"] = "北京",
    -- 可以根据需要添加更多映射
}

-- 初始化函数 --
function on_init()
    dofile("e_module.lua")
    print("系统初始化完成")
    -- 设置天气请求定时器(5秒后启动)
    start_timer(1, 5000, 0, 0)
end

-- 增强版请求函数 --
function get_weather()
    local url = string.format(
        "%s://api.seniverse.com/v3/weather/now.json?key=%s&location=%s&language=en&unit=c",
        USE_HTTPS and "https" or "http",
        API_KEY,
        LOCATION
    )

    if DEBUG_MODE then
        print("\n[调试] 请求URL:", url:gsub(API_KEY, "******"))
        print("设备时间:", os.date("%Y-%m-%d %H:%M:%S"))
    end

    http_request(1, url, 0, "Accept-Charset: utf-8", "")
end

-- 精准JSON解析 --
function parse_weather(json_str)
    local data = {}

    -- 基础字段匹配
    data.city = json_str:match('"name":"([^"]+)"') or "N/A"
    data.temp = json_str:match('"temperature":"?(%d+)%D*"?') or "N/A"
    data.status = json_str:match('"text":"([^"]+)"') or "N/A"

    -- 错误检测
    data.error = json_str:match('"status":"([^"]+)"')  -- API错误状态

    if DEBUG_MODE then
        print("\n[调试] 原始响应片段:", json_str:sub(1, 100).."...")
        print("解析结果:", data.city, data.temp, data.status)
    end

    return data
end

-- 响应处理函数 --
function on_http_response(taskid, response)
    if taskid == 1 then
        if response then
            weather_data = parse_weather(response)
            show_weather()
        else
            print("\n[网络错误] 可能原因:")
            print("1. 物理网络未连接")
            print("2. DNS解析失败")
            print("3. 防火墙拦截")
            print("4. API服务器不可用")

            -- 失败重试逻辑
            if retry_count < 3 then
                retry_count = retry_count + 1
                start_timer(1, 5000, 0, 0)
                print(string.format("将在5秒后重试(剩余次数%d)", 3 - retry_count))
            end
        end
    end
end

-- 数据显示函数 --
function show_weather()
    print("\n==== 实时天气 ====")
    if weather_data.error then
        print("[API错误]", weather_data.error)
        if weather_data.error == "invalid_key" then
            print("解决方案:")
            print("1. 访问心知控制台检查密钥")
            print("2. 联系技术支持")
        end
    else
        local city = EN_TO_ZH_MAP[weather_data.city] or weather_data.city
        local status = EN_TO_ZH_MAP[weather_data.status] or weather_data.status

        print("城市:", city)
        print("状态:", status)
        print("温度:", weather_data.temp.."℃")
        print("更新时间:", os.date("%H:%M"))
    end
    print("==================")
end

-- 定时器回调 --
function on_timer(timer_id)
    if timer_id == 1 then
        get_weather()
    end
end
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|广州大彩串口屏论坛_大彩开发者交流论坛

GMT+8, 2025-7-18 18:24 , Processed in 0.061341 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表