sudo sed -i -e '$a dtoverlay=w1-gpio' /boot/config.txt
sudo raspi-config
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/
ls
cd [SENSORBEZEICHNUNG z.B. 28-239a3d1e64ff]
cat w1_slave
Pythonskript (1)
nano DS18B20classfile.py
Skript einfügen:
import os
import glob
import time
class DS18B20:
def __init__(self):
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
self._count_devices = len(device_folder)
self._devices = list()
i = 0
while i < self._count_devices:
self._devices.append(device_folder[i] + '/w1_slave')
i += 1
def device_names(self):
names = list()
for i in range(self._count_devices):
names.append(self._devices[i])
temp = names[i][20:35]
names[i] = temp
return names
def _read_temp(self, index):
f = open(self._devices[index], 'r')
lines = f.readlines()
f.close()
return lines
def tempC(self, index=0):
lines = self._read_temp(index)
retries = 5
while (lines[0].strip()[-3:] != 'YES') and (retries > 0):
time.sleep(0.1)
lines = self._read_temp(index)
retries -= 1
if retries == 0:
return 998
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp = lines[1][equals_pos + 2:]
return float(temp) / 1000
else:
return 999 # error
def device_count(self):
return self._count_devices
Codequelle: AZ-Delivery
Pythonskript (2)
nano DS18B20multiple.py
Skript einfügen:
import time
from DS18B20classfile import DS18B20
degree_sign = u'\xb0' # degree sign
devices = DS18B20()
count = devices.device_count()
names = devices.device_names()
print('[press ctrl+c to end the script]')
try: # Main program loop
while True:
i = 0
print('\nReading temperature, number of sensors: {}'
.format(count))
while i < count:
container = devices.tempC(i)
print('{}. Temp: {:.3f}{}C, {:.3f}{}F of the device {}'
.format(i+1, container, degree_sign,
container * 9.0 / 5.0 + 32.0, degree_sign,
names[i]))
i = i + 1
time.sleep(1)
# Scavenging work after the end of the program
except KeyboardInterrupt:
print('Script end!')
Codequelle: AZ-Delivery
Skript ausführen
python3 DS18B20multiple.py