前言
(二)实时天气采用高德API,数据来源是中国气象局。个人认证用户有300000次/天的配额
(三)为了防止key泄露,可以将高德API封装成自己的API,然后可以分享给其他人使用,我这里已经封装好了,大家也可以使用我的API
天气API文档:http://api.xemowo.top/api/tqyb.html
只需要将城市代码改成自己的所在城市代码即可
城市代码表:https://wwmg.lanzouj.com/iGjyD1i9vf6b
(四)本项目硬件采用
1.8TFT_LCD(ST7735s)
(五)代码平台采用
内置所有的天气中文字符,而且常用的天气都是适配了图标
bmp.h文件存有常用天气的图标
xem_font.h文件存有所有天气的中文字符
高德API
参数:city=城市代码 key=密钥key
例:https://restapi.amap.com/v3/weather/weatherInfo?city=442000&key=c01d70381da92dee9c4f
我这里使用的是PHP语言,实现只需要输入城市代码。即可返回实时天气信息
http://api.xemowo.top/api/tqyb.php?city=442000
【大家懒的话,也可以直接使用我的API】
//有什么问题请联系xemowo@qq.com
//未经授权禁止商业、转载
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();
(一)API内必须要屏蔽PHP报错,否则key会有泄露的风险!!!
error_reporting(0);//屏蔽报错
(二)这一段是获取输入的city参数,如果没有输入就直接返回“城市为空”。然后访问高德API将返回的数据进行json解析
$city=$_GET["city"];
if($city==""){exit("城市为空。");}
$html = file_get_contents("https://restapi.amap.com/v3/weather/weatherInfo?city=".$city."&key=你的高德key");
(三)将解析的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,
);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
TFT_VCC----3.3V
TFT_MOSI----D23
TFT_SCLK----D18
TFT_CS----D5
TFT_DC----D2
TFT_RST----D4
TFT_BLK----3.3V
ESP32源码
大家可以自己设计ui界面,这里我已经帮大家做好了天气常用的字,以及常用天气的图标。
bmp.h文件存有常用天气的图标
xem_font.h文件存有所有天气的中文字符
(一)ESP32配网,这里写wifi账号和密码,不能使用5g网络