Python on Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing on Adafruit's Lern site, which I always refer to. There was a sample of temperature sensing of DS18B20 using. The W1ThermSensor library is introduced as an easy method, so let's try it. The license is GPLv2.
Check the kernel version.
$ uname -a
Linux raspberrypi 3.18.11+ #777 PREEMPT Sat Apr 11 17:24:23 BST 2015 armv6l GNU/Linux
The kernel is as described in 1-Wire in Parasite Power configuration (1-Wire using 2 wires) does not work in 3.18.3 # 348. For 3.18.1 +
and above, the setting to enable 1-Wire has been changed. Add the following line to /boot/config.txt
and reboot.
/boot/config.txt
dtoverlay=w1-gpio-pullup,gpiopin=4
First, try reading the w1_slave
file under/ sys / bus / w1 / devices /
from your Python code.
~/python_apps/w1-test.py
# -*- coding: utf-8 -*-
import os
import glob
import time
import subprocess
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('\n')
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
s = "celsius: {0:.3f}, fahrenheit: {1:.3f}"
temp = read_temp()
print(s.format(*temp))
time.sleep(5)
Run the program. I got Celsius and Fahrenheit and formatted them with 3 decimal places.
$ python w1-test.py
celsius: 27.312, fahrenheit: 81.162
celsius: 27.687, fahrenheit: 81.837
celsius: 27.500, fahrenheit: 81.500
Install W1ThermSensor by git clone
.
$ cd ~/python_apps
$ git clone https://github.com/timofurrer/w1thermsensor.git
$ cd w1thermsensor
$ sudo python setup.py install
Write a simple program. Sensing data can be acquired with a float with 3 digits after the decimal point.
~/python_apps/w1-test.py
# -*- coding: utf-8 -*-
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
celsius = sensor.get_temperature()
fahrenheit = sensor.get_temperature(W1ThermSensor.DEGREES_F)
all_units = sensor.get_temperatures([W1ThermSensor.DEGREES_C, W1ThermSensor.DEGREES_F, W1ThermSensor.KELVIN])
print("celsius: {0:.3f}".format(celsius))
print("fahrenheit: {0:.3f}".format(fahrenheit))
s = "celsius: {0:.3f}, fahrenheit: {1:.3f}, kelvin: {2:.3f}"
print(s.format(*all_units))
Run the program. The code is simplified because the library takes care of reading the w1_slave
file from the / sys / bus / w1 / devices
directory as in the first example.
$ python w1-test.py
celsius: 26.625
fahrenheit: 79.925
celsius: 26.625, fahrenheit: 79.925, kelvin: 299.775
Recommended Posts