内容介绍
内容介绍
一、实现功能
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、显示第一天的天气数据
2、显示第二天的天气数据
3、显示第三天的天气数据
四、心得体会
1、Wio Terminal的TFT库的显示有BUG,不能放在不同的函数里面全屏显示,不然会卡死
2、可以使用Arduino-Json处理Json数据
3、可以将图片放在TFT卡中,进行图片的展示
附件下载
wioterminal.bin
代码
团队介绍
钟离苛
团队成员
zhong
评论
0 / 100
查看更多
猜你喜欢
Funpack第12期Wio Terminal自动联网的天气预报仪Funpack第12期Wio Terminal自动联网的天气预报仪完成任务
voncg
1300
Funpack第12期-Wio Terminal-自动联网的天气预报仪_LVGLFunpack第12期-Wio Terminal-自动联网的天气预报仪_LVGL
奈奎斯特不稳定
1960
【Funpack第12期】基于Wio Terminal的自动联网天气预报仪基于Wio Terminal的自动联网天气预报仪,基于Arduino IDE及VSCode开发,可以连接WiFi后通过HTTP获取和风天气提供的天气API数据,通过JSON解析,并显示在LCD上,以及一些杂七杂八的实用功能。
葉SiR
1394