Markdown source

# OpenHAB and OneWire

<abstract>
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. 
</abstract>

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
dtoverlay=w1-gpio,gpiopin=4
</pre>

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](https://www.acmesystems.it/cm3home/CM3-HOME/CM3-Home_rel10_schematic.pdf), 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.

add:

<pre class="terminal">
# Enable the 1-wire bus 
dtoverlay=w1-gpio,gpiopin=16
</pre>

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/
	28-000006c423d3

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](https://github.com/openhab/openhab2-addons/tree/master/addons/binding/org.openhab.binding.exec)

### Scripts 
Create the file 

	/etc/openhab2/scripts/onewiretemp.sh

add the following lines:

	#!/bin/bash
	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 **./onewiretemp.sh sensorID**

### Things

Create the file 

	/etc/openhab2/things/onewire.things

add the following line:

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

### Items

Create the file 

	/etc/openhab2/items/onewire.items

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](http://www.tanzolab.it/openhab_widget)

# Links

* [GPIO 1-Wire Temp sensors on Raspberry Pi with openHABian](https://community.openhab.org/t/gpio-1-wire-temp-sensors-on-raspberry-pi-with-openhabian/20941)
* [Raspberry PIN setup](https://it.pinout.xyz/pinout/1_wire)
* [Il sensore di temperatura Dallas DS18B20](https://docs.google.com/presentation/d/10pQjiw_0tmQRUp2UNKwDqC0lZnmItRjQlWFt5CzhzWs/edit#slide=id.g100f2d24ee_0_66)

@include='bio_massimiliano_casini'
@include='bio_sergio_tanzilli'