File tree Expand file tree Collapse file tree
Raspberry_Pi_DS18B20_Temperature_Sensing Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- import board
1+ import glob
22import time
33
4- from adafruit_onewire .bus import OneWireBus
5- from adafruit_ds18x20 import DS18X20
4+ base_dir = '/sys/bus/w1/devices/'
5+ device_folder = glob .glob (base_dir + '28*' )[0 ]
6+ device_file = device_folder + '/w1_slave'
67
7- # Initialize one-wire bus on board pin D5.
8- ow_bus = OneWireBus (board .D4 )
8+ def read_temp_raw ():
9+ f = open (device_file , 'r' )
10+ lines = f .readlines ()
11+ f .close ()
12+ return lines
913
10- # Scan for sensors and grab the first one found.
11- ds18 = DS18X20 (ow_bus , ow_bus .scan ()[0 ])
14+ def read_temp ():
15+ lines = read_temp_raw ()
16+ while lines [0 ].strip ()[- 3 :] != 'YES' :
17+ time .sleep (0.2 )
18+ lines = read_temp_raw ()
19+ equals_pos = lines [1 ].find ('t=' )
20+ if equals_pos != - 1 :
21+ temp_string = lines [1 ][equals_pos + 2 :]
22+ temp_c = float (temp_string ) / 1000.0
23+ temp_f = temp_c * 9.0 / 5.0 + 32.0
24+ return temp_c , temp_f
1225
13- # Main loop to print the temperature every second.
1426while True :
15- print ('Temperature: {0:0.3f}C' . format ( ds18 . temperature ))
16- time .sleep (1.0 )
27+ print (read_temp ( ))
28+ time .sleep (1 )
You can’t perform that action at this time.
0 commit comments