Skip to content

Instantly share code, notes, and snippets.

@ustropo
Created March 26, 2020 22:39
Show Gist options
  • Save ustropo/261960759b4e7d5486e097e7419a47ad to your computer and use it in GitHub Desktop.
Save ustropo/261960759b4e7d5486e097e7419a47ad to your computer and use it in GitHub Desktop.
Exemplo de parser para o arquivo de leitura do DS18B20
def __read_temp_file():
'''Carrega todas linhas do arquivo criado pelo sensor.'''
devices_dir = pathlib.Path('/sys/bus/w1/devices')
sensor_file = devices_dir / self._id / 'w1_slave'
# Le todas as linhas
f = open(sensor_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
'''Read temperature from 1-Wire sensor'''
lines = __read_temp_file()
# Uma leitura válida possui a string "YES"
# no final da primeira linha
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.1)
lines = __read_temp_file()
# A segunda linha contém a temperatura lida
# após o texto t=
tindex = lines[1].find('t=')
if tindex == -1:
return
temp_string = lines[1].strip()[tindex+2:]
temp = float(temp_string) / 1000.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment