524bc96accd8b10e51f2ccaef1eca039c8a964e9
2 # Desc: Prints time and temperature reading
3 # Output: [date (ISO-8601)],[date (Unix Epoch)],[temp (°C)]
5 # Ref/Attrib: Raspberry Pi Temprature Sensor using the DS18B20; https://pimylifeup.com/raspberry-pi-temperature-sensor/
10 from time
import strftime
13 os
.system('modprobe w1-gpio')
14 os
.system('modprobe w1-therm')
16 base_dir
= '/sys/bus/w1/devices/'
17 device_folder
= glob
.glob(base_dir
+ '28*')[0]
18 device_file
= device_folder
+ '/w1_slave'
21 f
= open(device_file
, 'r')
27 lines
= read_temp_raw()
28 while lines
[0].strip()[-3:] != 'YES':
30 lines
= read_temp_raw()
31 equals_pos
= lines
[1].find('t=')
33 temp_string
= lines
[1][equals_pos
+2:]
34 temp_c
= float(temp_string
) / 1000.0
35 temp_f
= temp_c
* 9.0 / 5.0 + 32.0
36 temp_c_2dec
= f
"{temp_c:.5f}" # https://stackoverflow.com/a/15263885/10850071
37 #return temp_c, temp_f
41 myHostname
= socket
.gethostname()
44 nowTimeIso8601
= strftime("%Y%m%dT%H%M%S%z")
45 nowTimeUE
= strftime("%s")
47 print(str(nowTimeIso8601
) + ',' + str(nowTimeUE
) + ',' + str(myHostname
) + ',' + str(nowTemp
), flush
=True) # flush to stdout continuously (e.g. without buffer) https://stackoverflow.com/a/14729823