• Stars
    star
    485
  • Rank 87,519 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created about 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

W1ThermSensor

Gitter

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.


CI PyPI version codecov.io Code style: black

Raspberry Pi: this package is available in Raspbian as python-w1thermsensor and python3-w1thermsensor.

Python 2 drop: all w1thermsensor releases from 2.0 are Python 3.5+

Supported devices

The following w1 therm sensor devices are supported:

  • DS18S20
  • DS1822
  • DS18B20
  • DS28EA00
  • DS1825/MAX31850K

Setup

The following hardware is needed:

  • w1 therm compatible sensor (some of them can be bought here: Adafruit: DS18B20)
  • wires to connect the sensor to your board (you might need a breadboard, too)
  • a board like the Raspberry Pi or the Beagle Bone

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 x with dtoverlay=w1-gpio,gpiopin=x.

After that, don't forget to reboot.

Hardware-connection

Raspi VCC (3V3) Pin 1 -----------------------------   VCC    DS18B20
                                               |
                                               |
                                               R1 = 4k7 ...10k
                                               |
                                               |
Raspi GPIO 4    Pin 7 -----------------------------   Data   DS18B20
       (BCM)    (BOARD)

Raspi GND       Pin 6 -----------------------------   GND    DS18B20

Soft-pull-up

Alternatively to the hardware pull-up made by a physical resistor, or to the above mentioned software configuration dtoverlay=w1-gpio,pullup="y" in /boot/config.txt, the following soft pull-up can be used:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

When using this software pull-up, 1-Wire devices will be visible to the kernel only while the program pulls the GPIO pin up.

Hw device connection verification

Run the following command:

ls -l /sys/bus/w1/devices

You should check the availability of one or more filenames starting with "28-".

Filenames starting with "00-" possibly mean that the pull-up resistor is missing.

1-Wire devices can be plugged in dynamically and are visible to the kernel driver just after their hw connection.

To test reading the temperature, issue the following command:

for i in /sys/bus/w1/devices/28-*; do cat $i/w1_slave; done

Installation

From PIP

This possibility is supported on all distributions:

pip install w1thermsensor

Note: maybe root privileges are required

Use the async extra to add support for asyncio and AsyncW1ThermSensor:

pip install w1thermsensor[async]

On Raspbian using apt-get

If you are using the w1thermsensor module 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-setuptools or python3-setuptools respectively.

Manually build and install the debian package

debuild -us -uc
dpkg -i ../python3-w1thermsensor_*.deb

Usage as python package

The usage is very simple and the interface clean.. All examples are with the DS18B20 sensor - It works the same way for the other supported devices.

Basic usage with one sensor (implicit)

from w1thermsensor import W1ThermSensor, Unit

sensor = 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 W1ThermSensor class.
If something went wrong an exception is raised.

The first found sensor will be taken

Basic usage with one sensor (explicit)

The DS18B20 sensor with the ID 00000588806a will be taken.

from w1thermsensor import W1ThermSensor, Sensor

sensor = W1ThermSensor(sensor_type=Sensor.DS18B20, sensor_id="00000588806a")
temperature_in_celsius = sensor.get_temperature()

Multiple sensors

With the get_available_sensors class-method you can get the ids of all available sensors.

from w1thermsensor import W1ThermSensor

for 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, Sensor

for sensor in W1ThermSensor.get_available_sensors([Sensor.DS18B20]):
    print("Sensor %s has temperature %.2f" % (sensor.id, sensor.get_temperature()))

Set sensor resolution

Some w1 therm sensors support changing the resolution for the temperature reads. w1thermsensor enables to do so with the W1ThermSensor.set_resolution() method:

sensor = W1ThermSensor(sensor_type=Sensor.DS18B20, sensor_id="00000588806a")
sensor.set_resolution(9)

If the persist argument is set to False this value is "only" stored in the volatile SRAM, so it is reset when the sensor gets power-cycled.

If the persist argument is set to True the 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_type=Sensor.DS18B20, sensor_id="00000588806a")
sensor.set_resolution(9, persist=True)

Note: this is supported since Linux Kernel 4.7
Note: this requires root privileges

Disable kernel module auto loading

Upon import of the w1thermsensor package the w1-therm and w1-gpio kernel 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_MODULE environment variable to 1:

# set it globally for your shell so that sub-processes will inherit it.
export W1THERMSENSOR_NO_KERNEL_MODULE=1

# set 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_MODULE will case w1thermsensor to load the kernel modules.

Note: the examples above also apply for the CLI tool usage. See below.

Async Interface

The w1thermsensor package implements an async interface AsyncW1ThermSensor for asyncio.

The following methods are supported:

  • get_temperature()
  • get_temperatures()
  • get_resolution()

For example:

from w1thermsensor import AsyncW1ThermSensor, Unit

sensor = 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])

Usage as CLI tool

The w1thermsensor module can be used as CLI tool since version 0.3.0.
Please note that the CLI tool will only get installed with the Raspbian Python 3 package (sudo apt-get install python3-w1thermsensor)

List sensors

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 temperatures

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

Change temperature read resolution and write to EEPROM

# w1thermsensor resolution 10 1
# w1thermsensor resolution --hwid 00000588806a --type DS18B20 11

Note: this requires root privileges

Contribution

I'm happy about all types of contributions to this project! 🍻


This project is published under MIT.
A Timo Furrer project.
- πŸŽ‰ -

More Repositories

1

awesome-asyncio

A curated list of awesome Python asyncio frameworks, libraries, software and resources
4,418
star
2

try

Dead simple CLI tool to try Python packages - It's never been easier! πŸ“¦
Python
705
star
3

colorful

Terminal string styling done right, in Python 🐍 πŸŽ‰
Python
520
star
4

russian-roulette

πŸ€ You want to push your luck? ... Go ahead and try your best with this CLI russian roulette! πŸ’₯
Shell
111
star
5

shellfuncs

Python API to execute shell functions as they would be Python functions
Python
101
star
6

pandoc-plantuml-filter

Pandoc filter for PlantUML code blocks
Python
99
star
7

observable

minimalist event system for Python
Python
86
star
8

WOL

C program to send wol magic packets
C
79
star
9

pandoc-mermaid-filter

Pandoc filter for mermaid code blocks
Python
71
star
10

devheart

Listen to Tux's heartbeat with this awesome Linux Kernel Module ❀️
C
58
star
11

git-russian-roulette

πŸ€ play russian roulette in your git repository. πŸ”«
Shell
44
star
12

minion-ci

minimalist, decentralized, flexible Continuous Integration Server for hackers.
Python
43
star
13

javascript-style-guide

Ein vernΓΌnftiger Ansatz fΓΌr einen JavaScript-Style-Guide
35
star
14

securityheaders

πŸ”’ CLI application to analyse Security Headers from a given URL using securityheaders.io
Python
19
star
15

musichaos

tool to tidy up your music chaos
Python
17
star
16

dropbox-cli

cli to manage your dropbox account
Python
10
star
17

ariseem

Minimalistic REST API for wake-on-lan
Python
10
star
18

leaked

Find leaked information in different kind of services
Python
9
star
19

retry-cmd

Retry commands on the command line without all the loops you always used!
Rust
9
star
20

confluo

➰ Minimalist scalable microservice framework for distributed systems using AMQP/RabbitMQ.
Python
9
star
21

.vim

vim configuration for myself.
Vim Script
6
star
22

ramlient

Access to a RAML API done right, in Python. (Feasibility)
RAML
6
star
23

ptipython-meta

Metapackage to install ptpython and ipython.
Python
6
star
24

idn-homograph-attack

Resources for "IDN Homograph Attack" for HSLU FKOM blog post testat
HTML
6
star
25

avra-atmega2560

This repository is a clone of avra version 1.3.0 with additional fixes to support the ATmega2560 chip
C
5
star
26

embedeval

NLP Embeddings Evaluation Tool
Python
4
star
27

dotfiles.attic

My personal dotfiles
Shell
4
star
28

broadcom-wl-monitormode

This is a source mirror for the broadcom wl driver version 6.30.223.141 with fixed monitor mode
C
4
star
29

hslu-pren-fs19

Implementation of the PREN FS2019 challenge at @ HSLU
Jupyter Notebook
4
star
30

saythanks-cli

Say Thanks via CLI. Uses @kennethreitz's great saythanks.io
Python
4
star
31

tag-expressions

Python implementation of Shunting-yard Algorithm to evaluate logical tag expressions
Python
3
star
32

dotfiles-2

Vim Script
3
star
33

hslu-pren-digit-cnn

Convolutional Neural Network to recognize digits used in the PREN class @ HSLU
Jupyter Notebook
3
star
34

python3.10-pattern-matching

Python 3.10 demos
Jupyter Notebook
3
star
35

edelweiss

edelweiss. A delightful color scheme for my personal terminal stack
Lua
3
star
36

pylemon

python daemon to monitor specific directories and react on changes
2
star
37

advent-of-code

My solutions for the Advent of Code puzzles
Python
2
star
38

java-dev

Java dev environment (Vagrant, Ansible, VirtualBox, Ubuntu 16.04) for HSLU
2
star
39

hslu-webtec-testat

HSLU WebTec Testat
JavaScript
2
star
40

pysingleton

module which provides a decorator to create thread-safe singleton classes
Python
2
star
41

lightning-talk-linux-kernel-module-examples

Examples for my "Linux Kernel Modules 101" Lightning Talk ⚑
C
2
star
42

timofurrer.github.io

Personal website and blog
HTML
2
star
43

asciitable

print formatted ascii table on console
C
1
star
44

clicore

small cli to use in python programs
Python
1
star
45

hslu-roblab-behavior

Python
1
star
46

sudoku.vim

vim plugin to solve sudoku files
Python
1
star
47

.tmux

my tmux configuration
1
star
48

coverage-importlib-test

Python
1
star
49

schoolcli

This is a very useful command line interface to manage your school marks
Python
1
star
50

hslu-xml-technologies

HSLU - XML Technologies - Projektarbeit
XSLT
1
star
51

hslu-ipcv

Exercises for the IPCV class @ HSLU
Jupyter Notebook
1
star
52

hslu-dbs

HSLU - DBS
TypeScript
1
star
53

hslu-aiso

HSLU AISO Class Exercises
Jupyter Notebook
1
star
54

dotfiles

Lua
1
star