Part two on DIY home automation.

Step 1: Purchase Some Parts

Dollar Store (or similar) = cheap components and housings.I picked up a handful of these usb adapters and a couple of housings. (shown above)

Other items needed to build this

Step 2: Install MQTT on Your Openhab Pi

This is a continuation from my post here


Use an SSH connection for this setup. There are other ways to setup MQTT with OpenHAB, but this one is compatible with ESP Node MCU.

sudo apt-get update
sudo apt-get install build-essential python quilt devscripts python-setuptools python3 
sudo apt-get install libssl-dev libwrap0-dev libc-ares-dev 
sudo wget http://mosquitto.org/files/source/mosquitto-1.4.1.tar.gz
tar zxvf mosquitto-1.4.1.tar.gz 
cd mosquitto-1.4.1 
make all 
sudo make install 
sudo ldconfig

Now you can try mosquitto:

Open two terminal windows. In the first terminal run

mosquitto_sub -h -t myTopic

In the second terminal run

mosquitto_pub -h -t myTopic -m "My first message"

Enable MQTT Binding with OpenHAb

sudo nano /opt/openhab/configurations/openhab.cfg

Look for MQTT transport and enable "URL to the MQTT broker" and "User id to authenticate with the broker" by removing the hash mark at the beginning of the line.

################################# MQTT Transport ######################################<br>#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with a id you choose.
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:mymosquitto.url=tcp://localhost:1883<br># Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
# mqtt:mymosquitto.clientId=openHAB<br># Optional. User id to authenticate with the broker.
mqtt:mymosquitto.retain=true<br># Optional. Password to authenticate with the broker.
<password></password></broker># Optional. Set the quality of service level for sending messages to this broker. 
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2 
# (Deliver exactly once). Defaults to 0.
#mqtt:<broker>.qos=<qos></qos></broker><br># Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#mqtt:<broker>.retain=<retain></retain></broker><br># Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#mqtt:<broker>.async=<async></async></broker><br># Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained 
#mqtt:<broker>.lwt=<last will="" definition=""></last></broker>

Step 3: Program Your ESP

Tutorial Arduino Firmware Upload

Download and installing the Arduino IDE here


  • Select the "generic esp8266 module" board (for this tutorial) before uploading.


  • The ESP Easy has a userfriendly web interface for all configuration settings. After initial upload the ESP does not have a valid SSID and WPA key and it will start in Access Point mode with the WPA key "configesp"
  • Connect with your laptop or tablet to ip and enter the config tab to change SSID and WPA key. Now reboot the device and it should join your own local network.

Name: (frontdoor)
Admin Password: (no need to change)
SSID: (your network SSID)
WPA Key: (Your network password)
WPA AP Mode Key: (no need to change)
Unit nr: (1)
Protocol: (OpenHAB MQTT)
Controller IP: (OpenHAB/MQTT controller IP address)
Controller Port: (1883)
Sensor Delay: (15)
Sleep Mode: (Do not change) ****(Use the IP address assigned by your router here)

Select Task 1 (use the photo above and text below as a reference for the configuration)

Task Settings
Device: Switch input
Name: pir IDX / Var: 100
1st GPIO: GPIO-0
Pull UP: checked
Inversed: not checked
Switch Type:
Switch Switch Button Type: Normal Switch
Optional Settings
Value Name 1: contact

Select Task 2 (use the photo above and text below as a reference for the configuration)

Task Settings
Device: Temperature & Humidity - DHT
Name: dht
IDX / Var: 100 1st
DHT Type: DHT 11
Optional Settings Formula temperature: %value%*9/5+32 (this will convert celsius to fahrenheit)
Formula humidity:
Value Name 1: temperature
Value Name 2: humidity

Step 4: Build Your ESP Node

Disclaimer - Serious injuries and even death can occur if you don’t take the proper precautions.

  • Disassemble the USB adapter to power the ESP
  • Solder the power cord to the appropriate wires of the USB power adapter
  • Remove the female USB receiver from the USB power adapter board
  • Use a Voltmeter to identify the the 5 VDC positive and negative leads on the USB power adapter board
  • Connect the 5 VDC wires to the 5 VDC -> 3.3 VDC Voltage Regulator (in)
  • Use the 5 VDC to power the PIR module
  • Use the 3.3 VDC to power the DHT 11 and ESP
  • ESP diagram (above)
    • GND - 3.3 negative
    • VCC - 3.3 positive
    • CH_PD - 3.3 positive
    • GPIO 0 - PIR out
    • GPIO 2 - DHT out

Step 5: Setup OpenHAB Gui


your.items (file)

Refer to here for more information https://github.com/openhab/openhab/wiki/Explanation-of-items

Group chart_outdoor_fr_temp
Group chart_outdoor_fr_hum Group all Group outdoor Group esp Group temp Group chart_all_temp Group chart_all_temp Group contacts Number period_outdoor_fr_temp "Front Porch Temperature Period" Number period_outdoor_fr_hum "Front Porch Humidity Period" Number esp_outdoor_fr_temp "Front Porch Temperature[%.0f °F]" <temp_icon> (all,outdoor,esp,temp,chart_outdoor_fr_temp,chart_all_temp) {mqtt="<[mymosquitto:/frontdoor/dht/temperature:state:default]"} Number esp_outdoor_fr_hum "Front Porch Humidity[%.0f %%]" <hum_icon> (all,outdoor,esp,hum,chart_outdoor_fr_hum,chart_all_hum) {mqtt="<[mymosquitto:/frontdoor/dht/humidity:state:default]"} Number esp_frontdoor_pir "Front Door Motion [MAP(esp-pir.map):%s]" <motion> (all,outdoor,esp,contacts) {mqtt="<[mymosquitto:/frontdoor/pir/contact:state:default]"}

Not all the Goups above are in use for this tutorial. I'll include them in future tutorials.

Save and name the icons (32x32 px) above into \OpenHAB\webapps\images

  • temp_icon.png

  • hum_icon.png

  • motion.png (the still gray image)

  • motion-0.png (the moving blue image)

  • motion-2.png (the moving blue image)


your.sitemap (file)

Refer to here for more information https://github.com/openhab/openhab/wiki/Explanation-of-Sitemaps

Frame label="ESP Module"{
Group item=esp label="Front Porch" }

\OpenHAB\configurations\transform\esp-pir.map (you must use this name)

Refer to here for more information https://github.com/openhab/openhab/wiki/Transformations


That should do it... Next will introduce Charts

Step 6: Credits and Links

Eric Tsai - It was Eric's tutorial that inspired me to setup my own openHAB

John Wills - I hired John through eLance to help me get started with openHAB



I pulled many of these steps from other posts. I referenced the ones I remember and my apologies to those I missed.

<p>Hi </p><p>I have successfully installed mosquito as you shared. But on following command it says </p><p>Error:Connection refused</p><p>Command: mosquitto_sub -h -t myTopic.</p><p>Please guide</p>
<p>I was also getting connection refused no matter what I tried, even when testing Mosquitto right on the Raspberry Pi itself.</p><p>I finally fixed it by uninstalling Mosquito and re-installing it.</p><p>Look for an example of installing that uses a repository</p><p>The original Install I did on the Pi was very basic and did not work properly</p>
<p>You're likely leaving out the default username and password for mosquitto.</p>
<p>after installation of mosquitto, when i write</p><p>mosquitto_sub -h localhost -t test</p><p>i get in response<br><br>Error: connection refused<br><br>Can you guide me with this</p>
<p>try</p><p>sudo nano /etc/mosquitto/conf.d/default.conf </p><p>allow_anonymous true<br></p><p><a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-16-04" rel="nofollow">https://www.digitalocean.com/community/tutorials/h...</a></p>
<p>With the 'Error: connection refused' I presume you are using some Linux install. The error is probably because you don't have the user 'mosquitto' added to your users. Either add mosquitto to your users or change the user in the /etc/mosquitto/mosquitto.conf file. </p>
<p>I'm not an expert on mosquitto, but here's how I would try</p><p> Open two terminal windows. In the first terminal run</p><pre>mosquitto_sub -h -t myTest</pre><p> In the second terminal run</p><pre>mosquitto_pub -h -t MyTest -m &quot;Test&quot;</pre><p>If still no go, you may need to confirm the mosquitto.conf is in the correct directory and has been updated with your setup.</p><br>
<p>the link &quot;http://www.esp8266.nu/index.php/Tutorial_Arduino_Firmware_Upload&quot; doesn't work. It lead me &quot;here: http://www.letscontrolit.com/index.php/Tutorial_Arduino_Firmware_Upload&quot;</p>
Here's the new url. http://www.letscontrolit.com/wiki/index.php/ESPEasy They've rebranded recently.
<p>many thanks!</p>
Hi,<br>Great post.<br>This is a request for guidance.<br>My ESP8266 module connects to &ldquo;test.mosquitto.org&rdquo; and subscribes to a topic say &ldquo;b1&rdquo;. What ever is published to &ldquo;b1&rdquo;, it can read it immediately.<br>Problem happens when my second ESP8266 module subscribes to &ldquo;test.mosquitto.org&rdquo; to &ldquo;b1&rdquo;. It continuously returns :<br><br>WiFi connected<br>IP address:<br><br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected<br>Message arrived [b1] 0<br>Attempting MQTT connection&hellip;connected<br><br>Could you kindly suggest what might be going wrong. I look forward to your advice.
<p>It took me a while to figure this issue out. This happens for two reasons, when two clients with the same &quot;clientname&quot; subscribe to a mosquitto server or you are using a power supply that cant handle the sudden burst of ~800mA when a message from MQTT arrives. Each client connecting should have unique clientname and get a reliable 1 A or 2 A USB power supply.</p>
Thank you for taking your time to share the solution.
I'm not sure as you are connecting to a mosquitto server on the WAN (at a remote location). My tutorial is for a LAN connection where the server would be at a local IP i.e. (whatever the IP of the Raspberry Pi. <br><br>
<p>Fantastic read</p><p>Thanks again</p>
Step3 the link to Download and installing the Arduino IDE is broken. Please fix.
<p>Fixed. I had to break it out of a link. Instructables injects some other code that messes up the link. Thanks. </p>
<p>hi , thanks for sharing your experience however I have question that appreciated for your explanation , is it possible to made network of ESP enabled sensor module with ESP easy firmware?couple of station ESP module report back or got command from AP ESP module </p>
<p>hi , thanks for sharing your experience however I have question that appreciated for your explanation , is it possible to made network of ESP enabled sensor module with ESP easy firmware?couple of station ESP module report back or got command from AP ESP module </p>
great instructable, I wonder you can give me some hints on connecting tonmqqt broker when arduino is outside the LAN. I connect but no success with publish our subscribe.
<p>The PIR sensor did not work for my openhab / EspEasy versions. </p><p>EspEasy generates a number formatted to 1.00 or 0.00 as payload. This is not recognized in esp-pir.map. You need to add to esp-pir.map:</p><p> 0.00=Still</p><p>1.00=Motion</p><p>And there you go!</p><p>With node RED I subscribed to the topic and quickly learned about the actual payload that's why I had to add these extra lines. </p><p>Testing with node RED I learned that node RED changes the payload from 1.00 to 1 and from 0.00 to 0. </p>
Thanks! I am getting ready to deploy a bunch of these into my house for automation purposes and this makes the OpenHAB software make more sense when it comes to configuration.

About This Instructable




More by instrbles37568:Quick and Easy ESP8266 Plug Via OpenHAB Intro - Creating a PCB board for the Moteino (design, etching, and soldering) Installing openHAB2 on Raspberry Pi  
Add instructable to: