python 访问 InfluxDB 数据库

  我们开发了一个基于 Telegrapg+InfluxDB+chonograph+kapacitor 的气象站监控演示系统。

      通过modular-2 连接了一个气象站传感器,采集温度,湿度,PM2.5,PM1.0 ,照度,大气压等参数。通过MQTT 协议传送给Telegraph。Telegraph 传送到InluxDB。在一个物联网系统中,很重要的部分是对数据进行处理和分析。下面是通过Python来读取采集的数据。

使用python InfluxDB Client

 pip install influx-client

代码

  下面这个例子直接访问InfluxDB 数据库,读取 Temperature 和Humanity。

import pandas as pd
from datetime import datetime,timedelta
import matplotlib.pyplot as plt
from influxdb import InfluxDBClient
fig=plt.figure()
fig.suptitle("Weather Station")
ax1=fig.add_subplot(1,2,1)
ax1.set_title("Temperature")
ax2=fig.add_subplot(1,2,2)
ax2.set_title("Humidity")
client = InfluxDBClient('iot.maximlab.cn', '8086', 'maxim', '85135666', 'telegraf')  # 初始化
temp=pd.DataFrame(client.query('select "Temperature" ,"Humidity"  from "telegraf"."autogen"."mqtt_consumer" where time>now() - 24h ;').get_points()) 
#过滤掉错误信息
fh=temp[temp.Humidity>4]
#ax1.plot(ft['Temperature'],color="r")
#ax2.plot(fh['Humidity'],color='b')
#fh.plot()
temp.plot(ax=ax1)
fh.plot(ax=ax2)

显示的结果

猜你喜欢

转载自blog.csdn.net/yaojiawan/article/details/85094287