Introduction: Part 2 OpenHAB, MQTT, and ESP Easy - Build Your Own DIY Node

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
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

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(]" <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

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

\OpenHAB\configurations\transform\ (you must use this name)

Refer to here for more information


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.