已解决,进一步添加解决方案我一直在尝试用PyRRD从我连接到树莓派的热传感器的输出创建一个图表,但是没有运气得到实际数据显示在图表中(但是创建了png文件)。我不确定这样做是否正确,但这段代码每秒都会输出温度,因此while循环至少可以工作。
import os
import glob
import time
import subprocess
# RDD-imports
from pyrrd.graph import DEF, CDEF, VDEF
from pyrrd.graph import LINE, AREA, GPRINT
from pyrrd.graph import ColorAttributes, Graph
from pyrrd.rrd import DataSource, RRA, RRD
# Sensor-stuff
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# RRD-stuff
startTime = int(time.time())
filename = 'temptest.rrd'
dataSources = []
rras = []
dataSource = DataSource(dsName='temp', dsType='DERIVE', heartbeat=5)
dataSources.append(dataSource)
rra1 = RRA(cf='AVERAGE', xff=0.5, steps=1, rows=5)
rra2 = RRA(cf='AVERAGE', xff=0.5, steps=6, rows=10)
rras.extend([rra1, rra2])
myRRD = RRD(filename, ds=dataSources, rra=rras, start=startTime)
myRRD.create()
# Graph-making
graphfile = 'tempgraf.png'
def1 = DEF(rrdfile=myRRD.filename, vname='mytemp', dsName=dataSource.name)
# Data going into green field
cdef1 = CDEF(vname='temp', rpn='%s,3600,*' % def1.vname)
# Line for max value
line1 = LINE(value=30, color='#990000', legend='Max temp allowed')
# Green area
area1 = AREA(defObj=cdef1, color='#006600', legend='Temp')
def read_temp_raw():
catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('n')
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
myRRD.bufferValue(int(time.time()), int(temp_c))
myRRD.update()
return temp_c
while True:
print(read_temp())
g = Graph(graphfile, start=startTime, end=int(time.time()), vertical_label='Temp(c)')
g.data.extend([def1, cdef1, line1, area1])
g.write()
time.sleep(1)
我一直在做实验,读了很多RRD手册和初学者教程,但我就是不能得到这个正确的。我很不确定#图形制作部分的rpn内容。请帮帮我:)如果有更好的方法,请告诉我!
解决方案(我的问题):放弃PyRRD并尝试使用rrdtools自己的python实现。http://oss.oetiker.ch/rrdtool/prog/rrdpython.en.html
我在程序之外创建了数据库,并在终端(Linux)中像这样正确地设置了步骤:
rrdtool create dailyTemp.rrd --step 5
DS:temp:GAUGE:10:-100:200
RRA:AVERAGE:0.5:1:2880 RRA:MAX:0.9:1:2880
然后我删除了连接到PyRRD的所有代码,只是添加了一些导入行和一行用于rrdtool更新。更干净,现在我可以创建我的图表:D下面是"最终"代码:
import os
import glob
import time
import subprocess
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages/')
import rrdtool, tempfile
# Sensor-stuff
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
# RRD-stuff, not specific
startTime = int(time.time())
def read_temp_raw():
catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('n')
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
print(int(temp_c))
print(int(time.time()))
rrdtool.update('dailyTemp.rrd','N:' + `temp_c`)
return temp_c
while True:
print(read_temp())
time.sleep(5)
我还没有在代码中实现图形创建,但可以打印出过去2小时的读数:
rrdtool graph temp120.png --end now --start end-7200s --width 400
DEF:ds0a=dailyTemp.rrd:temp:AVERAGE
图表结果(工作正在进行中):获得所需声誉后立即添加图片(10)
这是另一种获取温度值然后更新RRD的方法…
#!/usr/bin/python
# reads 1-wire DS18B20 temperature sensors and outputs options for rrdupdate
import os
import glob
import rrdtool
sensors = ( {'aab8': { # last four characters of sensor ID
'DS': 'DSName1', # DS name in rrd
'value': 0
},
'5cc3': {
'DS': 'DSName2',
'value': 0
},
'9ce0': {
'DS': 'DSName3',
'value': 0
}
} )
RRD='/path/to/my.rrd'
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
DS_list = ""
value_list = "N:"
for index, sensor in enumerate(device_folder):
for k, v in sensors.items():
if sensor[-4:] == k:
f = open(sensor + '/w1_slave', 'r')
lines = f.readlines()
f.close()
if lines[0].strip()[-3:] == 'YES':
v['value'] = float(lines[1][lines[1].find('t=')+2:])/1000.0
DS_list += v['DS'] + ':'
value_list += str(v['value']) + ':'
DS_list = DS_list[:-1]
value_list = value_list[:-1]
rrdtool.update(RRD, '--template', DS_list, value_list)
看起来您与rrdtool混淆了。我建议您首先使用rrdtool手动生成图形。一旦你理解了它的功能,你就可以转到pyrrd了。
这些url包含了关于rrd的非常好的文档
http://oss.oetiker.ch/rrdtool/doc/index.en.html如果您已经生成了rrd,请使用以下url并尝试手动生成图形。
http://oss.oetiker.ch/rrdtool/doc/rrdgraph.en.html既然你提到空png文件正在生成,请确保数据正在正确更新到rrd。使用rrdtool fetch查看rrd中是否有实际数据被更新。
http://oss.oetiker.ch/rrdtool/doc/rrdfetch.en.html我在创建像你这样的rrd文件时挂在"step"设置上。
必须在创建RRD文件时设置:
myRRD = RRD(filename, step=5, ds=dataSources, rra=roundRobinArchives, start=datetime.fromtimestamp(time.time()), step=60)
myRRD.create()
不像我最初想的那样在数据源中…
我在PyRRD