A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
Get the temperature from your w1 therm sensor in a single line of code!
It's designed to be used with the Rasperry Pi hardware but also works on a Beagle Bone and others.
Raspberry Pi: this package is available in Raspbian as
python-w1thermsensorand
python3-w1thermsensor.
Python 2 drop: all w1thermsensor releases from 2.0 are Python 3.5+
The following w1 therm sensor devices are supported:
The following hardware is needed:
On the Raspberry Pi, you will need to add
dtoverlay=w1-gpio"(for regular connection) or
dtoverlay=w1-gpio,pullup="y"(for parasitic connection) to your /boot/config.txt. The default data pin is GPIO4 (RaspPi connector pin 7), but that can be changed from 4 to
xwith
dtoverlay=w1-gpio,gpiopin=x.
After that, don't forget to reboot.
Raspi VCC (3V3) Pin 1 ----------------------------- VCC DS18B20 | | R1 = 4k7 ...10k | | Raspi GPIO 4 Pin 7 ----------------------------- Data DS18B20Raspi GND Pin 6 ----------------------------- GND DS18B20
This possibility is supported on all distributions:
pip install w1thermsensor
Note: maybe root privileges are required
Use the
asyncextra to add support for asyncio and
AsyncW1ThermSensor:
pip install w1thermsensor[async]
apt-get
If you are using the
w1thermsensormodule on a Rasperry Pi running Raspbian you can install it from the official repository:
sudo apt-get install python3-w1thermsensor
Note: For older versions of this package you might get the following error:
ImportError: No module named 'pkg_resources'which indicates that you need to install
python-setuptoolsor
python3-setuptoolsrespectively.
debuild -us -uc dpkg -i ../python3-w1thermsensor_*.deb
The usage is very simple and the interface clean.. All examples are with the
DS18B20sensor - It works the same way for the other supported devices.
from w1thermsensor import W1ThermSensor, Unitsensor = W1ThermSensor() temperature_in_celsius = sensor.get_temperature() temperature_in_fahrenheit = sensor.get_temperature(Unit.DEGREES_F) temperature_in_all_units = sensor.get_temperatures([ Unit.DEGREES_C, Unit.DEGREES_F, Unit.KELVIN])
The need kernel modules will be automatically loaded in the constructor of the
W1ThermSensorclass.
The first found sensor will be taken
The DS18B20 sensor with the ID
00000588806awill be taken.
from w1thermsensor import W1ThermSensor, Sensorsensor = W1ThermSensor(Sensor.DS18B20, "00000588806a") temperature_in_celsius = sensor.get_temperature()
With the
get_available_sensorsclass-method you can get the ids of all available sensors.
from w1thermsensor import W1ThermSensorfor sensor in W1ThermSensor.get_available_sensors(): print("Sensor %s has temperature %.2f" % (sensor.id, sensor.get_temperature()))
Only sensors of a specific therm sensor type:
from w1thermsensor import W1ThermSensor, Sensorfor sensor in W1ThermSensor.get_available_sensors([Sensor.DS18B20]): print("Sensor %s has temperature %.2f" % (sensor.id, sensor.get_temperature()))
Some w1 therm sensors support changing the resolution for the temperature reads.
w1thermsensorenables to do so with the
W1ThermSensor.set_resolution()method:
sensor = W1ThermSensor(Sensor.DS18B20, "00000588806a") sensor.set_resolution(9)
If the
persistargument is set to
Falsethis value is "only" stored in the volatile SRAM, so it is reset when the sensor gets power-cycled.
If the
persistargument is set to
Truethe current set resolution is stored into the EEPROM. Since the EEPROM has a limited amount of writes (>50k), this command should be used wisely.
sensor = W1ThermSensor(Sensor.DS18B20, "00000588806a") sensor.set_resolution(9, persist=True)
Note: this is supported since Linux Kernel 4.7
Note: this requires
rootprivileges
Upon import of the
w1thermsensorpackage the
w1-thermand
w1-gpiokernel modules get loaded automatically. This requires the python process to run as root. Sometimes that's not what you want, thus you can disable the auto loading and load the kernel module yourself prior to talk to your sensors with
w1thermsensor.
You can disable the auto loading feature by setting the
W1THERMSENSOR_NO_KERNEL_MODULEenvironment variable to
1:
# set it globally for your shell so that sub-processes will inherit it. export W1THERMSENSOR_NO_KERNEL_MODULE=1set it just for your Python process
W1THERMSENSOR_NO_KERNEL_MODULE=1 python my_awesome_thermsensor_script.py
Every other values assigned to
W1THERMSENSOR_NO_KERNEL_MODULEwill case
w1thermsensorto load the kernel modules.
Note: the examples above also apply for the CLI tool usage. See below.
The
w1thermsensorpackage implements an async interface
AsyncW1ThermSensorfor asyncio.
The following methods are supported:
get_temperature()
get_temperatures()
get_resolution()
For example:
from w1thermsensor import AsyncW1ThermSensor, Unitsensor = AsyncW1ThermSensor() temperature_in_celsius = await sensor.get_temperature() temperature_in_fahrenheit = await sensor.get_temperature(Unit.DEGREES_F) temperature_in_all_units = await sensor.get_temperatures([ Unit.DEGREES_C, Unit.DEGREES_F, Unit.KELVIN])
The w1thermsensor module can be used as CLI tool since version
0.3.0.
sudo apt-get install python3-w1thermsensor)
List all available sensors:
$ w1thermsensor ls $ w1thermsensor ls --json # show results in JSON format
List only sensors of a specific type:
$ w1thermsensor ls --type DS1822 $ w1thermsensor ls --type DS1822 --type MAX31850K # specify multiple sensor types $ w1thermsensor ls --type DS1822 --json # show results in JSON format
Show temperature of all available sensors: (Same synopsis as
ls)
$ w1thermsensor all --type DS1822 $ w1thermsensor all --type DS1822 --type MAX31850K # specify multiple sensor types $ w1thermsensor all --type DS1822 --json # show results in JSON format
Show temperature of a single sensor:
$ w1thermsensor get 1 # 1 is the id obtained by the ls command $ w1thermsensor get --hwid 00000588806a --type DS18B20 $ w1thermsensor get 1 # show results in JSON format
Show temperature of a single sensor in the given resolution
$ w1thermsensor get 1 --resolution 10 $ w1thermsensor get --hwid 00000588806a --type DS18B20 --resolution 11
# w1thermsensor resolution 10 1 # w1thermsensor resolution --hwid 00000588806a --type DS18B20 11
Note: this requires
rootprivileges
I'm happy about all types of contributions to this project! :beers:
This project is published under MIT.
A Timo Furrer project.
- :tada: -