8000 GitHub - troxel/w1-therm-api: Python w1_therm api · GitHub
[go: up one dir, main page]

Skip to content

About

Python w1_therm api

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

w1-therm-api

A Python script to support the Linux w1_therm kernel driver. It discovers multiple 1-Wire buses, 1wire temperature sensors, triggers simultaneous conversions on supported buses, and returns temperatures in Fahrenheit by default.

The package reads the driver's sysfs interface directly and has no runtime dependencies.

Requirements

  • Linux with the kernel w1_therm driver loaded.
  • Read/write access to /sys/bus/w1/devices (or an alternate sysfs root).
  • Python 3.7 or later.

W1Bus recognizes the common w1_therm temperature families: 28-, 10-, 22-, and 3b-.

Install

python -m pip install w1-therm-api

Usage

from w1_therm_api import W1Bus

bus = W1Bus()

# Send Convert T to each master that exposes therm_bulk_read, then wait for
# the driver's cached results to become available.
if bus.convert():
	for sensor_id, temperature_f in bus.read_temperatures():
		if temperature_f is None:
			print(f"{sensor_id}: unavailable")
		else:
			print(f"{sensor_id}: {temperature_f:.2f} F")

To receive Celsius values, select it when creating the bus:

bus = W1Bus(unit="C")

Sensors are discovered when W1Bus is created. To ask each master to search for new devices and refresh that list:

bus.rescan()
print(bus.sensors)

For systems without therm_bulk_read, individual reads still work:

for sensor_id, temperature_f in bus.read_temperatures():
	print(sensor_id, temperature_f)

How It Uses w1_therm

When available, convert() writes trigger to each master's therm_bulk_read entry. It then polls until the driver reports that the conversion result is ready before reading every sensor's temperature entry. This lets devices on a bus convert in parallel, including parasite-powered devices when the driver and adapter support a strong pullup.

read_temperature() first reads the cached temperature value. If that file is missing, empty, invalid, or reports the DS18x20 power-on value of 85 C, it falls back to w1_slave and validates its CRC status. Failed reads and power-on values are returned as (sensor_id, None); valid values are returned as (sensor_id, temperature_f).

The underlying sysfs entries and their behavior are documented in the Linux kernel's w1_therm driver ABI. This package does not configure resolution, EEPROM, alarm, power, or driver feature settings.

API

W1Bus(
	devices_dir="/sys/bus/w1/devices",
	known_families=("28-", "10-", "22-", "3b-"),
	unit="F",
)
  • find_masters() returns discovered w1_bus_master* directories.
  • find_sensors() returns recognized sensor directories.
  • rescan(timeout=2.0) requests a bus search and refreshes sensors.
  • start_conversion() triggers bulk conversion and returns whether any master was triggered.
  • wait_for_conversion(timeout=2.0) waits for all triggered bulk conversions.
  • convert(timeout=2.0) performs both steps.
  • read_temperature(sensor_path) returns (sensor_id, temperature_f | None).
  • read_temperatures() reads every currently discovered sensor.
  • unit accepts "F" (default) or "C"; unit names are case-insensitive.

License

MIT.

About

Python w1_therm api

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0