Configuring OpenHAB with MQTT Broker
First make sure the OpenHAB application is running and that you can access the setup wizard page:
.http:/youripaddress:8080/ui/index.html#/setup/wizard
Just to recap, in the openhab2.0 directory, run startup.bat from the command line and enter youripaddress:8080 in your browser. The following web page should appear:

Click on Extensions:

Scroll down until you see “MQTT Binding” and click on the “Install” button. (My MQTT Bind is already installed in the picture)

Next edit the mqtt.cfg file located in the C:\openhab2.0\conf\services directory and add the IP address of the host where the MQTT BROKER will be running. Remove the hash at the beginning to uncomment the line:
mosquitto.url=tcp://yourbrokeripaddress:1883
You also need to uncomment the line for the clientID. This will help you track the message exchange with the mqtt broker and the openhab mqtt subscriber.

Now we need to get OpenHAB configured to run mqtt subscriber. In order to do this we need to install an OpenHAB tool called Eclipse SmartHome Designer. The 64-bit Windows version can be downloaded from here:
Other versions are available here:
After installing Eclipse SmartHome Designer (ESD) run SmartHome-Designer.exe. The tool should look like the following:

Click on Sitemaps. Double click on demo.sitemap. Add the line indicated by the red arrow:
Group item=HCSR icon=“temperature”

This entry will give you an item in the group section at the top of the main menu for the OpenHAB2.0 demo
Click on Items. Double click on demo.items. Add the following line as indicated by the red arrow:
Group HCSR “Motion Sensor” < temperature > ***NOTE: No spaces before and after temperature

Find the section label Lights and add the line highlighted by the red arrow:

Here is a line for you to copy/paste into your demo.items files and edit with your ESP settings:
Number HCSR04 “HC-SR04 Distance [%.1f cm]” < slider > (HCSR) {mqtt=“<[mosquitto:/MarksESP8266/MarksHCSR/Distance:state:default]”}
***NOTE: Remove the spaces before and after the word “slider”
Next add an entry to the demo.sitemap file so that the reading from the HCSR04 shows up on the webpage dynamically.

In the first “Frame” section at the following line to create a group item:
Group item=HCSR icon=“temperature”
Im using the temperature icon until I can determine how to replace it with something else.
Now you are ready to start testing the communications between OpenHAB, MQTT BROKER, SUB client and the PUB client.
(For some reason I could not get the MQTT BROKER on my Windows OS to communicate with any of the my SUB/PUB clients so I moved the broker to my Mac Mini. It works fine with the MQTT BROKER running on the Mac!)
Start the MQTT BROKER on the host with the IP address you entered in the mqtt.cfg for openhab. Its always a good idea to do a ping test to make sure you can reach the host…otherwise you are wasting time.
Next restart the OpenHAB application by entering “logout” at the openhab2.0 command prompt. Then enter startup.bat. You should see something similar to the screenshot below

There should be several entries of communication from the OpenHAB client subscriber with the clientID you entered in the mutt.cfg file.
Before we setup the HC-SR04 lets make sure that the MQTT software architecture is working. We can do this by setting up a sub and pub client on a separate host. In the MQTT setup above we configured everything on the same host. Hopefully these steps are helping build an understand of MQTT.
Open a Windows command prompt and enter the following command…see picture for command syntax:

This sets up a subscriber for our test.
Now open another Windows command prompt and enter the following command…

This will publish the number 324 to any client subscribing to /MarksESP8266/HCSR04/Distance.
Take a look at the log entries on the MQTT BROKER host (-v displays the activity - verbose mode). you can see the sub client notifying the broker of the topic its subscribing to and the pub client publishing 3 bytes to the same topic

The topic id and the number 324 should now show in the SUBSCRIBE command window.

Now that we have confirmed that the MQTT architecture is working lets setup the HCSR04 sensor in OpenHAB20 to send an email when something passes in front of it.