How to setup digitemp (1-wire) for use with VSCP

VSCP is constructed to use data from many different sources and make it possible to work with this data in a common way and also to collect and present this data in a common way. This is possible when the data is translated to the standardized version specified by VSCP. 1-Wire is a low-cost very popular way to obtain temperature, humidity, counts etc. This description shows how digitemp, a popular Unix 1-wire tool, can be used to deliver sensor data to VSCP.

Step 1

Install Digitemp.

On Debian

$ sudo apt-get install digitemp

Step 2

Initialize the adapter. For the DS2490 USB adapter use

$ sudo digitemp_DS2490 -i  

and for the serial adapter use

$ sudo digitemp -i -s /dev/ttyS0

if it is connected to /dev/ttyS0. This will generate a configuration file named .digitemprc in the folder you executed the command in. You should see something like this

root@oxygen:~# digitemp_DS2490 -i 
DigiTemp v3.6.0 Copyright 1996-2007 by Brian C. Lane
GNU General Public License v2.0 - http://www.digitemp.com
Found DS2490 device #1 at 002/012
Turning off all DS2409 Couplers
...
Searching the 1-Wire LAN
10A8AF9201080061 : DS1820/DS18S20/DS1920 Temperature Sensor
103D9D920108003C : DS1820/DS18S20/DS1920 Temperature Sensor
ROM #0 : 10A8AF9201080061
ROM #1 : 103D9D920108003C
Wrote .digitemprc

when it is run. Here two temperature sensors has been discovered.

the .digitemprc file look like

TTY USB
READ_TIME 1000
LOG_TYPE 1
LOG_FORMAT "%b %d %H:%M:%S Sensor %s C: %.2C F: %.2F"
CNT_FORMAT "%b %d %H:%M:%S Sensor %s #%n %C"
HUM_FORMAT "%b %d %H:%M:%S Sensor %s C: %.2C F: %.2F H: %h%%"
SENSORS 2
ROM 0 0x10 0xA8 0xAF 0x92 0x01 0x08 0x00 0x61
ROM 1 0x10 0x3D 0x9D 0x92 0x01 0x08 0x00 0x3C

Step 3

To read the temperature form all sensors issue

$ sudo digitemp_DS2490 -a

or for the serial adapter

$ sudo digitemp -a 

this will give something like this

root@oxygen:~# digitemp_DS2490 -a
DigiTemp v3.6.0 Copyright 1996-2007 by Brian C. Lane
GNU General Public License v2.0 - http://www.digitemp.com
Found DS2490 device #1 at 002/012
Sep 23 22:20:42 Sensor 0 C: 4.75 F: 40.55
Sep 23 22:20:43 Sensor 1 C: 22.12 F: 71.83

If you just want the data issue

$ sudo digitemp_DS2490 -a -q

which will give you

$ sudo digitemp_DS2490 -a -q
Sep 23 22:23:15 Sensor 0 C: 4.88 F: 40.77
Sep 23 22:23:16 Sensor 1 C: 22.12 F: 71.83    

We need to have the values at specific locations so edit the .digitemprc (the file is located in the same folder where you initialized the adapter above) file line LOG_FORMAT

so it looks

LOG_FORMAT "%.2C"  

this instead will give a file with a position for each temperature that is the same all the time and looks like

$ sudo digitemp_DS2490 -a -q
4.69
22.12

I have used a Celsius reading here but if you rather want a Fahrenheit reading replace the C with and F in the LOG_FORMAT.

Another version on the same theme can be

LOG_FORMAT "%R %.2C"  

where the sensor id is also given. The file will look as

$ sudo digitemp_DS2490 -a -q 
10A8AF9201080061 -1.25
103D9D920108003C 15.81

that is the sensor id will be included. This can be useful as a 1-wire sensor id directly can be translated into a VSCP GUID.

note If you want to use the script for humidity or counter data add a column to the line telling which sensor data type the line represent and add code to check for this line in the Python script.

Step 4

Now it to get this written to a file every minute you can create a file

$ sudo vim /etc/cron.d/dodigitemp

or with any name you like. In it add

* * * * * root cd /root;digitemp_DS2490 -a -q >tempreading

Here * * * * * says that we will that the command should be executed once every minute. root is the user name you want the script to be executed under and cd /root;digitemp_DS2490 -a -q >tempreading is the command to execute. In this case we execute the script as the root user (select a user that suites you) and we go to the folder where the .digitemprc file is located (in our case the root home folder “cd /root”) and lastly we execute the digitemp command with its output redirected to the file temp.

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

What we get is a file with the temperature readings updated every minute. The temperatures is positioned at constant locations in this file so we can read this file with the VSCP level II lmsensors driver. See Using the Level II lm-sensors driver for how to do this.

An alternative way is to use this python script

#!/usr/bin/python
"""
// File: sendvalues.py
//
// Usage: #!/usr/bin/python
"""
// File: sendvalues.py
//
// Usage: digitemp -a -q | send host user password
//
// Described here file:///home/akhe/development/sendvalues.py
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//
// This file is part of the VSCP (http://www.vscp.org)
//
// Copyright (C) 2000-2016
// Ake Hedman, Grodans Paradis AB, <akhe@grodansparadis.com>
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
//
"""

import getpass
import sys
import telnetlib
import sys

onewire_prefix = "FF:FF:FF:FF:FF:FF:FF:FF:"

if ( len(sys.argv) < 4 ):
    sys.exit("Wrong number of parameters - aborting")

host = sys.argv[1]
user = sys.argv[2]
password = sys.argv[3]

# Connet to VSCP daemon
tn = telnetlib.Telnet(host, 9598)
tn.read_until("+OK",2)

# Login
tn.write("user " + user + "\n")
tn.read_until("+OK", 2)

tn.write("pass " + password + "\n")
tn.read_until("+OK - Success.",2)

# For each line from piped digitemp output
for line in sys.stdin:

    guid = onewire_prefix
    event = "3,"    # Priority=normal
    event += "10,6,"    # Temperature measurement class=10, type=6
    event += "0,"    # Use interface timestamp
    event += "0,"      # Use obid of interface

    dtrow = line.split(" ")    # Separate id from temperature
    onewire_id = dtrow[0]    # save sensor id
    temperature = dtrow[1]    # Save temperature reading

    # Reverse temeprature id so MSB comes first as VSCP requires
    for i in range(7, -1, -1):
        guid += onewire_id[i*2:i*2+2]
        if ( 0 != i ):
            guid += ":"

    event += guid  + ","    # add GUID to event

    # datacoding = String format| Celsius | sensor 0
    datacoding = 0x40 | (1<<3) | 0
    event += hex(datacoding)    # Add datacoding byte to event

    # Make sure length is OK (max seven characters)
    temperature = temperature.strip()
    if ( len(temperature) > 7 ):
        temperature = temperature[0:7]

    # Write temperature into the event (not line breaks)
    for ch in temperature:
        if  ( ( 0x0a != ord(ch) ) and ( 0x0d != ord(ch) ) ):
            event += ","
            event += hex(ord(ch))

    # Send event to server
    tn.write("send " + event + "\n")
    tn.read_until("+OK - Success.",2)

tn.write("quit\n")

the script will send the read temperatures from the above configured Digitemp to a VSCP daemon. Usage is

digitemp -a -q | send host user password

and this command is preferably replacing the crontab command described above.

Step 5

When you managed to get events to the VSCP daemon it is then very easy to do things based on the sensor values. You can show them on a dynamic web page or start a program when a reading comes in or just log the data in a database or any other thing you may think of. Any solution you now come up with will have general usability. That is solve once use many times.

For instance you can now show the measurement values in flowing text like in this image

without any additional coding or you can show it in a live thermometer widget

and in diagrams etc.

* DigiTemp home - http://www.digitemp.com/ * Install Digitemp, a good instruction here on how to do this.

Go back

 
howto/how_to_setup_digitemp.txt · Last modified: 2016/12/19 10:01 by akhe
[unknown button type]
 
Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki