Part 2 OpenHAB, MQTT, and ESP Easy - Build Your Own DIY Node
by instrbles37568 in Circuits > Arduino
90949 Views, 173 Favorites, 0 Comments
Part 2 OpenHAB, MQTT, and ESP Easy - Build Your Own DIY Node
Part two on DIY home automation.
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)
- Housing (from $1 store)This is an LED wall plug night light with some paint and the insides pulled out.
- 110 VAC to 5 VDC adapter (from $1 store)
- 5 VDC to 3.3 VDC adapter
- spare 110v plug cord
- DHT11 Temperature and Relative Humidity Sensor
- HC-SR501 Pyroelectric Infrared Sensor
- ESP8266 Serial WIFI Wireless
Other items needed to build this
- FT232RL FTDI232 USB to TTL Serial Adapter Module
- Solder Iron
Install MQTT on Your Openhab Pi
This is a continuation from my post here
https://www.instructables.com/id/Part-1-openHAB-and...
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 127.0.0.1 -t myTopic
In the second terminal run
mosquitto_pub -h 127.0.0.1 -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. #</broker> # 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. #mqtt:<broker>.pwd= <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>
Program Your ESP
Tutorial Arduino Firmware Upload
Download and installing the Arduino IDE here
http://www.esp8266.nu/index.php/Tutorial_Arduino_Firmware_Upload
- 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 192.168.4.1 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)
http://192.168.2.159/devices ****(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
GPIO: GPIO-2
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
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
Setup OpenHAB Gui
\OpenHAB\configurations\items
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)
\OpenHAB\configurations\sitemaps
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
0=Still
1=Motion
That should do it... Next will introduce Charts
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
www.esp8266.nu/forum/index.php
https://community.openhab.org/
I pulled many of these steps from other posts. I referenced the ones I remember and my apologies to those I missed.