Skip to content

Instantly share code, notes, and snippets.

@johnmarc
Created October 6, 2015 17:27
Show Gist options
  • Save johnmarc/6ef21d74b67301d4394f to your computer and use it in GitHub Desktop.
Save johnmarc/6ef21d74b67301d4394f to your computer and use it in GitHub Desktop.
import os
import glob
import time
from stathat import StatHat
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
stathat = StatHat()
device_file = []
for folder in device_folder:
device_file.append(folder + '/w1_slave')
def read_temp_raw(file):
f = open(file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(file):
lines = read_temp_raw(file)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw(file)
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
rad_c, rad_f = read_temp(device_file[0])
amb_c, amb_f = read_temp(device_file[1])
stathat.ez_post_value('XXXX', 'radiator temp', rad_f)
stathat.ez_post_value('XXXX', 'ambient temp', amb_f)
#print "Radiator is currently %.2f room is current %.2f" % (rad_f, amb_f)
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment