Table of Contents

Class Ds18b20Thermometer

Namespace
Pamoja.Sensors
Assembly
Pamoja.Sensors.dll

A DS18B20 the Linux kernel serves as a w1_slave file.

public sealed class Ds18b20Thermometer : IDisposable
Inheritance
Ds18b20Thermometer
Implements
Inherited Members

Remarks

On a Raspberry Pi the w1-gpio overlay (dtoverlay=w1-gpio in config.txt) puts a 1-Wire bus on GPIO 4, and the kernel's w1_therm driver lists every DS18B20 it finds as a directory named by its family code and serial, such as 28-000005e2fdc3. Reading the directory's w1_slave file makes the kernel run a conversion and print the scratchpad, which Read() decodes.

That is the way to reach a DS18B20 from a Linux process, where the bit timing a 1-Wire driver needs cannot be held. The file is only text, so a test can write one anywhere and read it with At(string).

Properties

Path

The path of the file the thermometer reads.

public string Path { get; }

Property Value

string

Serial

The serial the kernel named the thermometer's directory after: the twelve hex digits after 28-, which tell one probe from another and stay with the part for life. Null when the file does not sit in a DS18B20's directory, as one named by At(string) may not.

public string? Serial { get; }

Property Value

string

Methods

At(string)

Names a thermometer by the path of its w1_slave file.

public static Ds18b20Thermometer At(string path)

Parameters

path string

The file to read.

Returns

Ds18b20Thermometer

The thermometer.

Exceptions

PamojaException

The path could not be passed to the native side.

Discover(string?)

Lists every DS18B20 the kernel has found, one per 28- directory.

public static IReadOnlyList<Ds18b20Thermometer> Discover(string? devices = null)

Parameters

devices string

The directory the kernel lists its 1-Wire devices in, or null for /sys/bus/w1/devices.

Returns

IReadOnlyList<Ds18b20Thermometer>

The thermometers, sorted by directory name.

Exceptions

PamojaException

The directory cannot be listed, which usually means the 1-Wire overlay is off.

Dispose()

Releases the thermometer.

public void Dispose()

ForSerial(string)

Names a thermometer by the serial in its directory name.

public static Ds18b20Thermometer ForSerial(string serial)

Parameters

serial string

The twelve hex digits after 28-.

Returns

Ds18b20Thermometer

The thermometer, reading /sys/bus/w1/devices/28-<serial>/w1_slave.

Exceptions

PamojaException

The serial could not be passed to the native side.

Read()

Reads the file, which makes the kernel run a conversion, and decodes it.

public Ds18b20Reading Read()

Returns

Ds18b20Reading

The CRC-checked reading.

Exceptions

PamojaException

The file cannot be read, because the overlay is off, the probe is gone, or the process may not read it; or the kernel or this decoder rejects the CRC.