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:
#!/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)