A C implementation of the w1_therm temperature sensor reader for WildlifeSystems.
This program reads temperature data from 1-Wire temperature sensors connected to
the system via the Linux kernel w1_therm driver interface. It outputs readings in JSON
format compatible with the WildlifeSystems sensor-control framework.
Supported sensors:
- DS18S20 (family code 0x10)
- DS1822 (family code 0x22)
- DS18B20 (family code 0x28)
- DS1825 (family code 0x3B)
- DS28EA00 (family code 0x42)
makesudo make installThis installs the binary to /usr/bin/sensor-w1therm and the man page to
/usr/share/man/man1/sensor-w1therm.1.
sudo make uninstallsudo sensor-w1therm enableThis adds the w1-gpio overlay to /boot/firmware/config.txt. A reboot is required.
sensor-w1thermThe output is a JSON array in the WildlifeSystems format with one reading per
sensor found on the bus. The node_id and deployment_id fields are filled in
by sr.
[{"sensor":"ds18b20","device":"ds18b20","measures":"temperature","value":23.500,"unit":"Celsius","node_id":null,"sensor_id":"28-0123456789ab","sensor_name":null,"location":null,"deployment_id":null,"timestamp":1789225958,"config":null,"internal":false,"error":null}]# Read only internal sensors
sensor-w1therm internal
# Read only external sensors
sensor-w1therm external
# Read all sensors explicitly
sensor-w1therm allsensor-w1therm setupThis is run automatically on boot by the systemd service.
sensor-w1therm listsensor-w1therm identifyReturns exit code 60.
sensor-w1therm versionsensor-w1therm mockConfiguration is optional and is read from /etc/ws/sensors/w1therm.json. Every
sensor found on the bus is reported whether or not it has an entry; an entry
matches a sensor by its 1-Wire hardware identifier and overrides the fields
below.
[
{
"hw_id": "28-00000a1b2c3d",
"sensor_name": "Enclosure",
"internal": true,
"location": "{{node}}"
},
{
"hw_id": "28-00000e4f5g6h",
"sensor_id": "pond_temp",
"sensor_name": "Pond",
"location": { "latitude": 51.4967, "longitude": -0.1764, "accuracy": 3.0 }
}
]hw_id: the 1-Wire hardware identifier to match, as it appears under/sys/bus/w1/devices.sensor_id: a custom sensor identifier. If omitted, the hardware identifier is used.sensor_name: a human-readable name, reported in thesensor_namefield.internal: whether the sensor is inside the enclosure. The default is false.location: where the sensor is. Either"{{node}}"for the position of the node,"{{none}}"for a sensor that has no position, or an object withlatitudeandlongitudein decimal degrees and, optionally,altitudeandaccuracyin metres. If omitted, thelocationfield of the reading is null.
An example showing every option in use is installed as
/usr/share/doc/sensor-w1therm/examples/w1therm.json; it is valid JSON and can
be copied into place and edited.
-
Enable 1-Wire interface:
sudo sensor-w1therm enable -
Reboot the Raspberry Pi
The program uses the Linux kernel w1_therm driver sysfs interface:
/sys/bus/w1/devices/*/w1_slave- Traditional interface with CRC check/sys/bus/w1/devices/*/temperature- Direct temperature reading in millidegrees/sys/bus/w1/devices/w1_bus_master1/therm_bulk_read- Bulk conversion trigger
For more information, see the kernel documentation.
Every sensor found on the bus is reported. A sensor that cannot be read is
still emitted, with value null and the reason in error, as the other
WildlifeSystems drivers do; a sensor absent from the output was not found on
the bus at all. The reported error conditions are:
- Failed to read sensor: neither the
temperaturenor thew1_slavesysfs file could be read, typically a permissions problem or a probe that dropped off the bus mid-read - Startup value (85.0°C): Indicates the sensor has not completed a conversion yet
- Insufficient power (127.937°C): Indicates the kernel driver detected a power or bus error (not that the sensor is completely unpowered)
From within the sensor-w1therm directory:
debuild -us -uc -b- Linux kernel with
w1_thermdriver - POSIX threads library (pthread)
- GCC compiler
GPL-2.0-or-later
Comparison with older Bash version
Ed Baker ed@ebaker.me.uk
Part of the WildlifeSystems project. For more information, visit: