import schedule
from pymysql import Connection
import pyecharts.options as opts
from pyecharts.charts import Gauge,Bar,Page,Line,Liquid
from pyecharts.components import Table
conn = Connection(
host="localhost",
port=3306,
user="root",
password="123456",
autocommit=True
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("bs")
def run():
sql = f"select UpdateTime from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result = reversed(result)
data = ""
for i in result:
UpdateTime = i.__str__()
UpdateTime = UpdateTime[2:17]
data+=UpdateTime
UpdateTime = data.replace("+",",")
UpdateTime = UpdateTime.strip(",")
print(UpdateTime)
UpdateTimelist = UpdateTime.split(',')
sql = f"select Weather from heweather where Num = (select max(Num) from heweather)"
cursor.execute(sql)
result=cursor.fetchall()
Weather = result.__str__()
Weather = Weather.split("'")
print(Weather[1])
sql = f"select WindDir from heweather where Num = (select max(Num) from heweather)"
cursor.execute(sql)
result=cursor.fetchall()
WindDir = result.__str__()
WindDir = WindDir.split("'")
print(WindDir[1])
sql = f"select WindScale from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result = reversed(result) # 反转
windscale = ""
for i in result:
WindScale = i.__str__()
WindScale = WindScale[1:3]
WindScale = WindScale.split(")")
WindScale = WindScale[0]
windscale += WindScale
windscale = windscale.strip(",")
print(windscale)
windscale = windscale.split(',')
sql = f"select Precip from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
precip=""
for i in result:
Precip = i.__str__()
Precip = Precip[1:5]
Precip = Precip.split(")")
Precip =Precip[0]
precip += Precip
precip = precip.strip(",")
print(precip)
precip = precip.split(',')
sql = f"select Pressure from heweather order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
pressure=""
for i in result:
Pressure = i.__str__()
Pressure = Pressure[1:7]
Pressure = Pressure.split(")")
Pressure =Pressure[0]
pressure += Pressure
pressure = pressure.strip(",")
print(pressure)
pressure = pressure.split(',')
sql = f"select Tem from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
tem=""
for i in result:
Tem = i.__str__()
Tem = Tem[1:5]
Tem = Tem.split(")")
Tem =Tem[0]
tem += Tem
tem = tem.strip(",")
print(tem)
temlist = tem.split(',')
sql = f"select Hum from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
hum=""
for i in result:
Hum = i.__str__()
Hum = Hum[1:5]
Hum = Hum.split(")")
Hum =Hum[0]
hum += Hum
hum = hum.strip(",")
print(hum)
humlist = hum.split(',')
sql = f"select UV from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
uv=""
for i in result:
UV = i.__str__()
UV = UV[1:5]
UV = UV.split(")")
UV =UV[0]
uv += UV
uv = uv.strip(",")
print(uv)
uv = uv.split(',')
sql = f"select PM from receive order by Num desc limit 12"
cursor.execute(sql)
result = cursor.fetchall()
result=reversed(result)#反转
pm=""
for i in result:
PM = i.__str__()
PM = PM[1:8]
PM = PM.split(")")
PM =PM[0]
pm += PM
pm = pm.strip(",")
print(pm)
pm = pm.split(',')
x_data = [UpdateTimelist[0], UpdateTimelist[1], UpdateTimelist[2], UpdateTimelist[3], UpdateTimelist[4],
UpdateTimelist[5], UpdateTimelist[6], UpdateTimelist[7], UpdateTimelist[8], UpdateTimelist[9],
UpdateTimelist[10], UpdateTimelist[11]
]
y_data1 = [windscale[0], windscale[1], windscale[2], windscale[3], windscale[4],
windscale[5], windscale[6], windscale[7], windscale[8], windscale[9],
windscale[10], windscale[11]
]
y_data2 = [precip[0],precip[1],precip[2],precip[3],precip[4],
precip[5],precip[6],precip[7],precip[8],precip[9],
precip[10],precip[11]
]
y_data3=[pressure[0],pressure[1],pressure[2],pressure[3],pressure[4],
pressure[5],pressure[6],pressure[7],pressure[8],pressure[9],
pressure[10],pressure[11]
]
y_data4=[temlist[0],temlist[1],temlist[2],temlist[3],temlist[4],
temlist[5],temlist[6],temlist[7],temlist[8],temlist[9],
temlist[10],temlist[11]
]
y_data5=[humlist[0],humlist[1],humlist[2],humlist[3],humlist[4],
humlist[5],humlist[6],humlist[7],humlist[8],humlist[9],
humlist[10],humlist[11]
]
y_data6=[uv[0],uv[1],uv[2],uv[3],uv[4],
uv[5],uv[6],uv[7],uv[8],uv[9],
uv[10],uv[11]
]
y_data7=[pm[0],pm[1],pm[2],pm[3],pm[4],
pm[5],pm[6],pm[7],pm[8],pm[9],
pm[10],pm[11]
]
attr = [x_data[11]]
v1 = [y_data1[11]]
v2 = [y_data2[11]]
v3 = [y_data3[11]]
v4 = [y_data6[11]]
v5 = [y_data7[11]]
def table_base() -> Table:
table = Table()
table.chart_id="0b661b17c86444b4b197426083dd2285"
headers = ["获取项","数据"]
rows = [
["UpdateTime",UpdateTimelist[11]],
["Weather",Weather[1]],
["WindDir",WindDir[1]],
["WindScale",windscale[11]]
]
table.add(headers, rows)
return table
def bar_datazoom_slider() -> Bar:
bar = Bar()
bar.chart_id="f9a9d6f1ad0748beb1cac2488fdf378a"
bar.add_xaxis(attr)
bar.add_yaxis("雨量", v2)
bar.add_yaxis("气压", v3)
bar.add_yaxis("UV", v4)
bar.add_yaxis("PM2.5", v5)
bar.set_global_opts(
title_opts=opts.TitleOpts(title="基于物联网的户外空气监测系统"),
)
return bar
def line_markpoint() -> Line:
line = Line()
line.chart_id="ae0c0ade615a4a79a86fa899ebed7544"
line.set_global_opts(
# tooltip_opts=opts.TooltipOpts(is_show=True,trigger="axis"),
legend_opts=opts.LegendOpts(is_show=True),
toolbox_opts=opts.ToolboxOpts(is_show=True,pos_left = "96%"),
xaxis_opts=opts.AxisOpts(type_="category", axislabel_opts={"rotate": 30}),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
),
)
line.add_xaxis(xaxis_data=x_data)
line.add_yaxis(
series_name="风力等级折线",
y_axis=y_data1,
symbol="emptyCircle",
color='orange',
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
)
line.add_yaxis(
series_name="雨量折线",
y_axis=y_data2,
symbol="emptyCircle",
color='red',
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=False),
)
line.add_yaxis(
series_name="气压折线",
y_axis=y_data3,
Esp32+Python获取天气数据+Pyechrts(Html)显示
需积分: 0 82 浏览量
更新于2023-05-30
1
收藏 19KB ZIP 举报
Esp32是一款功能强大的微控制器,它集成了Wi-Fi和蓝牙功能,非常适合IoT(物联网)应用。在本项目中,我们将使用Esp32通过Python编程来获取天气数据,并利用Pyecharts库将这些数据以Html形式进行可视化展示。Python是一种易学且功能丰富的编程语言,而Html则是网页开发的基础,Pyecharts则是一个基于JavaScript的Echarts图表库的Python接口,它使得在Python环境中创建复杂的图表变得简单。
我们要了解如何在Esp32上运行Python。Esp32通常与MicroPython或CircuitPython等轻量级Python实现一起使用,这些版本的Python被设计为在资源有限的硬件上运行。安装好固件后,你可以通过串口连接或者Wi-Fi网络来上传和执行Python脚本。
获取天气数据通常涉及使用API接口,如OpenWeatherMap、AccuWeather或Bing Weather等。你需要先注册并获取API密钥,然后使用Python的HTTP客户端库,如requests,向这些服务发送GET请求。例如,请求OpenWeatherMap的当前天气数据可能如下:
```python
import requests
api_key = "your_api_key"
city_id = "city_id" # 可以通过城市名查找
url = f"http://api.openweathermap.org/data/2.5/weather?id={city_id}&appid={api_key}"
response = requests.get(url)
weather_data = response.json()
```
解析返回的JSON数据后,你可以提取温度、湿度、风速等信息。这里我们假设天气数据已经处理并存储在变量`weather_data`中。
接下来,我们需要引入Pyecharts库来创建图表。首先确保在Esp32上已经安装了Pyecharts和必要的依赖。Pyecharts提供多种图表类型,如折线图、柱状图、饼图等。以创建一个简单的折线图展示温度变化为例:
```python
from pyecharts import options as opts
from pyecharts.charts import Line
# 假设我们有历史温度数据
dates = ["2022-01-01", "2022-01-02", "2022-01-03"]
temperatures = [10, 12, 8]
line = (
Line()
.add_xaxis(dates)
.add_yaxis("Temperature", temperatures, label_opts=opts.LabelOpts(position="top"))
.set_global_opts(title_opts=opts.TitleOpts(title="Temperature Over Time"))
)
# 将图表保存为HTML文件
line.render_html("temperature.html")
```
这段代码会生成一个名为"temperature.html"的文件,包含一个展示日期与温度的折线图。在Esp32上运行这段代码后,可以通过连接Esp32的Web服务器或者将其复制到其他设备上查看Html文件,以实现天气数据的可视化展示。
总结一下,这个项目涵盖了以下几个关键知识点:
1. Esp32的Python编程环境(MicroPython或CircuitPython)
2. 使用HTTP API获取天气数据(如OpenWeatherMap)
3. Python的requests库用于发送HTTP请求
4. JSON数据的解析与处理
5. Pyecharts库的使用,创建Html图表
6. Html文件的生成与展示
这个项目不仅提供了获取实时信息的能力,还展示了如何将IoT设备与数据可视化结合,是学习 Esp32、Python编程、API接口以及数据可视化的良好实践。
![avatar](https://profile-avatar.csdnimg.cn/f5c059e6d0304ab3aee4412cf9e5167c_qq_52303930.jpg!1)
前后端交互头秃7天患者
- 粉丝: 1
- 资源: 1
最新资源
- jdk1.8 Windows版本
- 智能网联实验小车的实验指导文档
- dwg cad 字体 shx 字体
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 快手无人直播变现项目玩法教程,直播间人气轻松破千上热门
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- 智能网联实验小车的实验指导文档
- Rust 编程语言的入门教程,适合有一定编程基础的学习者快速上手 教程分为基础语法、核心概念和实用工具三个部分
- 美妆产品进销存管理系统的设计与开发ssm.zip
- 同城绘本馆的设计与开发ssm.zip