https://blog.csdn.net/qq_40181592/article/details/119176805 |
天气信息通过心知天气 seniverse.com 官方API获取,免费提供天气实况及未来三天的天气,首先注册登录。
https://seniverse.yuque.com/books/share/e52aa43f-8fe9-4ffa-860d-96c0f3cf1c49/nyiu3
天气实况API:
https://api.seniverse.com/v3/weather/now.json?key=你的私钥&location=查询城市的拼音&language=zh-Hans&unit=c
当天+未来3天天气API:
https://api.seniverse.com/v3/weather/daily.json?key=你的私钥&location=查询城市的拼音&language=zh-Hans&unit=c&start=0&days=5
生活指数API :
https://api.seniverse.com/v3/life/suggestion.json?key=你的私钥&location=查询城市的拼音&language=zh-Hans
https://api.seniverse.com/v3/weather/now.json?key=SiFlIC-Dte98pIK7z&location=haerbin&language=zh-Hans&unit=c
{
"results":[
{
"location":
{
"id":"YB1UX38K6DY1",
"name":"哈尔滨",
"country":"CN",
"path":"哈尔滨,哈尔滨,黑龙江,中国",
"timezone":"Asia/Shanghai",
"timezone_offset":"+08:00"
},
"now":
{
"text":"晴",
"code":"0",
"temperature":"19"},
"last_update":"2022-05-16T13:54:10+08:00"
}
]
}
https://arduinojson.org/
TFT_eSPI tft = TFT_eSPI();
const char *ssid = "********";
const char *password = "********";
const char* host = "api.seniverse.com"; //心知天气服务器地址
String now_address="",now_weather="",now_temperature="";
void get_weather()
{
WiFiClient client; //创建TCP连接
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
Serial.println("connection failed"); //网络请求无响应打印连接失败
return;
}
//URL请求地址
String url ="/v3/weather/now.json?key=SiFlIC-Dte98pIK7z&location=haerbin&language=zh-Hans&unit=c";
//发送网络请求
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(5000);
//定义answer变量用来存放请求网络服务器后返回的数据
String answer;
while(client.available())
{
String line = client.readStringUntil('\r');
answer += line;
}
//断开服务器连接
client.stop();
Serial.println();
Serial.println("closing connection");
//获得json格式的数据
String jsonAnswer;
int jsonIndex;
//找到有用的返回数据位置i 返回头不要
for (int i = 0; i < answer.length(); i++)
{
if (answer[i] == '{')
{
jsonIndex = i;
break;
}
}
jsonAnswer = answer.substring(jsonIndex);
Serial.println();
Serial.println("JSON answer: ");
Serial.println(jsonAnswer);
StaticJsonDocument<512> doc;
deserializeJson(doc, jsonAnswer);
JsonObject results_0 = doc["results"][0];
JsonObject results_0_location = results_0["location"];
const char* results_0_location_name = results_0_location["name"]; // "哈尔滨"
JsonObject results_0_now = results_0["now"];
const char* results_0_now_text = results_0_now["text"]; // "晴"
const char* results_0_now_temperature = results_0_now["temperature"]; // "19"
now_address = results_0_location_name;
now_weather = results_0_now_text;
now_temperature = results_0_now_temperature;
}
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password); // 连接网络
while (WiFi.status() != WL_CONNECTED) //等待wifi连接
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected"); //连接成功
Serial.print("IP address: "); //打印IP地址
tft.init(); //初始化
tft.fillScreen(TFT_BLACK); //屏幕颜色
get_weather();
}
void loop()
{
show_weather(TFT_WHITE,TFT_BLACK); // 显示天气界面
}
void show_weather(uint16_t fg,uint16_t bg)
{
tft.setSwapBytes(true); //使图片颜色由RGB->BGR
showMyFonts(40, 40, now_address.c_str(), TFT_WHITE);
showMyFonts(45, 60, now_weather.c_str(), TFT_WHITE);
//showMyFonts(75, 100, now_temperature.c_str(), TFT_WHITE);
showtext(55,80,1,1,fg,bg,now_temperature);
}
/*******************整句字符串显示****************/
void showtext(int16_t x,int16_t y,uint8_t font,uint8_t s,uint16_t fg,uint16_t bg,const String str)
{
//设置文本显示坐标,和文本的字体,默认以左上角为参考点,
tft.setCursor(x, y, font);
// 设置文本颜色为白色,文本背景黑色
tft.setTextColor(fg,bg);
//设置文本大小,文本大小的范围是1-7的整数
tft.setTextSize(s);
// 设置显示的文字,注意这里有个换行符 \n 产生的效果
tft.println(str);
}
/*******************单个汉字显示****************/
void showMyFont(int32_t x, int32_t y, const char c[3], uint32_t color) {
for (int k = 0; k < 26; k++)// 根据字库的字数调节循环的次数
if (hanzi[k].Index[0] == c[0] && hanzi[k].Index[1] == c[1] && hanzi[k].Index[2] == c[2])
{ tft.drawBitmap(x, y, hanzi[k].hz_Id, hanzi[k].hz_width, 16, color);
}
}
/*******************整句汉字显示****************/
void showMyFonts(int32_t x, int32_t y, const char str[], uint32_t color) { //显示整句汉字,字库比较简单,上下、左右输出是在函数内实现
int x0 = x;
for (int i = 0; i < strlen(str); i += 3) {
showMyFont(x0, y, str+i, color);
x0 += 17;
}
}
TFT显示天气信息: