内容介绍
内容介绍
效果展示:
说明文档:
本次选择的是任务二:
- 制作一个自动联网的天气预报仪,在设计界面显示温湿度、天气情况、空气质量以及未来三天内的天气变化。
- 所以本次就利用WIO做一个迷你的气象站显示终端,通过网络上的天气API获取信息来进行展示。
完成气象站的显示需要以下功能模块:
- 网络连接模块,获取当前需要的天气信息——RTL8720DN
- 通过rpcWiFi库进行wifi的网络连接
- 通过HTTPClient获取天气API的信息:http://140.82.45.174:8000/weather
- 通过ArduinoJson库将获取的信息进行解析
- 显示模块,将获取到的信息进行排列的显示——2.4” LCD屏幕
- 使用TFT_eSPI库进行界面和信息的展示
代码:
#include "rpcWiFi.h"
#include "TFT_eSPI.h"
#include "HTTPClient.h"
#include "ArduinoJson.h"
TFT_eSPI tft;
TFT_eSprite spr = TFT_eSprite(&tft);
const char* ssid = "wio";
const char* password = "wiotestwifi";
const char* host = "http://140.82.45.174:8000/weather";
void checkWiFi(){
if(WiFi.status() == WL_CONNECTED){
return;
}
int dotCounter = 0;
while (WiFi.status() != WL_CONNECTED)
{
if(dotCounter%10 == 0){
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_CYAN); //sets the text colour to black
tft.setTextSize(2);
tft.setCursor(60,110);
}
tft.fillScreen(TFT_BLACK);
tft.setCursor(5,10);
tft.print("Wait for WiFi...");
dotCounter ++;
WiFi.begin(ssid, password);
}
Serial.println("WiFi connected");
tft.fillScreen(TFT_BLACK);
tft.setCursor(5,10);
tft.println("WiFi connected!");
Serial.println("IP address: ");
tft.println("IP address: ");
Serial.println(WiFi.localIP());
tft.print(WiFi.localIP());
delay(2000);
}
void setup()
{
Serial.begin(115200);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
tft.begin();
tft.setRotation(3);
tft.fillScreen(TFT_BLACK); //Black background
tft.setTextColor(TFT_CYAN); //sets the text colour to black
tft.setTextSize(2);
checkWiFi();
tft.fillScreen(TFT_BLACK); //Black background
tft.setCursor(5,10);
tft.println("Get Weather API...");
HTTPClient client;
client.begin(host); // Make HTTP GET request
int httpCode = client.GET();
StaticJsonDocument<1024> doc;
tft.print("StatusCode: ");
tft.println(httpCode);
if(httpCode!=0){
String data = client.getString();
DeserializationError error = deserializeJson(doc,data);
if (!error){
const char *city = doc["city"];
const char *day0_date = doc["0"][0];
const char *day0_wea = doc["0"][1];
const char *day0_tem = doc["0"][2];
const char *day0_temr = doc["0"][3];
const char *day0_hum = doc["0"][4];
const char *day0_air = doc["0"][5];
const char *day1_date = doc["1"][0];
const char *day1_wea = doc["1"][1];
const char *day1_temr = doc["1"][3];
const char *day2_date = doc["2"][0];
const char *day2_wea = doc["2"][1];
const char *day2_temr = doc["2"][3];
const char *day3_date = doc["3"][0];
const char *day3_wea = doc["3"][1];
const char *day3_temr = doc["3"][3];
// Display
// Display Title
tft.fillScreen(TFT_WHITE);
tft.fillRect(0,0,320,50,TFT_NAVY);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(3);
tft.drawString("Weather Forecast",20,15);
// Outline
tft.drawFastHLine(0,140,320,TFT_NAVY);
tft.drawFastVLine(107,140,190,TFT_NAVY);
tft.drawFastVLine(214,140,190,TFT_NAVY);
//0 day weather info
// screen 1
tft.setTextColor(TFT_BLACK);
tft.setTextSize(2);
tft.drawString(city,20,60);
tft.setTextSize(3);
tft.drawString(day0_tem,25,85);
tft.drawString(day0_wea,115,85);
tft.setTextSize(2);
tft.drawString(day0_date,20,120);
// screen 2
tft.setTextSize(2);
tft.drawString("Air:",229,60);
tft.drawString(day0_air,279,60);
tft.drawString("Hum:",229,89);
tft.drawString(day0_hum,279,89);
tft.drawString(day0_temr,229,120);
//1day info
tft.setTextSize(2);
tft.drawString(day1_date,25,155);
tft.drawString(day1_wea,25,183);
tft.drawString(day1_temr,15,215);
//2day info
tft.setTextSize(2);
tft.drawString(day2_date,132,155);
tft.drawString(day2_wea,132,183);
tft.drawString(day2_temr,122,215);
//3day info
tft.setTextSize(2);
tft.drawString(day3_date,239,155);
tft.drawString(day3_wea,239,183);
tft.drawString(day3_temr,229,215);
}
else{
Serial.print(error.f_str());
}
Serial.print(data);
}
client.end();
delay(200000);}
void loop(){
}
附件下载
sketch_dec09a_wio-weather.ino
团队介绍
我是一名电子公司的测试工程师,工作是配合项目做一些测试的工作,平时也喜欢玩一玩有趣的开发板,目前在学习编程,非常号的是这个项目给我提供了动力,是一个非常好的机会去学习嵌入式编程。
团队成员
吃货李李李
评论
0 / 100
查看更多