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:
https://github.com/openhab/openhabian/releases
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: https://www.maximintegrated.com/en/app-notes/index.mvp/id/148
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: http://www.itbasic.de/openhab-onewire-sensoren-einbinden/
Below is a modified script for those of us in the USA, which converts the output value to Fahrenheit:
#!/bin/bash
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:
dtoverlay=w1-gpio,gpiopin=4
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:
[pi1]
kernel=vmlinuz-4.4.0-1-rpi
initramfs initrd.img-4.4.0-1-rpi followkernel
# to disable DeviceTree, uncomment the next line
device_tree=
dtoverlay=w1-gpio,gpiopin=4
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 ./onewiretemp.sh 28-xxxxxxxxxxxx
70.586
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/onewiretemp.sh 28-xxxxxxxxxxxx"]
exec:command:onewiretemp2 [command="bash /etc/openhab2/scripts/onewiretemp.sh 28-xxxxxxxxxxxx"]
exec:command:onewiretemp3 [command="bash /etc/openhab2/scripts/onewiretemp.sh 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: