I have an 'sht3x' temp/humidity sensor connected to my Raspberry Pi via I2C. I've written a bash
script to get the sensor readings from sysfs
. My problem is this: the name of the folder containing the sensor files I need to read changes, and I don't know how I should get the correct folder name. IOW: Which folder in sysfs
contains the files with the sensor measurements and configuration parameters?
I wired and configured (via RPi's device tree & overlays in config.txt
) the sht3x
to take up residence on channel i2c-0
at bus address 0x44
. I confirmed that using i2cdetect -y 0
.
Using grep
, I found the sysfs
folder I needed at the following location:
/sys/bus/i2c/devices/0-0044/hwmon/hwmon3
I did some research to sort out how that particular folder was instantiated/selected to contain the results - but my research was not entirely successful. Here's what I worked out:
/sys/bus/i2c/devices
is the "root" for all I2C devices that the system knows of
0-0044
corresponds to the i2c "channel" I selected (i2c-0
), and the device address (44
)
hwmon
is the "root" sysfs
folder for all I2C sensor devices
But that still leaves me lacking one folder: hwmon3
. This folder has changed names a few times; typically after a reboot
. This final folder name usually alternates between hwmon3
& hwmon2
, but I imagine that may change if I add other sensors.
I could add a grep
routine into my bash
script to find the correct folder, but I can't help but think "there must be a better way to do this". In the kernel document "Rules on how to access information in sysfs", the author(s) suggest using udev
to navigate sysfs
- but AFAICT there is nothing in udev
for I2C devices. So - that's my question: How do I get the kernel to tell me what sysfs
folder my sensor data is in?
I'm posting this answer b/c I have made progress on my question, but an issue remains:
In my question, I opined "there must be a better way [than grep
to locate my sensor device file in sysfs
]". I wondered if udev
was a solution, but I have never seen a udev
example that was applied to a device on an i2c
bus. All the examples one finds concern themselves only with disk drives (or the occasional network device)!
Anyway, I read a bit about udev
, found nothing stating that it was incapable of dealing with i2c
-based sensor devices, and decided to have a go at it. I was successful, but the solution involved a detour.
Here's my "solution" - such as it is:
.rule
file at /etc/udev/rules.d/80-local.rules
with one line:ACTION=="add", SUBSYSTEM=="hwmon", ATTR{name}=="sht3x", KERNELS=="0-0044", SUBSYSTEMS=="i2c", RUN+="/bin/sh -c 'ln -s /sys$devpath /dev/hwmon_sht3x'"
I have tested this rule several times now, and it accomplishes my objective: I get a symlink to the correct folder in sysfs
. I have forced "the system" to move/rename the sensor folder from /sys/bus/i2c/devices/0-0044/hwmon/hwmon3
to /sys/bus/i2c/devices/0-0044/hwmon/hwmon2
to /sys/bus/i2c/devices/0-0044/hwmon/hwmon4
. In each case, the symlink produced in /dev
by the above rule was correct. This gives me a persistent location for the sensor data in /dev/hwmon_sht3x
The symlink I get in /dev
is as follows:
hwmon_sht3x -> /sys/devices/platform/soc/3f205000.i2c/i2c-0/0-0044/hwmon/hwmon<X>
where <X> is the correct number; "2, 3 or 4" on my system.
If you're familiar with udev
rules, you may be wondering why I didn't use the SYMLINK+=
assignment key - instead of the RUN+=
assignment key with a sh
command to create a symlink?
The answer is: the SYMLINK+=
assignment key did not create a symlink of any kind in /dev
! I wonder if this has something to do with the fact it's in the hwmon
SUBSYSTEM???
I've read through all the udev
documentation I could handle, but found no answer why this is so. Perhaps it's a bug? I don't know... but if you know the answer, I'd love to hear from you.