Introduction: OpenHab on Pi Controlling Particle Photon Relay

Picture of OpenHab on Pi Controlling Particle Photon Relay

In this article we will be running OpenHab which is an open source home automation software on a Raspberry Pi. I used a Raspberry Pi model B running Raspbian but this article should work as long as you are running Linux on a Rapberry Pi.

Also running on the pi is an application called Mosquitto which acts as an MQTT broker/server. More on this later.

We will control a relay connected to a Particle Photon module via the MQTT protocol. This will be the communication interface between OpenHab and the Photon module. Both OpenHab and the Photon modules will connect to the Mosquitto Broker application running on the Pi. By publishing and subscribing to MQTT events we have a communication method between the Particle Photon and Open Hab. Lets do it!

For this project you will need the following:

Raspberry Pi

1 Channel Particle Photon Relay

Step 1: Setup the Pi

Picture of Setup the Pi

First off you need a Pi running Raspbian or something similar. I will not cover that in this guide as it is outside the scope but for more information check here.

Once your Pi is running Raspbian and is connected to you LAN we need to setup OpenHab and Mosquitto on it. Once again instructions for both are very well documented already.

Install OpenHab on Raspberry Pi

Install Mosquitto on Raspberry Pi

So at this point you have OpenHab and Mosquitto running on your Pi. Fantastic!

I should note that the article to follow assumes you have installed OpenHab exactly as instructed in the Install OpenHab article from Instructables. If not some of the following directory paths may not be exact for your install.

For this project I just SSH to the Pi and do all my configuration there. This mainly because I have not taken the time to hook up a monitor, mouse, and keyboard to the Pi. I basically run it as a headless server.

MQTT OpenHab configuration

If you unzipped all of the addons to the /opt/openhab/addons directory then in that directory you will have org.openhab.binding.mqtt-x.x.x.jar This is the only add on jar that will be used in this tutorial so just make sure that is there in your addons directory.

Next we need to setup OpenHab's MQTT client. This requires that we make some slight alterations to the file at /opt/openhab/configurations/openhab.cfg I generally just pico or nano the file to make my changes. So in the terminal just enter:

sudo pico /opt/openhab/configurations/openhab.cfg

In this file scroll down to Transport configurations or do a Where is by holding the ctrl key + W and enter Transport configurations

Here you will find a line that says

#mqtt:mybroker.url=tcp://localhost:1883

Change that line to

mqtt:mybroker.url=tcp://localhost:1883

Note that the # character at the start of the line is removed as this is a comment delimiter in this file.

Save the file and exit.

That is all of the configuration we have to do to hook OpenHab up to the Mosquitto broker, it can now subscribe to MQTT events and publish MQTT events. Neat huh!?

OpenHab Sitemap and Items list configuration

Next we need to setup the interface elements on the UI to allow the user to turn our Particle Photon relay on and off. Spoiler alert I am new to OpenHab. This is how I set it up by I am by no means an expert so if you have another way you would rather do this please do so. Also let me know in the comments.

If you installed OpenHab for the first time by following the Instructables guide above then you would have already downloaded the Demo Setup. If you did not and you already know what you are doing you can skim over this.

Lets start with the sitemap file. Lets go to the configurations directory so we can see what all we have to work with. In the terminal enter:

cd /opt/openhab/configurations

Then enter ls to list all files and directories. Here you will see the sitemaps directory. Enter cd sitemaps then ls to list all files and directories there. Here you should see demo.sitemap For simplicity sake we are just going to delete this file and re-create it. If you are doing everything through SSH and pico trust me this is faster. Of course if you are doing other things in open hab already you may not want to do this if you are using that sitemap. Anywho in the terminal enter

sudo rm demo.sitemap

This will delete the file, but don't worry we are about to resurrect it. Now enter:

sudo touch demo.sitemap

This will create a new empty file called demo.sitemap which we can modify as we see fit. In the command line enter:

sudo pico demo.sitemap

After you are in the pico editor paste in the following:

sitemap demo label="My Relay Board" {

Frame label="Relays"{

Switch item=relaySwitch label="Relay Switch"

Text item=status

}

}

After that save the file and exit.

Ok... sitemap is done. Last thing to do to OpenHab is to edit our Items file. In the terminal enter:

cd /opt/openhab/configurations/items

Enter ls to list all files and directories. Here you should see a demo.items file. It is about to go the way of the demo.sitemap file. In the terminal enter:

sudo rm demo.items

Then enter:

sudo touch demo.items

Now we have a blank file to setup our items. In the terminal enter:

sudo pico demo.items

In the pico editor paste in the following:

Switch relaySwitch "Relay Board" {mqtt=">[mybroker:setrelaystatus:command:ON:on],>[mybroker:setrelaystatus:command:OFF:off]"}

Number status "Status is [%d]" {mqtt="<[mybroker:relaystatus:state:default]"}

Start It UP!!

In the terminal enter:

cd /opt/openhab

Now enter:

sudo ./start.sh

This will start the OpenHab server which will take a couple minutes to get up and running initially.

This concludes our OpenHap/Mosquitto setup on the Pi. Pat yourself on the back, that was the hard part. Lets head over Particle's way.

Step 2: Setup the Photon

Picture of Setup the Photon

First make sure your Photon is installed in the relay board, is associated with your Particle account, is configured for your network(same network as the Pi), and is connected to the Particle cloud(breathing Cyan LED).

At this point we are ready to flash some code in that Photon so head over to https://build.particle.io/build

Make sure the module installed in the relay board is selected in the devices section. Now go to Libraries and search for NCD1Relay under Community Libraries. Once found click to select the Library and you should see all of the files in the window to the right. Make sure you select the MQTT-Relay.ino tab is selected, then click the Use This Example button on the left. Once forked to your local code library finally click the Flash button in the upper left corner. You should see the LED on the Photon module flash magenta momentarily, then it will re associate with the Particle Cloud indicated by a breathing Cyan LED.

Step 3: Have Fun!

At this point you are done working and its time to play.

Open your Web browser and enter

<ip of your Pi>:8080/openhab.app?sitemap=demo

Here you will be pleasantly greeted with a switch that you can use to turn the relay on and off which is extremely fun!

From here I recommend installing the OpenHab app on your smart phone. You can also setup the Pi for remote access so you can control from anywhere in the world and setup your Pi to run OpenHab on boot. There are articles for this all over the web so have fun!!!

Comments

JavierV58 (author)2016-05-31

Hi, thanks for sharing your work! I am a little stacked with the particle build code i cant find on NCD library the mqtt example. Also i am using the particle 4 relay shield but i cant find the ncd ino file to maybe adapt it. Any hints?

About This Instructable

3,360views

24favorites

License:

More by ControlEverything:Amazon Echo Activated Garage DoorKnocki KnockoffOpenHab on Pi Controlling Particle Photon Relay
Add instructable to: