Funpack12:桌面天气预报小助手
Wio 终端与 Arduino 和 Micropython 兼容,采用ATSAMD51微控制器构建,具有 Realtek RTL8720DN支持的无线连接。它的 CPU 速度运行在120MHz (Boost up to 200MHz)。
标签
嵌入式系统
颜七岁
更新2021-12-30
2025

Wio Terminal 板卡介绍:

Ft-Rn2Hlc4aaJetz_rJYzSgxp08S

Wio Terminal 基本参数

 

Fnk1bnLl-ryWCwWp55dR4q-7WZ8r

Wio Terminal 引脚

FkTww6yTBCznzURWRcZUXKM99ynU

 

Wio Terminal 板卡上手:

工具包下载

工具包地址(选用):https://github.com/Seeed-Studio/ambd_flash_tool

Git下载:

cd ~
git clone https://github.com/Seeed-Studio/ambd_flash_tool
更新固件擦除固件
cd ~
git clone https://github.com/Seeed-Studio/ambd_flash_tool
进入目录
cd ambd_flash_tool
擦除命令
.\ambd_flash_tool.exe erase

注意:初始擦除过程可能需要一段时间。请耐心等待,不要关闭窗口

第 2 步 - 刷新最新固件

对于 Windows

进入目录

cd ambd_flash_tool

当您在ambd_flash_tool目录中时,执行以下命令将最新固件刷入 RTL8720

.\ambd_flash_tool.exe flash
检查版本

从 Arduino IDE 检查 RTL8720 固件版本

 

Wi-Fi 所需的库

我们需要以下库才能在 Wio 终端上开始使用 Wi-Fi。您可以通过在 Arduino 库管理器的搜索框中键入库名称来搜索这些库。

安装上述固件后,可以通过Arduino IDE上传以下代码到Wio终端来检查固件是否安装正确。

固件查看程序
#include "rpcWiFi.h"
 
void setup() {
    Serial.begin(115200);
    while(!Serial); // Wait to open Serial Monitor
    Serial.printf("RTL8720 Firmware Version: %s", rpc_system_version());
}
 
void loop() {
}

Fn-JmxiE5GACVeb1CTrWHPcjZUAS

本期任务:

任务要求:

1.联网查询

2.温湿度值

3.天气情况

4.空气质量

5.未来三天内的天气

第一步:获取天气数据
OpenWeather的天气数据API格式
//获取日期数据
etTimeJSON = Get_Json_Date("http://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json&tdsourcetag=s_pctim_aiomsg");

//获取天气数据
GetWeatherJSON = Get_Json_Date("http://api.openweathermap.org/data/2.5/weather?id=" + CityId + "&units=metric&appid=" + OpenWeatherApiKey);

//获取空气质量数据
GetAQIJSON = Get_Json_Date("http://api.openweathermap.org/data/2.5/weather?lat=" + Citylat + "&lon=" + Citylon + "&appid=" + OpenWeatherApiKey);

//获取未来三天天气数据
GetFuture_WeatherJSON = Get_Json_Date("https://api.openweathermap.org/data/2.5/forecast?id=" + CityId + "&units=metric&appid=" + OpenWeatherApiKey");

void Get_Weather_Date(String url){//获取天气数据

    if((WiFi.status() == WL_CONNECTED)) {
        
        HTTPClient http;
        Serial.print("[HTTP] begin...\n");

        http.begin(url); //HTTP
        Serial.print("[HTTP] GET...\n");
        //发送HTTP请求
        int httpCode = http.GET();
        // httpCode在出现错误时为负值
        if(httpCode > 0) {
            // HTTP标头已发送,服务器响应标头已处理
            Serial.printf("[HTTP] GET... code: %d\n", httpCode);
            //得到JSON信息,开始解析
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                Serial.println(payload);//打印请求的JSON
            }
        } else {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }
        http.end();
    }
    }

FkNC0x7N4Ex5a94jce87Pda8ad_n

第二步:解析JSON内容

以当天天气为例

void Weather_JsonDate()
{
    
    GetWeatherJSON = Get_Json_Date("http://api.openweathermap.org/data/2.5/weather?id=" + CityId + "&units=metric&appid=" + OpenWeatherApiKey);  // 获取当前天气 
     
    StaticJsonDocument<600> doc;
    deserializeJson(doc, GetWeatherJSON);
    //地址位置
    JsonObject coored = doc["coord"];
    lon = coored["lon"];
    Citylon = lon;
    Serial.println(lon);
    lat = coored["lat"];
    Citylat = lat;
    Serial.println(lat);
     // 获取天气状况
    JsonArray weather_A = doc["weather"];
    JsonObject weather = weather_A[0];

    Serial.println("icon = ");
    icon_current = weather["icon"];
    Serial.println(icon_current); 

    description = weather["description"].as<const char*>();
    Serial.println(description);   

    // 获取温湿度
    JsonObject main = doc["main"];
    temp_current = main["temp"];
    Serial.println(temp_current);
    humidity_current = main["humidity"];
    Serial.println(humidity_current);

    pressure_current = main["pressure"];
    Serial.println(pressure_current);
    feels_like_current = main["feels_like"];
    Serial.println(feels_like_current);  
    temp_max = main["temp_max"];
    Serial.println(temp_max);
    temp_min = main["temp_min"];
    Serial.println(temp_min);
}

解析JSON

需要用到的网站:https://www.sojson.com/

FvMePye08mkjVDeYUnL6lgZhiGjJ

通过校验可以使得我们获取的数据更好解析

如果需要在代码中逐一调试,我们还需要对Json数据进行转义

FqJgCODll-oBqFDIsnI3S0vV43Zk

FmmF3TsLhMaT-tyFCFx09ZmWwCSo

 

第三部:判断空气质量(文字采用tft群取模)

//?判断空气质量并显示
void ShowWeatherAQI(int32_t x, int32_t y, int32_t aqi_val)
{
    switch (aqi_val)
    {
    case 1: // 优 绿色
        tft.drawBitmap(x , y , W_AQI + 16 * 8 * 4, 32, 32, TFT_GREEN);
        break;
    case 2: // 良 黄色
        tft.drawBitmap(x , y , W_AQI + 16 * 8 * 5, 32, 32, TFT_YELLOW);
        break;
    case 3: // 轻度污染 橙色
        tft.drawBitmap(x , y , W_AQI + 16 * 8 * 6, 32, 32, TFT_ORANGE);
        tft.drawBitmap(x + 35 , y , W_AQI + 16 * 8 * 7, 32, 32, TFT_ORANGE);
        break; 
    case 4:// 中度污染 红色
        tft.drawBitmap(x , y , W_AQI + 16 * 8 * 10, 32, 32, TFT_RED);
        tft.drawBitmap(x + 35, y , W_AQI + 16 * 8 * 7, 32, 32, TFT_RED);
        break;
    case 5: // 重度污染 紫色
        tft.drawBitmap(x , y , W_AQI + 16 * 8 * 11, 32, 32, TFT_PURPLE);
        tft.drawBitmap(x + 35 , y , W_AQI + 16 * 8 * 7, 32, 32, TFT_PURPLE);
        break;
    case 6: // 严重污染 褐红色
        tft.drawBitmap(x , y, W_AQI + 16 * 8 * 12, 32, 32, 0xf81f);
        tft.drawBitmap(x + 35, y, W_AQI + 16 * 8 * 11, 32, 32, 0xf81f);
        break;
    default:
        tft.drawBitmap(x , y + 32, W_AQI + 16 * 8 * 4, 32, 32, TFT_WHITE);
        break;   
    }
}

第四步:通过NTP获取时间

#include <WiFiUdp.h>
#include <TimeLib.h>
#include <NTPClient.h> 
// WiFi信息
const char* ssid = "SSDI";
const char* password =  "wifipassword";

//网络授时时间
WiFiUDP ntpUDP;   
NTPClient timeClient(ntpUDP, "ntp1.aliyun.com", 60 * 60 * 8, 30 * 60 * 1000);

String nowTime = ""; 

//?主程序初始化
void setup() {
    //串口初始化
    Serial.begin(115200);
    //WiFi初始化
    WiFi_INIT();
    //连接NTP服务器(阿里的NTP服务器)
    timeClient.begin();
}

//?主程序循坏
void loop() {
Serial.println(nowtime);
}
 

 

整体源码在文章附件当中

参考网址:

Arduino 在线json教程https://arduinojson.org/v6/example/

原文地址:https://wiki.seeedstudio.com/Wio-Terminal-Network-Overview/

项目地址:https://www.eetree.cn/project/detail/601

 

屏幕坐标系

Fj5_H5vhSKcO2et4Js-IP7xpU7ct

 

 

总结与分享:

Wio Terminal 是一块不错学习板卡,尤其是物联网领域的学生,工程师或创客。板卡自身携带WiFi,蓝牙功能,存储的Flash多达4MB,还支持SD卡拓展,本次我做的任务②,涉及到网络解析方便比较多,同时我也把这款板卡首次上手到编程学习,从头记录一遍,给我的印象非常深刻,感谢硬禾学堂提供的Funpack活动,我只能说Funpack太赞了!

 
附件下载
WiFi.zip
程序
团队介绍
评论
0 / 100
查看更多
硬禾服务号
关注最新动态
0512-67862536
info@eetree.cn
江苏省苏州市苏州工业园区新平街388号腾飞创新园A2幢815室
苏州硬禾信息科技有限公司
Copyright © 2024 苏州硬禾信息科技有限公司 All Rights Reserved 苏ICP备19040198号