How to add a ds18b20 thermometer to raspberry pi and weewx

581 views
Skip to first unread message

Horacio

unread,
May 21, 2016, 5:37:25 PM5/21/16
to weewx-user
Hello:

After reading how to add a new sensor to weewx I wrote this little program to get values from ds18b20 in a raspberry pi and I wanted to share it. I am sure it could be more efficient and elegant but it seems to work fine.

Create a file named ds18b20.py in /usr/share/weewx/user if weewx was installed from a .deb file. 

Then, add this line in section [[services]] in the weewx.conf file as follows

data_services = user.ds18b20.ds18b20_Service

As a result the extraTemp1 value is available in weewx. It is possible to change the event label in the third line from the bottom of ds18b20.py

On how to add a ds18b20 to raspberry pi:

On how to add an extra sensor to weewx:

Thanks!

#!/usr/bin/python

import os
import time
import syslog
import weewx
from weewx.wxengine import StdService

class ds18b20_Service(StdService):
    def __init__(self, engine, config_dict):
        super(ds18b20_Service, self).__init__(engine, config_dict)      
        d = config_dict.get('ds18b20_Service', {})
        self.filename = d.get('filename', '/sys/bus/w1/devices/28-011564ae1bff/w1_slave')
        syslog.syslog(syslog.LOG_INFO, "ds18b20: using %s" % self.filename) 
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.read_temp)

    def read_file(self):
        f = open(self.filename, 'r')
        lines = f.readlines()
        f.close()
        return lines

    def read_raw(self):
        lines = self.read_file()
        while lines[0].strip()[-3:] != 'YES':
            time.sleep(0.2)
            lines = self.read_file()
        temp_output = lines[1].find('t=')

        if temp_output != -1:
           temp_string = lines[1].strip()[temp_output+2:]
           temp_c = float(temp_string) / 1000.0
           temp_f = temp_c * 9.0 / 5.0 + 32.0
           return temp_f

    def read_temp(self, event):
        try:
            value = self.read_raw()    
            syslog.syslog(syslog.LOG_DEBUG, "ds18b20: found value of %s" % value)
            event.record['extraTemp1'] = value
        except Exception, e:
            syslog.syslog(syslog.LOG_ERR, "ds18b20: cannot read value: %s" % e)

Ashley Hinton

unread,
Jun 2, 2022, 12:09:29 PM6/2/22
to weewx-user
Hello

I just wanted to say thank you for this, I recently decided to rationalise my two Rasp Pi's into one. One of them was running weewx using the Interceptor driver to read my weather station meanwhile the other Rasp Pi ran OWFS which weewx then read across the network to grab a couple of extra temperature sensors. Decided to use 1-wire directly on the GPIO of the same Rasp Pi as runs weewx.

Tried OWFS with w1 as a source but it was turning out to be a bit of a nightmare just for reading 2x sensors so found your code instead.

Had to change the line [code] except Exception, e: [/code] to [code] except Exception as e: [/code] and obviously add my own sensors to both services "greenhousetemp.py and heaksinktemp.py"

My two sensors are in the greenhouse: one for the ambient temperature and the other poked down an exit hole from the heatsink ballast shingle under the floor.
With this I think I could probably add a soil moisture sensor pretty easily.

Anyway just wanted to say thanks!

Cheers

Ashley
Reply all
Reply to author
Forward
0 new messages