Install and setup OpenHAB 2.0 with Mosquitto and ESPEasy+HC-SR04

I think I got it… :confused:

I’ve found this: https://sivatechworld.wordpress.com/2015/06/11/step-by-step-installing-and-configuring-mosquitto-with-windows-7/

I wasn’t aware I have to reinstall mosquitto after adding the required dll.s.

Now it’s done and obviously mosquitto.exe leads me where I wanted to be…

Nope, I can’t write anything there…

email me at markandcandyingle@gmail.com if you need my help

Thanks, now it’s fine. Had some issues with the ipv4 listen socket, got some unknown errors, but a complete reinstall solved the problem.

Please keep up the good work, I guess there are other noobs out there like me who need such lead at the beginning at least.

1 Like

Configuring ESPEasy with a HC-SR04

In order to configure the HC-SR04 you need a working ESP8266dev board loaded with the ESPEasy version 78 firmware. After the initial configuration of the firmware you should see the device listed in the available wifi networks

My device is call ESP_123…I think a new device that has not been configured will be ESP or ESPEasy. The password for connecting to the ESP is “configesp”

After connecting to the ESP at the default address of 192.168.4.1 you should see the following web page with welcome message and the name of your device at the top:

Your ESP should be configured to connect to your wifi network. Click on Config and enter the SSID and Password for your wifi network. Leave the WPA AP Mode Key password as it is.

Click on the Submit button on the bottom. Also set the Sensor Delay to something less than 10 so that you do not have to wait for a reading on the log.

You can look on your wireless access point for the IP address assigned to the ESP or you can connect to an Arduino and click on the serial monitor to see the IP address assigned. I use my Arduino.

Now join back to your wifi network and enter the new IP address in your browser.

Click on Devices at the top menu. Click in the first Edit button.

Click on the drop down list to display a list of available device for ESPEasy. Select the HC-SR04 device.

For Name, give the device a unique name of your choosing. My Device name is “MarksHCSR”

Enter a unique number for the IDX/Var. This number should unique to any other device per the MQTT Broker in use.

The 1st GPIO should be GPIO-13 for hardware pin D7 on the ESP8266Dev board. 2nd GPIO should be GPIO-14 for hardware pin D5.

For the “Mode” parameter select Value if you want to receive a distance measurement fof an object in front of the HC-SR04. Select State if you just want to know an object is in front of the HC-SR04. Setting Mode to Value will cause the actual distance to the object to be displayed. Setting the Mode to State will cause a 1 or 0 to be displayed.

The Value Name 1 is just a custom label. This can be anything you want. I labeled mine “Distance”.

I will stop here and take a minute to cover the MQTT topic that will be used. There are three pieces of data entered in the configuration setup that will be used to create the unique topic for each device configured in the firmware. This will be the topic used when the HC-SR04 data is published to the MQTT Broker. The following values will be used based on your setup:

The ESP8266 device name - MarksESP8266
The “Device” name - This is the sensor input/output device you are setting up for control or monitoring. My sensor device name is MarksHCSR
The “Value Name 1:” - This is the unique label you assign to the data coming from your device (sensor). My label is Distance

So for my setup the publish topic identifier is /MarksESP8266/MarksHCSR/Distance

1 Like

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:

http://www.eclipse.org/smarthome/documentation/community/downloads.html

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.

6 Likes

Hi. Thanks for the great article. You have your SR04 connected to the 3.3V pin on the ESP826-12E but the spec’s I read say the SR04 needs 5V? Can you confirm it worked on 3.3V? Also, do you need to set the trigger GPIO pin to high to get it to work? I’m getting no measurement data with trigger connected to D5 and echo connected to D7.

Yes the HCSR04 requires 5v. I have a voltage divider between the HCSR04 Echo pin and ESP GPIO14. Send me an email at markandcandyingle@gmail.com and I send you a picture of my breadboard setup. The trigger pin on the HCSR04 only requires 3.3v. I dont recall what the code is doing for setting the pin state. If I get a chance I will have look and let you know…hope this helps!

Also I reviewed my instructions above and realized I had the GPIO and board pins reversed. It has been corrected.

Hi Mark,
thanks for your thorough guide.
I have the same setup as you except I am using OH 1.8 .Just a question about the ESP Easy as I couldnt find the answer anywhere else… When you setup the sensor does the ESP Easy GUI show the sensor value in the device window? or the value only appears in OH?
I setup a Dallas DS18B20 last night following the instructions http://www.esp8266.nu/index.php/TemperatureSensor but the value on the ESP Easy GUI wasn’t changing (it was either nan or 0.0), hence I did not proceed to configuring OH for the sensor.
Thanks

Sam - I apologize for the very late reply. I have been busy with other projects and just checked in OpenHAB. ESP Easy does show the value in the device window but I think you need to refresh the page. I am sure you have probable moved on to your next project but let me know if you have any further questions.

Thank you Mark, this was a breakthrough for me. Really appreciate your contribution. Fantastic. Ton of applications here… presence, security… don’t you love the buzz after a break through… delusions of grandeur…
NOW to try and group these type of sensors together… PIR and hc sr04 to build in some sort of escalation… or hierarchy… i.e. Yes you are home (your cell phone has hit the wifi) and PIR/HC sr04 has detected movement… Astro binding says its dark so turn on the lights BUT not too many because its after 11 and we don’t want to wake people… wait the PIR/HCSR04 array has sensed another… who is that behind you…_sigh… I can dream…BIG

Alternative: arduino ide mashup sketch to help others…an alternative to pushing MQTT from sensors http://forum.arduino.cc/index.php?topic=408657.0

1 Like

Knowledge is power! Go improve the world with it!!! Thank you!!

When I do this, I don’t get such a config file (I assume on raspberry Pi/jessie it should be in the folder/etc/openhab2/services)

am I correct I can then create it myself, or does this mean I have another problem?

I think you have another problem. The mqtt.cfg file is part of the OpenHab install. Let me know if you need some help

also in Openhab2 on raspberry?

I relaunched :

wget -qO - ‘Service End for Bintray, JCenter, GoCenter, and ChartCenter | JFrog’ | sudo apt-key add -

echo “deb http://dl.bintray.com/openhab/apt-repo2 testing main” | sudo tee /etc/apt/sources.list.d/openhab.list

sudo apt-get install openhab2-online

and still get the same result.

I am sorry but I am not familiar with the raspberry install of openhab.

I figure it out how it does work in Openhab2, the current stable release.

I created manually an mqtt.cfg file
I updated the addons.cfg and added mqtt to the binding part.

It’s my guess that selecting mqtt on openhab2 on a raspberry pi only installs the needed mqtt binaries yet no configuration files. (and thus also does not allow to update the mqtt binding setting in paperUI

Great! That makes sense…glad you got it worked out.

1 Like

Hi all, hate to post this here but am struggling at finding this on the web, believe it or not. I have even posted to other forums and no luck.

I am not a gpio guru but have worked with oh for awhile and love it. I have a wemos D1 R2 that is connected to my wifi just fine. I am running the espeasy and working nicely. I have mqtt setup in oh and will connect it all as i go along here. So to my question, i have four relays that i need to connect to the wemos. i can find the schematic but its really greek to me. i just need to know what pins to connect and where to connect the relays. i find tons of youtube videos but they dont show the connection details, just how the code works etc. i guess thats just the simple part that everyone should know but this is my first foray into the electronic side.

Can someone point me in the right direction or just help with some simple dummy information like, dude, just connect pin x to one relay, pin y to another and your good to go.

thanks!

and by the way, this thread is great!

Hello Clint! I can try to help. It’s been awhile so I may struggle a bit. Can you share a pics of your setup to get started?