This could be improved:
def read_sensor(sensorID): tempfile = open("/sys/bus/w1/devices/"+ sensorID +"/w1_slave") thetext = tempfile.read() tempfile.close() tempdata = thetext.split("\n") [1].split(" ")[9] temperature = float(tempdata[2:]) temp_sensor = temperature / 1000 return (temp_sensor)Using pathlib and regular expression and context manager.
from pathlib import Path import re DS18B20 = re.compile(r't=(\d{5})') # regex to get the temperature # seek for t=5-digits def read_sensor(sensorID): device = Path('/sys/bus/w1/devices') / str(sensorID) / 'w1_slave' with device.open() as fd: data = fd.read() match = DS18B20.search(data) # match is None, if nothing was found if not match: raise Exception('Could not read data from sensor {}'.format(sensorID)) temperature = float(match.group(1)) # group 1 is the first match in parenthesis return temperature / 1000BTW: I prefer single quotes. This does not mean, that you have to use single quotes.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!