Markdown source

# OpenHAB and OneWire

This article describes how to OpenHAB can read a Dallas DS18B20 temperature sensor directly attached to a Raspberry 3 GPIO, and the equivalent attached to the dedicated socket of the CM3-Home. 

The DS18B20 sensor has this pinout:

<img src="./ds18b20_pin.jpg" class="img-responsive center-block">

It needs a 3.3V power supply and a 4.7 or 10K&ohm; resistor on the data line:

<img src="./1wire_raspi.png" class="img-responsive center-block">

On the Raspberry PI3 connect the data line to PIN 7 (BCM 4) and add the lines below to **/boot/config.txt** file: 

<pre class="terminal">
# Enable 1Wire on GPIO

On the CM3-Home connect the sensor as shown below:

<img src="./1wire.jpg" class="img-responsive center-block">

As depicted on the [schematic diagram](, the 1-wire data line is interfaced through a dedicated buffer in order to allow a more robust bus, therefore it doesn't require a pullup.


<pre class="terminal">
# Enable the 1-wire bus 

Reboot the board.

The sensors wired are automatically detected by the Linux Kernel and a new directory is created on the folder __/sys/bus/w1/devices__ for each of them.

	$ ls /sys/bus/w1/devices/

In this case a temperature sensor with id=28-000006c423d3 has been detected. To read the temperature value type:

	$ watch cat /sys/bus/w1/devices/28-000006c423d3/w1_slave 
	a4 01 4b 46 7f ff 0c 10 da : crc=da YES
	a4 01 4b 46 7f ff 0c 10 da t=26250

## OpenHab
To read the sensor value we are going to use the binding that allows to execute periodically a script:

Install the [Exec Binding](

### Scripts 
Create the file 


add the following lines:

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

test the script executing the command **./ sensorID**

### Things

Create the file 


add the following line:

	exec:command:onewiretemp1 [command="bash /etc/openhab2/scripts/ IDSensore", interval=60, timeout=5, autorun=false]

### Items

Create the file 


add the following line:

	String onewiretemp1Value "La temperatura del sensore 1 è [%s °C]" <temperature> {channel="exec:command:onewiretemp1:output"}

Add the sensor to sitemap to show the value on UI:

	Text item=onewiretemp1Value

Or use the item value in a widget to show it in an habpanel, as in [this example](

# Links

* [GPIO 1-Wire Temp sensors on Raspberry Pi with openHABian](
* [Raspberry PIN setup](
* [Il sensore di temperatura Dallas DS18B20](