node.jsraspberry-pikernel-modulesysfs1wire

nodejs fs.writeFile to sysfs (/sys) doesn't seem to work


I'm using the w1_therm kernel module to read temperature of a few DS18B20 temperature sensors on a Raspberry Pi 4. This works well by just reading the temperature files in /sys/bus/w1/devices/<ROM>.

Since I have quite a few sensors I want to make use of the bulk temperature conversion feature (see linked docs above). This can be triggered by writing trigger to /sys/bus/w1/devices/w1_bus_master1/therm_bulk_read. This write will take a bit over 750ms (that is how long the temperature conversion takes for full precision on DS18B20s). The temperature reads after that are just reading from the fs and don't trigger another conversion, meaning they are very fast.

Now to the problem: If I manually write using echo trigger > /sys/bus/w1/devices/w1_bus_master1/therm_bulk_read everything works, but if I try to do this from node js (v18.17.1) using await writeFile(...) (where writeFile is imported from fs/promises) the write takes only ~20ms (doesn't throw) and the following temperature reads are slow, meaning the bulk trigger didn't work. I tried opening the file in nodejs, writing and then calling sync() on the file descriptor to no avail. The node process has permission to write to that file.

I tried spawning a process form node that just echos to the file as a debugging step and that works perfectly fine.

Any ideas why writeFile doesn't work are appreciated.


Solution

  • 🤦‍♂️ As usual, you try to find a solution for hours and the second you ask the question you find it. I forgot to add a new line when writing to the file, i.e. writeFile(path, 'trigger\n'). echo does that by default.