GPIO 1-Wire Temp sensors on Raspberry Pi with openHABian

Tags: #<Tag:0x00007f212b8d20b8> #<Tag:0x00007f212b8d1f78> #<Tag:0x00007f212b8d1de8>

(dijit) #1

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:

Exec Binding script for 1wire temps on OpenHABianPi GPIO
3x wire temperature sensors with raspberry pi and openhab
Raspberry PI 2 1-wire on gpio bus support
Raspberry Pi3 Openhab2 GPIO - not working - please help
OneWire GPIO Binding is missing in OH2.2 Paper UI
[Solved]Displaying bme280 information
(Wesley Schijven) #2

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?

(dijit) #3

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!

( ) #4

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:

(Alexandr) #5

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

(dijit) #6

@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.

(Josar) #7

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.