![]() |
rasProject_01 / weSweetHome
R. 116 2026-05-14
process control / RasPi software by weinert-automation
|
Common types and values for 1-wire sensors. More...
Data Structures | |
| struct | oneWireDevice_t |
| A structure for 1-wire devices - temperature sensors. More... | |
Macros | |
| #define | BAD_TEMP_READ |
| Bad temperature reading. | |
| #define | STD_DEVICES_PATH |
| The standard devices location. | |
| #define | STD_DEVICES_PATH_LEN |
| Standard 1-wire device path length. | |
| #define | TEMPbufferSize |
| size of return text buffer | |
| #define | TEMPvalidYpos |
| position of 'Y' (yes good) in return text | |
| #define | TEMPvalueEqpos |
| position of '=' followed by temperature (if good) | |
Functions | |
| int | getTemp (oneWireDevice_t *const tempSensor) |
| Get temperature. | |
| void | initTempSensor (oneWireDevice_t *const tempSensor, char const *const name, char const *const valueFile) |
| Initialise a 1-wire sensor structure. | |
Common types and values for 1-wire sensors.
Revision history
1-wire devices and especially 1-wire temperature sensors are an attractive way to enrich Raspberry's process I/O repertoire. Up to some dozen devices can share one digital I/O pin; the default is GPIO04 (Pin7).
In most Linuxes and in Pi OS 1-wire devices are accessed by Linux's "device as file" approach. The behaviour is being implemented in kernel modules (i.e. files of type .so). Those modules must be activated in OS's configuration files. Access to a 1-wire device's I/O values, configuration data etc. is done by a) opening, b) reading from respectively writing to and, finally, c) closing the corresponding (pseudo) file.
The sheer number of files for a small un-complicated device and the content semantics seam neither clearly designed nor documented *). One ordering principle is to have those pseudo files in one directory named according to 1-wire devices' unique readable serial number.
N.b.: As the I/O pin used for 1-wire (default GPIO04, Pin7) is governed by those kernel modules no other process control software must touch this port directly.
) On the other hand: The files are text files. All values etc. are text. Hence, the formats can be explored and the devices can be tested by just Linux commands, via ssh e.g..
This include file introduces some types and functions to handle 1-wire.
| #define STD_DEVICES_PATH |
The standard devices location.
This is the directory where each device's sub-directory is located.
Value: /sys/bus/w1/devices/
| #define STD_DEVICES_PATH_LEN |
Standard 1-wire device path length.
This is the length of "/sys/bus/w1/devices/" without the trailing '/' respectively the index of that trailing slash.
This path length +1 would be the index of the device directory name "28-..." in "/sys/bus/w1/devices/28-01e63c07010c/".
This path length +15 — i.e. + STD_DEVICES_PATH_LEN — would be the end of the concrete device's directory path respectively the index of its trailing respectively separating slash.
| #define BAD_TEMP_READ |
Bad temperature reading.
The integer value for bad temperature sensor reading is +987650 m°C respectively +987.650 °C far beyond the 1-wire temperature sensors range.
Rationale for choosing a (too) high value as bad value: The usual and safe "turn XYZ off" when temperature exceeds xydegC" requires no special treatment for sensor failure.
| int getTemp | ( | oneWireDevice_t *const | tempSensor | ) |
Get temperature.
This function tries a new measurement on the 1-wire temperature sensor provided. On success the new value is returned. On failure the last good value is returned, but at most 7 times after 7 or more good readings before. On total failure -99900 (-99.9 °C, 0 K, BAD_TEMP_READ ) is returned.
Additionally on temperature changes, the integer reading (.value) and the floating point string (valueGrdC[]) will be set in the structure.
| tempSensor | pointer to the sensor's structure |
| void initTempSensor | ( | oneWireDevice_t *const | tempSensor, |
| char const *const | name, | ||
| char const *const | valueFile ) |
Initialise a 1-wire sensor structure.
This function does basic initial settings for a 1-wire temperature sensor. tempSensor.name and tempSensor.valueFile are set by the respective parameters.
The temperature values are set to bad value; see BAD_TEMP_READ .
| tempSensor | pointer to the sensor's structure (never NULL!) |
| name | the sensor's short name or its directory name; NULL / empty: no change |
| valueFile | the canonical absolute path to its value file; NULL / empty: no change |