Snippets

Christian Ashby rpi-ds18b20

Updated by Former user

File temp.py Modified

  • Ignore whitespace
  • Hide word diff
 		self.sensorid = sensorid
 		self.sensordir = sensordir
 		self.currentTemp = -999
-		self.correctionFactor = 1
+		self.correctionFactor = 0
 		self.enabled = True
 		self.error = False
 		self.ready = False
Updated by Former user

File temp.py Modified

  • Ignore whitespace
  • Hide word diff
 
 from threading import Thread
 import time
-class Temp(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 = fileName
-	self.currentTemp = -999
-	self.correctionFactor = 1;
-	self.enabled = True
-	self.ready = False
+import sys
 
-def run(self):
-    while True:
-        if self.isEnabled():
-            try:
-                f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
-            except IOError as e:
-                print "Error: File " + self.tempDir + self.fileName + "/w1_slave" + " does not exist.";
-                return;
+class Temp(Thread):
+	"""
+	 A class for getting the current temp of a DS18B20
+	"""
+	def __init__(self, sensorid = None, sensordir='/sys/bus/w1/devices', group=None, target=None, name=None, verbose=None):
+		super(Temp, self).__init__(group=group, target=target, name=name, verbose=verbose)
+		self.sensorid = sensorid
+		self.sensordir = sensordir
+		self.currentTemp = -999
+		self.correctionFactor = 1
+		self.enabled = True
+		self.error = False
+		self.ready = False
+		self.daemon = True
 
-			lines=f.readlines()
-            crcLine=lines[0]
-            tempLine=lines[1]
-            result_list = tempLine.split("=")
+	def run(self):
+		while True:
+			if self.isEnabled():
+				filename = self.sensordir + "/" + self.sensorid + "/w1_slave"
+				try:
+					f = open(filename, 'r')
+				except IOError as e:
+					sys.stderr.write("Error: File " + filename + " does not exist.\n")
+					self.error = True
+					return
+				
+				lines=f.readlines()
+				crcLine=lines[0]
+				tempLine=lines[1]
+				result_list = tempLine.split("=")
 
-			temp = float(result_list[-1])/1000 # temp in Celcius
+				temp = float(result_list[-1])/1000 # temp in Celcius
 
-			temp = temp + self.correctionFactor # correction factor
+				temp = temp + self.correctionFactor # correction factor
 
-			#if you want to convert to fahrenheit, uncomment this line
-			#temp = (9.0/5.0)*temp + 32  
-            
-			if crcLine.find("NO") > -1:
-				temp = -999
+				#if you want to convert to fahrenheit, uncomment this line
+				#temp = (9.0/5.0)*temp + 32  
+				
+				if crcLine.find("NO") > -1:
+					temp = -999
+				else:
+					self.ready = True
 
-			self.currentTemp = temp
-			self.ready = True
-			#print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
+				self.currentTemp = temp
+				#print "Current: " + str(self.currentTemp) + " " + str(self.sensorid)
 
-			time.sleep(1)
+				time.sleep(1)
 
 	#returns the current temp for the probe
-    def getCurrentTemp(self):
-        return self.currentTemp
+	def getCurrentTemp(self):
+		return self.currentTemp
 
 	#setter to enable this probe
-    def setEnabled(self, enabled):
-        self.enabled = enabled
-    #getter       
-    def isEnabled(self):
-        return self.enabled
+	def setEnabled(self, enabled):
+		self.enabled = enabled
+	#getter	   
+	def isEnabled(self):
+		return self.enabled
 
 	#getter for ready check
 	def isReady(self):
 		return self.ready
 
+	#getter for error check
+	def isError(self):
+		return self.error
Updated by Former user

File temp.py Modified

  • Ignore whitespace
  • Hide word diff
      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 = fileName
-        self.currentTemp = -999
-        self.correctionFactor = 1;
-        self.enabled = True
+	Thread.__init__(self)
+	self.tempDir = '/sys/bus/w1/devices/'
+	self.fileName = fileName
+	self.currentTemp = -999
+	self.correctionFactor = 1;
+	self.enabled = True
+	self.ready = False
 
 def run(self):
     while True:
 				temp = -999
 
 			self.currentTemp = temp
+			self.ready = True
 			#print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
 
 			time.sleep(1)
     #getter       
     def isEnabled(self):
         return self.enabled
+
+	#getter for ready check
+	def isReady(self):
+		return self.ready
+
Updated by Former user

File temp.py Modified

  • Ignore whitespace
  • Hide word diff
         self.currentTemp = -999
         self.correctionFactor = 1;
         self.enabled = True
+
 def run(self):
-        while True:
-            if self.isEnabled():
-                try:
-                    f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
-                except IOError as e:
-                    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 Celcius
-temp = temp + self.correctionFactor # correction factor
-#if you want to convert to fahrenheit, uncomment this line
-                #temp = (9.0/5.0)*temp + 32  
-                
-                if crcLine.find("NO") > -1:
-                    temp = -999
-self.currentTemp = temp
-                #print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
-time.sleep(1)
-#returns the current temp for the probe
+    while True:
+        if self.isEnabled():
+            try:
+                f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
+            except IOError as e:
+                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 Celcius
+
+			temp = temp + self.correctionFactor # correction factor
+
+			#if you want to convert to fahrenheit, uncomment this line
+			#temp = (9.0/5.0)*temp + 32  
+            
+			if crcLine.find("NO") > -1:
+				temp = -999
+
+			self.currentTemp = temp
+			#print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
+
+			time.sleep(1)
+
+	#returns the current temp for the probe
     def getCurrentTemp(self):
         return self.currentTemp
-#setter to enable this probe
+
+	#setter to enable this probe
     def setEnabled(self, enabled):
         self.enabled = enabled
     #getter       
Created by Christian Ashby

File temp.py Added

  • Ignore whitespace
  • Hide word diff
+#!/usr/bin/env python
+# coding=utf-8
+
+"""
+Author: Christian Ashby
+September 2016
+@babelmonk
+Thanks to: http://raspbrew.tumblr.com/post/44456213110/reading-temperatures-on-a-raspberry-pi-using
+"""
+
+from threading import Thread
+import time
+class Temp(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 = fileName
+        self.currentTemp = -999
+        self.correctionFactor = 1;
+        self.enabled = True
+def run(self):
+        while True:
+            if self.isEnabled():
+                try:
+                    f = open(self.tempDir + self.fileName + "/w1_slave", 'r')
+                except IOError as e:
+                    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 Celcius
+temp = temp + self.correctionFactor # correction factor
+#if you want to convert to fahrenheit, uncomment this line
+                #temp = (9.0/5.0)*temp + 32  
+                
+                if crcLine.find("NO") > -1:
+                    temp = -999
+self.currentTemp = temp
+                #print "Current: " + str(self.currentTemp) + " " + str(self.fileName)
+time.sleep(1)
+#returns the current temp for the probe
+    def getCurrentTemp(self):
+        return self.currentTemp
+#setter to enable this probe
+    def setEnabled(self, enabled):
+        self.enabled = enabled
+    #getter       
+    def isEnabled(self):
+        return self.enabled
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.