22.03, Raspberry 4B, reading DS18B20 temp sensors

I have two DS18B20 temperature sensors attached to my Raspberry Pi 4B via 1-Wire-system.
I can successfully read the sensor data, when booted via Raspberry PI OS:
cat /sys/bus/w1/devices/${PROBEADDRESS}/w1_slave
and query a list of the attached sensors addresses via modprobe w1-therm

Now the challange would be, to do this on OpenWRT 22.03:
I've found that I need a few more packages:

But when I tried to follow this, I soon :astonished: and :confused: and then :sob:.
Wow, this is some serious gourmet complicated issue in OpenWRT.... Installing opkg packages would have been fine, but having to compile stuff and all? Isn't there a simpler option?

Update: I did not succeed with a solution, but after some more research, I think I have now at least a better understanding of the problem context:

The raspberry GPIO (and likely also the GPIO pins of other boards) offers different external IO protocols:

  • SPi
  • i2c
  • 1wire

The temp sensor DS18B20 I am talking about needs the 1wire protocol, which seems not supported/maintained any more in OpenWRT, the OpenWRT wiki lists SPI and i2c only: https://openwrt.org/toh/raspberry_pi_foundation/raspberry_pi#using_i2c_and_spi

Seems other people had the problem identified earlier:

Good news. :slight_smile:
This weekend I had unexpectedly many lucky punches: I did not achieve anything helpful in real life, but I tried again with 1-wire and found, how to successfully access 1-wire temp sensor values via OpenWRT 22.03.5 on a Raspberry Zero! (I guess the same steps should also work identical for OpenWRT 23.05)

In other words: I found by coincidence that 1wire and Raspberry GPI thermal sensors now work in OpenWrt (not sure if it was ever broken and fixed or just confusing doc)

Preamble

I also noticed that the OpenWRT Raspberry Wiki seems updated since last time I read it during the initial post. The formerly weird section about device tree todos + 1wire + OpenWRT 22.03 seems gone.

I am not sure, of what is still valid in the current OpenWRT wiki pages about Raspberry, there are also some text notes mentioning Raspberry EEPROM updates that happen automatically, when you install and run an official Raspberry OS update on the Raspberry. As these steps aren't difficult, maybe just install and run Raspberry OS on SD card once so, to be on the safe side, to have that mysterios EEPROM brought to a more recent state once, before you continue with the following steps, as I do not know, if those updates might be needed (at least they wont hurt).


Prep work

  1. Write the appropriate OpenWRT image for the Raspberry device onto an SD-card.

  2. enable network (that prework may not be required. Depending on, if you can already connect to the Raspberry via LAN). In case, you need to add USB-LAN-adapter-based network to a Raspberry, see my other lucky punch: Improve OpenWRT NIC init behavior for Raspberry Pi (all variants) - #3 by Pico

  3. Once the OpenWRT image for Raspberry is applied to the SD-card, that SD-card will have a FAT-based boot partition (just like the Raspberry OS offical images). This Raspberry-specific boot partition is easily accessible, if you stick in then SD-card into the card reader of a Windows computer and open that one partition that appears in Windows Explorer. It is then required, to update the file "config.txt" in that partition, to enable the 1wire protocol. To do so, add the following single line at the end of that text config file:
    dtoverlay=w1-gpio

  4. Boot the Raspberry with that SD card

  5. Install packages in OpenWRT (if your Raspberry does not have WAN access, to install packages, you need to add those packages earlier, by creating a custom image via https://firmware-selector.openwrt.org/ and then reapply the image to the SD card. Anyway, I had installed the following packages for 1wire access:
    opkg update
    opkg install kmod-w1
    opkg install kmod-w1-slave-therm
    opkg install kmod-w1-master-gpio

  6. I am not sure, if I had to reboot here (maybe just do it), then run the following once to make the 1wire temp sensors visible in the file system:
    modprobe w1-gpio
    modprobe w1-therm

(That's it with the prep work)


Read temp sensors

Now you can start reading the sensor temperature value:
check the folder, where the sensors appear as devides and make sure that the sensors get listed:
ls /sys/bus/w1/devices
Example output, if any sensors have been found in the system:
28-01204fde9648 28-012050210e3e 28-0120502733ee w1_bus_master1
(In my case, these were the IDs of 3 connected sensors)

Now to print the current temperatures of these 3 1wire temp sensors of type DS18B20, run:
for A in $(ls /sys/bus/w1/devices/*/w1_slave); do echo "$A:"; cat $A; echo ""; done

Example output:

/sys/bus/w1/devices/28-01204fde9648/w1_slave:
92 01 4b 46 7f ff 0c 10 b5 : crc=b5 YES
92 01 4b 46 7f ff 0c 10 b5 t=25125

/sys/bus/w1/devices/28-012050210e3e/w1_slave:
8f 01 4b 46 7f ff 0c 10 9d : crc=9d YES
8f 01 4b 46 7f ff 0c 10 9d t=24937

/sys/bus/w1/devices/28-0120502733ee/w1_slave:
90 01 4b 46 7f ff 0c 10 33 : crc=33 YES
90 01 4b 46 7f ff 0c 10 33 t=25000

(the t=... part is the current temperature value of each sensor)


Final words

Could be there are a few redundant steps in my description (I may need to repeat this at sometime with a blank image), but the mentioned steps for sure resulted in success with OpenWRT 22.03.5 on a Raspberry Zero with 3x DS18B20 1wire sensors.


GPIO PINs I had used

I had my three 1wire sensors all connected to the following pins of the 2x20 pin section of the Raspberry Zero (with the usual resistor between +3.3V and "GPIO4", but please refer to any official doc, how to properly connect a DS18B20 sensor to GPIO, I will not be made responsible, if you mess up anything):

  • pin 7 (for 1 wire data, called "GPIO4", which I think is the default Rasp Pi PINs meant for 1wire protocol)
  • pin 1, which is +3.3V
  • pin 9. which Is GND