GPIO 1-Wire Temp sensors on Raspberry Pi with openHABian

This tutorial explains how to connect multiple Maxim DS18b20 1-wire temperature sensors to a GPIO pin on a Raspberry Pi, and pull those temperature readings into OpenHAB.

This is a re-write of my previous post on this issue, which includes problems I ran into, and how I fixed them: Exec Binding script for 1wire temps on OpenHABianPi GPIO

I have also posted a longer version on my blog, which is a bit more…“bloggy”…:

Step 1: Install and configure openHABianPi

For this tutorial, I used the openHABian image for Raspberry Pi:

Also, github page with download link:

Follow the instructions on the link above to download the .img file, flash to SD card, boot up Pi, wait approximately 45min. It’s really that simple.

Connect power and Ethernet to your RasPi, and you should now have a working openHAB with SSH access.

Step 2: Build 1-wire sensor network

Visit the manufacturer’s reference document here to help with properly building your network of (wired) 1-wire sensors:

Connect the 1-wire sensors to GPIO pin 4 on the RasPi.
Below is an example breadboard configuration of the connections on an original Raspberry Pi model B.
3.3V, GND, and GPIO pin 4 (as labeled) are the only connections on the RasPi side. Once I confirmed this was working I then used CAT-5 cable to run the temp sensors to different rooms of my house.

Note: the 4.7k pull-up resistor on the data pin of the DS18b20. Only one of these is required in total for the 1-wire network. Also note that the RasPi is used to power the 1-wire sensor network so keep that in mind and make sure your RasPi power supply is adequate. This setup is working successfully for me with a 2-amp power supply.

Step 3: Reading Temperature sensors through GPIO on RasPi

The 1-wire Binding for openHAB relies on ow-server, and is therefore limited to serial, USB, or network sockets/tcp for the 1-wire connection. There is no current configuration for GPIO sensors, so instead you will need to use Exec Binding and a script that polls the sensors.
There is a good starter script here:
Below is a modified script for those of us in the USA, which converts the output value to Fahrenheit:

wert=`cat /sys/bus/w1/devices/$1/w1_slave | tail -n1 | cut -d '=' -f2`   
wert2=`echo "scale=3; $wert/1000 * 9.0 / 5.0 + 32.0" | bc`
echo $wert2

Save this script to /etc/openhab2/scripts for use later.

Next, configure the RasPi to load the 1-wire devices at boot:

**Option 1:SSH into your openHABianPi, and nevigate to /boot. Then use vi or your favorite text editor to open the RasPi config.txt file:

pi@openHABianPi:~$ cd /boot
pi@openHABianPi:/boot$ sudo vi config.txt

**Or, option 2:Power down your RasPi and put the SD card in your PC to access the FAT formatted boot partition.

With either method, once you open config.txt, add the following line:


Note that the config.txt that comes with openHABian contains 3 sections for the various version of RasPi’s: 1, 2, and 3, so the correct section must be modified for the corresponding RasPi version.

So, for my Pi model B (aka Pi1), my config.txt looks like this:

initramfs initrd.img-4.4.0-1-rpi followkernel
# to disable DeviceTree, uncomment the next line 

Reboot, and then confirm the RasPi can see the temp sensors by checking here:

pi@openHABianPi:/boot$ cd /sys/bus/w1/devices
pi@openHABianPi:/sys/bus/w1/devices$ ls
28-xxxxxxxxxxxx  28-xxxxxxxxxxxx  28-xxxxxxxxxxxx  w1_bus_master1

If the w1 directory does not exist, the 1-wire driver was not loaded, and you need to check config.txt and make sure it is correct.

The 1-wire temp sensors each have a UID (28-xxxxxxxxxxxx) that is displayed in this folder. This UID must be specified when running the sample script.

Now that you know your RasPi can see the sensors, head to the scripts folder where you previously saved the 1-wire script.

Manually run the script and include the UID of one of your identified sensors. You should get a temperature value output:

pi@openHABianPi:/etc/openhab2/scripts$ sudo bash ./ 28-xxxxxxxxxxxx

If you receive an error, double check the UID, try a different sensor UID, and also check your script for syntax errors.

Step 4: Creating the openHAB items, things, sitemap, etc

The next step is to install Exec Binding, and created the appropriate openHAB files (optionally you may use the wizards to create things and items).

In openHAB’s Paper UI (http://192.168.blah.blah:8080/paperui/index.html), the Exec Binding can be installed by navigating to:

Add-ons>Bindings>Exec Binding

Next, create things and items files, and place them in their respective openHAB folders: /etc/openhab2/things and /etc/openhab2/items

example onewire.things file:

exec:command:onewiretemp1 [command="bash /etc/openhab2/scripts/ 28-xxxxxxxxxxxx"]
exec:command:onewiretemp2 [command="bash /etc/openhab2/scripts/ 28-xxxxxxxxxxxx"]
exec:command:onewiretemp3 [command="bash /etc/openhab2/scripts/ 28-xxxxxxxxxxxx"]

example onewire.items file:

String onewiretemp1Value "Temp 1 is [%s °F]" {channel="exec:command:onewiretemp1:output"}
String onewiretemp2Value "Temp 2 is [%s °F]" {channel="exec:command:onewiretemp2:output"}
String onewiretemp3Value "Temp 3 is [%s °F]" {channel="exec:command:onewiretemp3:output"}

It is important that you specify “output” for the channel that links the item to the thing. Other value options will not work with this ‘script method’ .

Sample sitemap entries:

Frame label="Environment" {
Text label=”Temperatures” icon=”temperature_hot” {
Text item=onewiretemp1Value
Text item=onewiretemp2Value
Text item=onewiretemp3Value

Step 5: Testing the UI display

Assign your custom sitemap to the Basic UI.
You can do this through Paper UI>Configuration>Services>BasicUI>Configure.

You should now have a fully functioning 1-wire temperature sensor network, displaying live temps:


thanks for the tutorial.
I’m almost getting there.

I get “[sudo] password for openhabian” when i try to run the scripts.
I’m new to openhab and tried the standard password “passwd”, no success
also tried password “openhabian” but then i get output “(standard_in) 1: illegal character: ^M”

Do you know what goes wrong?

That is the default…
You could also try pi:raspberry . Is is possibly a keyboard/language setting? Ive come across a few posts on the forum about different language keyboards running into this problem (QUERTY vs. QUERTZ?). If you have just done fresh install it wouldnt be too much to stsrt over with a new .img and try again. Good luck!

No need to go overboard :slight_smile:

  1. The default username and password are “openhabian”. See
  2. keyboard problems are only present if you are physically working on the RPi. I’d strongly suggest to work from a PC via SSH. This way you do also not encounter the keyboard problem.
  3. illegal character: ^M" is one of the classics of Linux problems when you are working with different operation systems. See here for what to do:

Why is the temperature value as String type?
How to convert String type to Number type?
It is not possible to operate whit it!

@Bagunda the temp value output of the script is a string. I am not aware of a way around this with the original script, but I’m sure it’s possible.

There are several examples on the forum of how to convert/cast/parse the String to a Number/Decimal, such as:

I have followed my own tutorial a few times (fresh installs) and receive a working setup each time. If you find a more efficient way of implementing this using Number items please reply with your code and I’d be happy to update the tutorial.

I think in openhab2 and the exec bindnig this is not possible anymore.
As the output channel of the exec binding will always be a string.

Maybe this helps.

hello, i know that this is a fairly old post now, but i’m having difficulty with this script. i can the the UI happening but cannot work out how to get it to read the temp sensors. any help on this would be most appreciated.


Great tutorial, thanks. Has anyone successfully connected an AM2301 censor through 1-wire? I’ve gone through the steps and I see the /sys/bus/w1 directory but it only contains w1_bus_master subdirectory and nothing with sensor ID’s. Anyone know where to look?

P.S. Adafruit DHT reads the temperature and humidity fine.

AM2301 sensors are not Dallas OneWire sensors, they use their own protocol. I am building up the selection of modules for MQTTany and plan to include one to provide access to DHT/AM23 sensors over MQTT as well as Dallas OneWire sensors.

I’m not an expert in RPI nor Linux nor Python.
I tried installing and running MQTTany, but so far failed :sleepy:

Please provide some real example config files for beginners.
Thank you

2019-11-26 19:38:15,048 [ERROR] [mqttany ] yaml.parser.ParserError: while parsing a block mapping
2019-11-26 19:38:15,049 [ERROR] [mqttany ] in “/etc/mqttany/mqttany.yml”, line 206, column 3
2019-11-26 19:38:15,050 [ERROR] [mqttany ] expected , but found ‘’
2019-11-26 19:38:15,051 [ERROR] [mqttany ] in “/etc/mqttany/mqttany.yml”, line 218, column 5

Looks like there are a couple of errors in your config file. Have a look at line 206 and 218, as shown in the log you posted.

If you still need help, please open an issue on github and post your entire config file using code fences.

Hi, yes I also understand that something is wrong in those lines… that’s why I would like examples of working config files because it’s not clear when to use ’ signs and when not… if you make it more dummy proof more people will use your code…

1 Like

I’m working on it, but currently the program is about 4 months old and I have 2 users that I know of, only one of which is using the OneWire module. I don’t have a lot of feedback to go on.

Please open an issue on github as this forum is for openhab support only. Post your config file, or at least the OneWire section and we can sort out your problem and discuss improvements to the documentation further.

Maybe a stupid remark but… is the breadboard diagram correct? The A4 pin seems to go to the wrong side of the bus.

1 Like

Yes, you are right, it should go to the ground

could you please provide an example file (mqttany.xml) for providing onewire DS18B20-signals to bus?
I think, your mqttany would be THE perfect tool for GPIO I/O - but your abstract documentation is unfortunately not “dummy proof”…
Please: just provide some (working) lines of code, how to output any ‘28-…’.
Thanks in advance!

Why not just use the existing binding for these sensors?

…because - for me - Tasmota is much more reliable and I am going to de-centralize some important smart actions (point–to–point from gpio via mqttany via broker to tasmota).
Nevertheless I want to read these values in openhab too.
So the best way for me is: not installing thousands of bindings for each “thing”, but installing (and understanding!) bindings only essential openhab-bindings.

Fair enough…
my system has 5 temp sensor Things from a single GPIO binding and has been very reliable