831e4aa2593ec0912607278c910cbdd482088fd0
[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 import socket
8
9 os.system('modprobe w1-gpio')
10 os.system('modprobe w1-therm')
11
12 base_dir = '/sys/bus/w1/devices/'
13 device_folder = glob.glob(base_dir + '28*')[0]
14 device_file = device_folder + '/w1_slave'
15
16 def read_temp_raw():
17 f = open(device_file, 'r')
18 lines = f.readlines()
19 f.close()
20 return lines
21
22 def read_temp():
23 lines = read_temp_raw()
24 while lines[0].strip()[-3:] != 'YES':
25 time.sleep(0.2)
26 lines = read_temp_raw()
27 equals_pos = lines[1].find('t=')
28 if equals_pos != -1:
29 temp_string = lines[1][equals_pos+2:]
30 temp_c = float(temp_string) / 1000.0
31 temp_f = temp_c * 9.0 / 5.0 + 32.0
32 temp_c_2dec = f"{temp_c:.5f}" # https://stackoverflow.com/a/15263885/10850071
33 #return temp_c, temp_f
34 return temp_c_2dec
35
36
37 myHostname = socket.gethostname()
38
39 while True:
40 nowTimeIso8601 = strftime("%Y%m%dT%H%M%S%z")
41 nowTimeUE = strftime("%s")
42 nowTemp = read_temp()
43 print(str(nowTimeIso8601) + ',' + str(nowTimeUE) + ',' + str(myHostname) + ',' + str(nowTemp))
44 time.sleep(1)