In my last blog entry, I was able to use the ESP-01 board to control an LED by subscribing and publishing to the MQTT topic /LivingRoom/LED/1 and although this worked well, to create any kind of useful HA system, I would need to design my own HA client – probably using something like Python – and I really don’t want to get into all that! Therefore, I’ve been looking at using OpenHab to do all that side for me.

Setup

I’ve already got my RasPi and ESP board setup from the previous blog, so no need to go into details of that. The code on the ESP board is identical to before.

Installing OpenHab on the RasPi is simple enough and I used the tutorial here to get started, including setting up Samba so I can access my OpenHab folder from my Windows laptop where I do most of my work. For now, the only add-on I require – as far as I can see – is the mqtt binding, so I’ve moved all the other ones out of the folder to keep it small and to allow OpenHab to start quicker on the RasPi.

OpenHab Config

There are some useful pages on how to use MQTT with OpenHab. I found this one VERY useful.

I’ve changed things around a bit to make things simple.

First, I needed to add the following to the openhab.cfg file to allow it to connect to my MQTT broker:

mqtt:localbroker.url=tcp://192.168.0.7:1883
mqtt:localbroker.clientId=openHAB

Second, I need created the following sitemap (which as far as I understand it creates the GUI), which I called test.sitemap:

sitemap TestSiteMap label="Test Site-Map"
{
	Frame label="MQTT Test"{
		Switch item=MQTTLED label="LED"
	}
}

And finally, add the following items file, which I called testItems.items:

Switch MQTTLED {mqtt=">[localbroker:/LivingRoom/LED/1:command:ON:1],>[localbroker:/LivingRoom/LED/1:command:OFF:0]"}

The above creates a simple gui that only contains one switch. When this switch is pressed, OpenHab will send a 1 or 0 message to the MQTT topic /LivingRoom/LED/1 which the ESP board will pickup and switch the LED on or off. The gui created from the code is shown below.

Screenshot_2015-01-26-12-30-27

I’ve tested the above setup and my LED is being turned on and off as quick as I can press the button!

Next Step

So now that that’s working well (though I’ve not done a very deep test), I’m going to work on the next step which is to connect an AVR to the ESP board and get some useful serial communication going on in both directions. When this is done, I’ll be creating a small temperature sensing board that sends the temperature via MQTT which can be read on the OpenHab gui.

Advertisements