#!/usr/bin/env python# coding=utf-8"""Author: Christian AshbySeptember 2016@babelmonkThanks to: http://raspbrew.tumblr.com/post/44456213110/reading-temperatures-on-a-raspberry-pi-using"""fromthreadingimportThreadimporttimeclassTemp(Thread):""" A class for getting the current temp of a DS18B20 """def__init__(self,fileName):Thread.__init__(self)self.tempDir='/sys/bus/w1/devices/'self.fileName=fileNameself.currentTemp=-999self.correctionFactor=1;self.enabled=Trueself.ready=Falsedefrun(self):whileTrue:ifself.isEnabled():try:f=open(self.tempDir+self.fileName+"/w1_slave",'r')exceptIOErrorase:print"Error: File "+self.tempDir+self.fileName+"/w1_slave"+" does not exist.";return;lines=f.readlines()crcLine=lines[0]tempLine=lines[1]result_list=tempLine.split("=")temp=float(result_list[-1])/1000# temp in Celciustemp=temp+self.correctionFactor# correction factor#if you want to convert to fahrenheit, uncomment this line#temp = (9.0/5.0)*temp + 32 ifcrcLine.find("NO")>-1:temp=-999self.currentTemp=tempself.ready=True#print "Current: " + str(self.currentTemp) + " " + str(self.fileName)time.sleep(1)#returns the current temp for the probedefgetCurrentTemp(self):returnself.currentTemp#setter to enable this probedefsetEnabled(self,enabled):self.enabled=enabled#getter defisEnabled(self):returnself.enabled#getter for ready checkdefisReady(self):returnself.ready
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.