ESP32实时天气,高德天气查询API(全开源)

科技   科技   2024-09-05 08:01   广东  
效果展示
项目源码文件:https://mbb.eet-china.com/download/316202.html(或点击文末阅读原文

前言
(一)本项目全开源,包括接口地址,ESP32源码均开源,请尊重原创,转载、商用请联系xemowo@qq.com

(二)实时天气采用高德API,数据来源是中国气象局。个人认证用户有300000次/天的配额
高德API文档:https://lbs.amap.com/api/webservice/guide/api/weatherinfo

(三)为了防止key泄露,可以将高德API封装成自己的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

(四)本项目硬件采用
ESP32 wroom 32
1.8TFT_LCD(ST7735s)

(五)代码平台采用
Arduino
内置所有的天气中文字符,而且常用的天气都是适配了图标
bmp.h文件存有常用天气的图标
xem_font.h文件存有所有天气的中文字符

高德API
(一)注册高德开放平台的账号https://lbs.amap.com/
(二)应用管理----创建新应用
(三)添加key,选择web服务(请勿泄露key)
(四)下载城市代码表,查询自己的城市代码 https://wwmg.lanzouj.com/iGjyD1i9vf6b
比如我是中山市,表内搜索中山市,代码为442000
(五)高德天气API
API地址:https://restapi.amap.com/v3/weather/weatherInfo
参数:city=城市代码  key=密钥key
例:https://restapi.amap.com/v3/weather/weatherInfo?city=442000&key=c01d70381da92dee9c4f
封装API
以上的高德API已经可以使用了,但是如果你要分享给别人使用,建议将高德API封装一下,避免key泄露
我这里使用的是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, );
(四)输出json数据,必须加上 JSON_UNESCAPED_UNICODE,禁止json编码Unicode
echo json_encode($data, JSON_UNESCAPED_UNICODE);

ESP32和1.8TFT接线
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文件存有所有天气的中文字符

(一)ESP32配网,这里写wifi账号和密码,不能使用5g网络
(二)定义变量,大家可以根据自己需求设置
(三)配置TFT屏幕、配置ntp时钟、配置天气API,city请改成自己的城市代码
(四)当获取到数据之后,解析json,并存入变量内
(五)loop代码内就是一直检测天气状态,然后tft上显示内容,可以根据自己的需求更改
作者:小恶魔owo,来源:面包板社区


面包板社区
分享电子技术干货,工程师福利!EET电子工程专辑、ESM国际电子商情、EDN电子技术设计官方社区。
 最新文章