I have a problem with returning temperature value from inside a function. I'm reading it from DS18B20 sensor and setup was done using this tutorial: http://www.cl.cam.ac.uk/projects/raspbe ... mperature/
Here is my code:
Code: Select all
#!/usr/bin/python3
# import libraries
import time
import subprocess
# define sensor serial number
sensorSerNum = '28-00000329361f'
# helper functions to run modules needed for sensor communication
def runModules():
subprocess.call(['modprobe', 'w1-gpio'])
subprocess.call(['modprobe', 'w1-therm'])
# helper function to read temperature
def readTemp():
try:
sensorFile = open('/sys/bus/w1/devices/' + sensorSerNum + '/w1_slave')
fileData = sensorFile.read()
sensorFile.close()
secondLine = fileData.splitlines()[1]
rawTemp = secondLine.split()[9]
temp = float(rawTemp[2:]) / 1000
print(temp)
return temp
# if file cannot be opened
except IOError:
runModules()
# give some time to load modules
time.sleep(0.1)
# make another attempt to read temperature
readTemp()
print(readTemp())
Code: Select all
25.187
None
Code: Select all
25.187
25.187