Rrdtool Collectd exec with 1 wire usb temperature probe DS18B20 PL2303

I worked on how to get collectd exec working with a 1 wire usb DS18B20 PL2303 device

I bought this device on eBay Digital USB DS18B20 1wire -55+125C thermometer probe for linux win pc

You need to install or compile the firmware with usb serial support and the pl2303 module
plugin the temperature prob into the usb

lsmod |grep 2303 should the the following output

pl2303                 20480  0 
usbserial              28672  1 pl2303

also install the program digitemp and the collectd ecec module
to test the usb prob do the following

digitemp_DS9097 -i -s /dev/ttyUSB0 -q -c /etc/digitemp.conf
the output should be something like this

28FF641E39845968 : DS18B20 Temperature Sensor

ROM #0 : 28FF641E39845968

now you should be able to read the temperature

digitemp_DS9097 -a -q -c /etc/digitemp.conf
Aug 18 15:23:36 Sensor 0 C: 35.56 F: 96.01

The collectd exec module does not work with user root.
create a user collectd

collectd:Nbc7MK3509MnE:1001:100:collectd:/home/collectd:/bin/sh  >>  etc/passwd
add collectd user to group
usermod -a -G dialout collectd
cat /etc/group should have this line
dialout:x:20:collectd

now you need to make a program to collect the data
I choose /usr/bin/collectdexec

HOST=$COLLECTD_HOSTNAME
INTERVAL=$COLLECTD_INTERVAL

#[-z "$INTERVAL"] && INTERVAL=5
INTERVAL=20
INTERVAL=$(awk -v i=$INTERVAL 'BEGIN{print int(i)}')

while sleep $INTERVAL; do
#    val=$(dd if=/dev/urandom bs=1 count=1 2>/dev/null |apstats | awk '/Rx PHY errors/ {print $5}')
    val=$(digitemp_DS9097 -a -q -c /etc/digitemp.conf | awk -F 'F: ' '{print $2}')
    echo "PUTVAL \"$HOST/exec-usb_temp/temperature-usb\" interval=$INTERVAL N:$val"
    #echo "N:$val"
done

chmod 0755 /usr/bin/collectdexec

create file /usr/lib/lua/luci/statistics/rrdtool/definitions/exec.lua


module("luci.statistics.rrdtool.definitions.exec", package.seeall)

function rrdargs(graph, plugin, plugin_instance)
    -- For $HOSTNAME/exec-foo-bar/temperature_baz-quux.rrd, plugin will be
    -- "exec" and plugin_instance will be "foo-bar".  I guess graph will be
    -- "baz-quux"?  We may also be ignoring a fourth argument, dtype.
    if "usb_temp" == plugin_instance then
        return {
            title = "%H: USB temperature reading",
            vlabel = "Fahrenheit",
            data = {
                types = { "temperature" },
                options = {
                    temperature = {
                        title  = "Fahrenheit",
                        color  = "ff0000"
                    }
                }
            }
        }
    end
end

now configure exec with luci

script /usr/bin/collectdexec
user collectd
group dialout

save all of this and you should get this database

/tmp/rrd/$HOSTNAME/exec-usb_temp/temperature-usb.rrd

Hope this helps somebody very important are the rights of user collectd and tryout that it can execute the file /usr/bin/collectdexec

1 Like