效果展示
前言
1、本项目全开源,包括接口地址,ESP32源码均开源。
2、实时天气采用高德API,数据来源是中国气象局。
高德API文档:
https://lbs.amap.com/api/webservice/guide/api/weatherinfo
3、为了防止key泄露,可以将高德API封装成自己的API。
天气API:
http://api.xemowo.top/api/tqyb.php?city=442000
天气API文档:
http://api.xemowo.top/api/tqyb.html
城市代码表:
https://wwmg.lanzouj.com/iGjyD1i9vf6b
4、本项目硬件采用ESP32 wroom 32,1.8TFT_LCD(ST7735s)。
5、代码平台采用Arduino,内置所有的天气中文字符,而且常用的天气都是适配了图标。bmp.h文件存有常用天气的图标。xem_font.h文件存有所有天气的中文字符。
项目实现
一、高德开放平台API
1、注册高德开放平台的账号。
https://lbs.amap.com/
2、应用管理-创建新应用。
3、添加key,选择web服务(请勿泄露key)。
4、下载城市代码表,查询自己的城市代码。
https://wwmg.lanzouj.com/iGjyD1i9vf6b
比如我是中山市,表内搜索中山市,代码为442000。
5、高德天气API。参数:city=城市代码 key=密钥key。
二、封装API
以上的高德API已经可以使用了,我这里使用的是PHP语言,实现只需要输入城市代码。
即可返回实时天气信息:
http://api.xemowo.top/api/tqyb.php?city=442000
error_reporting(0);//屏蔽报错
$city=$_GET["city"];
if($city==""){exit("城市为空。");}
$html = file_get_contents("https://restapi.amap.com/v3/weather/weatherInfo?city=".$city."&key=你的高德key");
$arr = json_decode($html, true); // 将获取到的 JSON 数据解析成数组
$province = $arr["lives"][0]["province"];
$city = $arr["lives"][0]["city"];
$adcode = $arr["lives"][0]["adcode"];
$weather = $arr["lives"][0]["weather"];
$temperature = $arr["lives"][0]["temperature"];
$winddirection = $arr["lives"][0]["winddirection"];
$windpower = $arr["lives"][0]["windpower"];
$humidity = $arr["lives"][0]["humidity"];
$reporttime = $arr["lives"][0]["reporttime"];
$temperature_float = $arr["lives"][0]["temperature_float"];
$humidity_float = $arr["lives"][0]["humidity_float"];
$data = array(
'province' => $province,
'city' => $city,
'adcode' => $adcode,
'weather' => $weather,
'temperature' => $temperature,
'winddirection' => $winddirection,
'windpower' => $windpower,
'humidity' => $humidity,
'reporttime' => $reporttime,
'temperature_float' => $temperature_float,
'humidity_float' => $humidity_float,
);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit();
1、API内必须要屏蔽PHP报错,否则key会有泄露的风险!!!
error_reporting(0);//屏蔽报错
2、这一段是获取输入的city参数,如果没有输入就直接返回“城市为空”。然后访问高德API将返回的数据进行json解析。
$city=$_GET["city"];
if($city==""){exit("城市为空。");}
$html = file_get_contents("https://restapi.amap.com/v3/weather/weatherInfo?city=".$city."&key=你的高德key");
3、将解析的json数据再一次封装成json数据。
$arr = json_decode($html, true); // 将获取到的 JSON 数据解析成数组
$province = $arr["lives"][0]["province"];
$city = $arr["lives"][0]["city"];
$adcode = $arr["lives"][0]["adcode"];
$weather = $arr["lives"][0]["weather"];
$temperature = $arr["lives"][0]["temperature"];
$winddirection = $arr["lives"][0]["winddirection"];
$windpower = $arr["lives"][0]["windpower"];
$humidity = $arr["lives"][0]["humidity"];
$reporttime = $arr["lives"][0]["reporttime"];
$temperature_float = $arr["lives"][0]["temperature_float"];
$humidity_float = $arr["lives"][0]["humidity_float"];
$data = array(
'province' => $province,
'city' => $city,
'adcode' => $adcode,
'weather' => $weather,
'temperature' => $temperature,
'winddirection' => $winddirection,
'windpower' => $windpower,
'humidity' => $humidity,
'reporttime' => $reporttime,
'temperature_float' => $temperature_float,
'humidity_float' => $humidity_float,
);
4、输出json数据,必须加上。JSON_UNESCAPED_UNICODE,禁止json编码Unicode。
echo json_encode($data, JSON_UNESCAPED_UNICODE);
TFT_GND----GNF
TFT_VCC----3.3V
TFT_MOSI----D23
TFT_SCLK----D18
TFT_CS----D5
TFT_DC----D2
TFT_RST----D4
TFT_BLK----3.3V
三、ESP32源码
源码下载 :
https://mbb.eet-china.com/download/316202.html
【不要疯狂调用API可能会被拉黑,请适当调整刷新时间】
源码内还做了ntp实时时间,30秒更新一次。大家可以自己设计ui界面,这里我已经帮大家做好了天气常用的字,以及常用天气的图标。
bmp.h文件存有常用天气的图标。
xem_font.h文件存有所有天气的中文字符。
1、ESP32配网,这里写wifi账号和密码,不能使用5g网络。
2、定义变量,大家可以根据自己需求设置。
3、配置TFT屏幕、配置ntp时钟、配置天气API,city请改成自己的城市代码。
4、当获取到数据之后,解析json,并存入变量内。
5、loop代码内就是一直检测天气状态,然后tft上显示内容,可以根据自己的需求更改。
高德开放平台第二期实战案例,三等奖作品
作者:孙嘉俊
仅代表作者个人观点