Home Automation with Rasp pi and OpenHAB

Niraj Basnet Topics Covered

● What is Home Automation? ● Current Smart Home landscape ● Intro to OpenHAB ● Installing and Configuring openHAB ● Core concepts in OpenHAB ● GPIO binding in OpenHAB ● Exercise: Controlling a LED and relay ● MQTT protocol in IOT ● Using MQTT to interface DHT11 sensor ● Autonomous rules in OpenHAB ● Demo: Some automation rules ● Interaction through HAB panel

What is Home Automation?

● Automating appliances used in home in order to create a more hospitable environment. ● Why? Resource and time management are becoming increasingly important. ● Moving quickly from a convenience tool to an indispensable tool in near future. ● Control lighting, climate, entertainment systems, and appliances. Current Smart Home Landscape

● Many competing vendors, few worldwide accepted industry standards resulting in the smart home space being heavily fragmented. ● Too many home automation control apps for end users ● Integrating different technologies to get the best of features and pricing is really a pain and middlemen are reaping all the benefits. Intro to

● Open Home Automation Bus is an open source home automation platform which runs as the center of your smart home. ● Integration and interaction between different home automation technologies and devices. ● Vendor and technology agnostic ● Developed in java ● Runs on any device capable of running JVM ● Ships iOS and Android apps for device control, as well as design tools so you can create your own UI for your home system. ● Best-known home automation tools among open source enthusiasts, with a large user community. Installing OpenHAB on

● Install Raspbian lite or full version in your raspberry pi by flashing raspbian image from official raspberry pi site. ● sudo apt-get update sudo apt-get install git sudo git clone https://github.com/openhab/openhabian.git /opt/openhabian sudo ln -s /opt/openhabian/openhabian-setup.sh /usr/local/bin/openhabian-config sudo openhabian-config ● Select ‘Openhab stable’ from the menu. It will install automatically in about 15 minutes. ● Install ‘Mosquito MQTT broker’ from ‘Optional Components’ section in the menu and set password to ‘iot’ during configuration. Command line interface for OpenHAB

Usage: openhab-cli command [options]

Possible commands: start [--debug] -- Starts openHAB in the terminal. stop -- Stops any running instance of openHAB. status -- Checks to see if openHAB is running. console -- Opens the openHAB console. backup [filename] -- Stores the current configuration of openHAB. restore filename -- Restores the openHAB configuration from a backup. showlogs -- Displays the log messages of openHAB. info -- Displays distribution information. Core concepts in OpenHAB Core concepts in OpenHAB(contd) GPIO binding in OpenHAB

● GPIO Binding allows us to interact with the onboard gpio pins of the raspberry pi. ● Interact implies being able to turn the GPIO pins on and off as well as read the status of the input on a GPIO pin. 1. To install the binding go to your openhab web user interface and click on paper ui. 2. Then go to add-ons. After that click on bindings and in the search type in GPIO and install the one that says GPIO binding. 3. If you want to configure some other settings for the GPIO binding you can go to the configuration tab and then the bindings sub tab. 4. After that you can click the configure button in the GPIO binding. Circuit connection details ● Led - GPIO3- resistor and gnd-cathode(-) ● Relay- GPIO 21-IN1/IN2 pin ○ VCC pin- 5V on pi ○ GND - GND on pi ● Button- GPIO 16 Exercise: Controlling a LED and relay

1. Create the items in the items file. 2. Type in ‘sudo nano /etc/openhab2/items/home.items and press enter.’ 3. Now, add following contents to the items file.

Switch LED "LED" { gpio="pin:3" } Switch channel1 "Fan" { gpio="pin:21 activelow:yes initialValue:high" } Contact DoorSensor "Door Sensor [%s]" { gpio="pin:16 debounce:10 activelow:yes" }

4. put the items into the sitemap file, so type in sudo nano /etc/openhab2/sitemaps/home.sitemap and press enter. 5. sitemap home label= “Home App”

{Switch item=LED Switch item=channel1 Text item=DoorSensor} MQTT protocol in IOT

● Message Queuing Telemetry Transport ● A featherweight ISO standard pub-sub messaging protocol specifically designed for IOT ● Data packet consists of fixed header and payload only. ● Used easily on low power devices like sensors,phones,μ-Controllers(especially battery powered) ● Guaranteed in-order delivery per publisher Features of MQTT ● Decouples publishers and subscribers in: ○ Space: Publishers and Subscribers don’t know each other’s IP addresses. ○ Time : Don’t have to be actively connected at the same time ○ Synchronization: Sending and receiving happen at their own speeds. ● MQTT QoS (Quality of Service) ○ QOS 0: At most once , “Fire and Forget” ○ QOS 1: At least once ○ QOS 2: Exactly once; 2 phase commit ■ QOS1 and QOS 2 messages are queued for clients which may be offline but have not timed out. ● Security ○ Pass username and password with MQTT packet. ○ Encryption across the network can be handled with SSL, independently of MQTT. (But extra security measures always add significant network overhead ) Setting up MQTT

● Install MQTT binding v1.1 by enabling legacy bindings in configuration tab of Paper UI . ● Install python mqtt client by following command. ○ pip install paho-mqtt ● Go to folder /etc/openhab2/services and edit mqtt.cfg file. ● Use the same password that you set during Mosquitto installation for configuring mqtt as well. Setting up MQTT using legacy bindings Installing binding using Add-ons Using MQTT to interface DHT11 import paho.mqtt.client as mqtt mqttc = mqtt.Client() mqttc.username_pw_set(username="openhabian", password= "iot") mqttc.connect("192.168.1.126", 1883) while True : humidity,temperature=Adafruit_DHT.read_retr y(Adafruit_DHT.DHT11, DHTpin) mqttc.publish("home/temperature", '{:3.2f}'.format(temperature), qos=1, retain=True ) mqttc.loop() Linking MQTT binding to Mosquitto broker

● Edit mqtt.cfg file inside /etc/openhab2/services. mosquitto.url=tcp://:1883 mosquitto.user=openhabian mosquitto.pwd=iot mosquitto.qos=1 mosquitto.retain=True mosquitto.async=False home.items file

home.sitemap file Demo: Some automation rules ● Time activated LED and temperature activated relay Autonomous rules in OpenHAB ● "Rules" are used for automating processes ● Each rule can be triggered, which invokes a script that performs any kinds of tasks, e.g. turn on lights by modifying your items, do mathematical calculations, start timers etcetera. ● Rules are placed in the folder $OPENHAB_CONF/rules ● A rule file is a text file comprising Imports, Variable Declarations and Rules. ● Before a rule starts working, it has to be triggered. ● Item(-Event)-based triggers: They react on events on the openHAB event bus, i.e. commands and status

updates for items

● Member of triggers: React on events on the event bus for items acting as member of the supplied Group.

● Time-based triggers: They react at special times, e.g. at midnight, every hour, etc.

● System-based triggers: They react on certain system statuses.

● Thing-based triggers: They react on thing status, i.e. change from ONLINE to OFFLINE. Interaction through HAB panel

● Allows the creation of user-friendly dashboards, particularly suited for (e.g. wall-mounted) tablets ● Dashboards can be designed interactively in the embedded designer, rather than using configuration files. ● Despite being similar, HABPanel dashboards and sitemaps are separate concepts, and can be designed independently as they aren't related to each other; however, they rely and act on items which must therefore be defined first ● Several types of built-in standard widgets, configured separately, and the administrator can also develop (or import) custom widgets. Custom HABpanel Some cool HAB panels Thanks! Any questions?

Register your participation: http://groups.engr.oregonstate.edu/iota/participation Participation type: Club Event Club name: Alliance(IOTA)