Search code examples
pythonrrdtool

Python Updating RRDTool with Serial Port Data


I am trying to update a RRDTool DB with serial information. Is it possible to declare the serial data as a variable in the update line? Using the code below, rrdtool doesn't see the N: timestamp. However if I manually enter the data following the "N:" it will update.

import serial
import time
import numpy
import sys
import rrdtool


ser = serial.Serial('/dev/ttyUSB0', 9600)
time.sleep(1)
ser.flush()


for i in range(2):
    ser.readline()


while 1:    
    # Read data
    temp = ser.readline()
    ret = rrdtool.update('temperature.rrd', 'N:', temp)
    if ret:
     print rrdtool.error()
     time.sleep(5)
    quit()

Solution

  • I believe you want to do something like this:

    ret = rrdtool.update('temperature.rrd', 'N:%s' % temp)
    

    Each argument in an rrdtool wrapper function should correspond to an argument in the rrdtool cli command. So in your previous example when you were running rrdtool.update with 3 arguments you were actually running something like:

    rrdtool update temperature.rrd N: 65.6
    

    the update should be a single argument, so this is really what you wanted:

    rrdtool update temperature.rrd N:65.6