|
本帖最后由 topdog 于 2022-5-25 08:49 编辑
高德也提供免费的气象的web应用服务。首先注册获得服务以后,再用合宙ESP32C3使用TFT_eSPI库操作ST7735s屏幕,做一个桌面气象显示器。
高德气象服务的API: https://lbs.amap.com/api/webservice/guide/api/weatherinfo
从气象服务可以知道通过 https://restapi.amap.com/v3/weat ... ity=110101&key=<用户key> 我们可以知道查询城市的实时天气情况,其中譬如上海浦东city=310115
再在控制台获得key=<用户key>代入就能够实现了。
天气现象表:https://lbs.amap.com/api/webservice/guide/tools/weather-code/ 因为设想是中文显示,网站的json回文也是中文的,所以,要把出现的中文字体统计一下,按照上一讲的方法转化成自己备用的字库。
https://restapi.amap.com的链接是HTTPS在通讯中需要提供CA安全证书,根据动图获取网站的CA证书:
然后用Notepad++打开刚才的*.cer安全证书文件,鼠标点住最左上的点,alt+鼠标左键向下拉一条直线,直至最后一行。鼠标移至序列号位置,alt+c调出列编辑选项卡,英文输入状态下输入"
右面重复上述操作,注意输入的字符不一样。
按照下图整理一下,复制黏贴到程序中。
https://restapi.amap.com的链接json回文采用ArduinoJson解析,ArduinoJson助手会提供了防呆解析json数据的服务:
示例程序如下:
[pre]#include <Arduino.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "FontYahei24.h"
#include <SPI.h>
#include <TFT_eSPI.h>
const char* SSID = "WiFi名称";
const char* PASSWORD = "WiFi密码";
String City = "城市代码"; //查询城市代码 https://lbs.amap.com/api/webservice/download
String UserKey = "用户key"; //申请用户key https://console.amap.com/dev/
unsigned int QueryInterval = 60000; //查询间隔时间,这是一分钟。如需5分钟改为:5*60000
const char* rootCACertificate = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG\n" \
"A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNV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JlR3XC321Y9YeRq4VzW9v493kHMB65jUr9TU/Qr6cf9tveCX4XSQRjbgbME\n" \
"HMUfpIBvFSDJ3gyICh3WZlXi/EjJKSZp4A==\n" \
"-----END CERTIFICATE----- \n" ;
TFT_eSPI tft = TFT_eSPI();
WiFiMulti WiFiMulti;
void setup(void) {
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(SSID, PASSWORD);
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextFont(2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0);
//connect to WiFi
tft.printf("Connecting to %s ", SSID);
while ((WiFiMulti.run() != WL_CONNECTED)) {
delay(500);
tft.print(".");
}
tft.println(" CONNECTED");
}
void loop() {
WiFiClientSecure *client = new WiFiClientSecure;
if (client) {
client -> setCACert(rootCACertificate);
{
HTTPClient https;
if (https.begin(*client, "https://restapi.amap.com/v3/weather/weatherInfo?city=" + City + "&key=" + UserKey)) {
int httpCode = https.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String input = https.getString();
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
return;
}
JsonObject lives_0 = doc["lives"][0];
const char* lives_0_city = lives_0["city"]; // "浦东新区"
const char* lives_0_weather = lives_0["weather"]; // "小雨"
const char* lives_0_temperature = lives_0["temperature"]; // "18"
const char* lives_0_winddirection = lives_0["winddirection"]; // "东北"
const char* lives_0_windpower = lives_0["windpower"]; // "≤3"
const char* lives_0_humidity = lives_0["humidity"]; // "99"
tft.fillScreen(TFT_BLACK);
tft.loadFont(FONTYAHEI24);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.print(lives_0_city);
tft.print("天气: ");
tft.println();
tft.print("温度: ");
tft.print(lives_0_temperature);
tft.println(); //
tft.print("湿度: ");
tft.print(lives_0_humidity);
delay(QueryInterval);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0);
tft.print("气象: ");
tft.print(lives_0_weather);
tft.println();
tft.print("风向: ");
tft.print(lives_0_winddirection);
tft.println();
tft.print("风力: ");
tft.print(lives_0_windpower);
tft.println(); // New line
tft.unloadFont();
delay(QueryInterval);
}
https.end();
}
}
delete client;
}
}
}[/pre]
附件下载以后导入Arduino IDE,修改WiFi名称以及密码,城市代码和用户key烧录。
esp32c3_tft_espi_ChineseFont_gaode_st7735s_string.rar
(53.83 KB, 下载次数: 32)
最终效果:
|
|