Funpack第12期-自动联网的天气预报仪
我选择的是任务二:制作一个自动联网的天气预报仪,在设计界面显示温湿度、天气情况、空气质量以及未来三天内的天气变化。
标签
嵌入式系统
zhong
更新2021-12-24
广东交通职业技术学院
1004

一、实现功能

1、使用Http模块,访问天气API,获得天气数据

2、使用Arduino-Json模块,解析API的JSON数据,获得三天的气温,湿度,空气质量等数据

3、使用Wio terminal TFT模块,将数据显示到LCD屏幕上

二、代码片段解释

1、TFT显示部分

void set_message(
    int wather_index,
    String temperature, 
    String humidity, 
    String wather, 
    String air_quality, 
    String date 
    ) {
    wather_index_global = wather_index;
    drawImage<uint8_t>("bg.bmp", 0,0); 
    if (wather_index_global<4)
    {
        drawImage<uint8_t>(ico_list[0], 215,16); //Display this 8-bit image in sd card from (0, 0)
    }
    else if (wather_index_global>=4& wather_index_global<9)
    {
        drawImage<uint8_t>(ico_list[3], 215, 16); //多云
    }
    else if (wather_index_global>=9& wather_index_global<19)
    {
        drawImage<uint8_t>(ico_list[1], 215,16); //Display this 8-bit image in sd card from (0, 0)
    }
    else if (wather_index_global>=20& wather_index_global<26)
    {
        drawImage<uint8_t>(ico_list[2], 215,16); //Display this 8-bit image in sd card from (0, 0)
    }
    else if (wather_index_global>=26)
    {
        drawImage<uint8_t>(ico_list[3], 215,16); //多云
    }
    tft.setFreeFont(FF15); //select Free, Mono, Oblique, 12pt.
    Serial.println(wather_index);
    Serial.println(wather);
    
    tft.setFreeFont(FF15); //select Free, Mono, Oblique, 12pt.
  
    tft.drawString(String(temperature + "C"),100,22); // set temperature
    tft.drawString(String(humidity+ "%"),100,71);// set humidity
    tft.drawString(String(wather),164,119);// set weather
    tft.drawString(String(air_quality),164,168);// set air quality
    tft.setFreeFont(FF5); //select Free, Mono, Oblique, 12pt.
    tft.drawString(String(date),66,211);// set date
}

void cleanScreenWithGreen(){
  tft.fillScreen(TFT_DARKGREEN); 
}

void setDate(String strDate){
  //in First line, set the date 
  tft.setTextSize(2);                   //sets the size of text
  tft.drawString(strDate, 10, 0); //prints strings from (0, 0)
}
void setHighTemp(String strHighTemp){
  //in Second line, set the High tempratrue
  tft.setTextSize(2);                   //sets the size of text
  tft.drawString("HighTemp: "+strHighTemp+" C", 10, 20); //prints strings from (0, 0)
}
void setLowTemp(String strLowTemp){
  //in Third line, set the Low tempratrue
  tft.setTextSize(2);                   //sets the size of text
  tft.drawString("LowTemp: "+strLowTemp+" C", 10, 40); //prints strings from (0, 0)
}
void setWather(String strWather){
  //in the Fourth line, set the wather
  tft.setTextSize(2);                   //sets the size of text
  tft.drawString("Wather: "+strWather, 10, 60); //prints strings from (0, 0)
}

2、HTTP连接部分

void httpClient(){

    WiFiClient client;
 
    if (!client.connect(host, port)) {
        Serial.println("Connection failed.");
        Serial.println("Waiting 5 seconds before retrying...");
        delay(5000);
        return;
    }
 
    // This will send a request to the server
    //uncomment this line to send an arbitrary string to the server
    //client.print("Send this data to the server");
    //uncomment this line to send a basic document request to the server
    String reqRes = "http://api.seniverse.com/v3/weather/daily.json?key=" + reqUserKey +  //Create the request url of the request
                    "&location=" + reqLocation + "&language=en&unit=" +
                    reqUnit+"&start=0&day=3";
    Serial.println(reqRes);//test for reqRest
    Serial.println("");

    String httpRequest = String("GET ") + reqRes + "HTTP/1.1"; //Combinate the first line of  Http request
    client.println(httpRequest);
    client.println("Host: api.seniverse.com"); // Second line of Http request
    client.println("Connection: close");
    client.println();
 
    int maxloops = 0;
 
    //wait for the server's reply to become available
    while (!client.available() && maxloops < 1000) {
        maxloops++;
        delay(1); //delay 1 msec
    }
    if (client.available() > 0) {

    jsonParse(client); // use the function jsonParse to parse json

    } else {
        Serial.println("client.available() timed out ");
    }
 
    Serial.println("Closing connection.");
    client.stop();

}

三、功能展示

1、显示第一天的天气数据

FpKAmyz2OQy2-vJp6efwwXVZuuk8

2、显示第二天的天气数据

FgEmiMJmhv4IEo0WXgN_M6TGT5-q

3、显示第三天的天气数据

Fk9yegE2Y3LtEuvjSzxHvs3ror3k

四、心得体会

1、Wio Terminal的TFT库的显示有BUG,不能放在不同的函数里面全屏显示,不然会卡死

2、可以使用Arduino-Json处理Json数据

3、可以将图片放在TFT卡中,进行图片的展示

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