21a727b00112ff634819551f37d30c94c2e80054
[EVA-2020-02.git] / exec / temperature / DS18B20..temp_poll.py
1 #!/usr/bin/env python3
2
3 import os
4 import glob
5 import time
6 from time import strftime
7
8 os.system('modprobe w1-gpio')
9 os.system('modprobe w1-therm')
10
11 base_dir = '/sys/bus/w1/devices/'
12 device_folder = glob.glob(base_dir + '28*')[0]
13 device_file = device_folder + '/w1_slave'
14
15 def read_temp_raw():
16 f = open(device_file, 'r')
17 lines = f.readlines()
18 f.close()
19 return lines
20
21 def read_temp():
22 lines = read_temp_raw()
23 while lines[0].strip()[-3:] != 'YES':
24 time.sleep(0.2)
25 lines = read_temp_raw()
26 equals_pos = lines[1].find('t=')
27 if equals_pos != -1:
28 temp_string = lines[1][equals_pos+2:]
29 temp_c = float(temp_string) / 1000.0
30 temp_f = temp_c * 9.0 / 5.0 + 32.0
31 temp_c_2dec = f"{temp_c:.5f}" # https://stackoverflow.com/a/15263885/10850071
32 #return temp_c, temp_f
33 return temp_c_2dec
34
35 while True:
36 nowTimeIso8601 = strftime("%Y%m%dT%H%M%S%z")
37 nowTimeUE = strftime("%s")
38 nowTemp = read_temp()
39 print(str(nowTimeIso8601) + ',' + str(nowTimeUE) + ',' + str(nowTemp))
40 time.sleep(1)