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)
- 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
Step 2: 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>
Step 3: 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
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
\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
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
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.
23 Comments
6 years ago
Hi,
I try to build a basic project using espeasy + RPI3 with openhabian 2,1 and mosquito. everything is going fine but relay not working, I can turn on and off relay using espeasy commands using web browser all screens shots are attache can some one tell me where i am wrong.
additionally i want to add timer on this relay with save the relay state during power failure.
7 years ago
Hi
I have successfully installed mosquito as you shared. But on following command it says
Error:Connection refused
Command: mosquitto_sub -h 127.0.0.1 -t myTopic.
Please guide
Reply 6 years ago
I was also getting connection refused no matter what I tried, even when testing Mosquitto right on the Raspberry Pi itself.
I finally fixed it by uninstalling Mosquito and re-installing it.
Look for an example of installing that uses a repository
The original Install I did on the Pi was very basic and did not work properly
Reply 7 years ago
You're likely leaving out the default username and password for mosquitto.
6 years ago
after installation of mosquitto, when i write
mosquitto_sub -h localhost -t test
i get in response
Error: connection refused
Can you guide me with this
Reply 6 years ago
try
sudo nano /etc/mosquitto/conf.d/default.conf
allow_anonymous true
https://www.digitalocean.com/community/tutorials/h...
Reply 6 years ago
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.
Reply 6 years ago
I'm not an expert on mosquitto, but here's how I would try
Open two terminal windows. In the first terminal run
In the second terminal run
If still no go, you may need to confirm the mosquitto.conf is in the correct directory and has been updated with your setup.
6 years ago
the link "http://www.esp8266.nu/index.php/Tutorial_Arduino_Firmware_Upload" doesn't work. It lead me "here: http://www.letscontrolit.com/index.php/Tutorial_Arduino_Firmware_Upload"
Reply 6 years ago
Here's the new url. http://www.letscontrolit.com/wiki/index.php/ESPEasy They've rebranded recently.
Reply 6 years ago
many thanks!
7 years ago
Hi,
Great post.
This is a request for guidance.
My ESP8266 module connects to “test.mosquitto.org” and subscribes to a topic say “b1”. What ever is published to “b1”, it can read it immediately.
Problem happens when my second ESP8266 module subscribes to “test.mosquitto.org” to “b1”. It continuously returns :
WiFi connected
IP address:
192.168.0.9
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Message arrived [b1] 0
Attempting MQTT connection…connected
Could you kindly suggest what might be going wrong. I look forward to your advice.
Reply 7 years ago
It took me a while to figure this issue out. This happens for two reasons, when two clients with the same "clientname" 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.
Reply 6 years ago
Thank you for taking your time to share the solution.
Reply 7 years ago
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. 192.168.1.20 (whatever the IP of the Raspberry Pi.
7 years ago
Fantastic read
Thanks again
7 years ago
Step3 the link to Download and installing the Arduino IDE is broken. Please fix.
Reply 7 years ago
Fixed. I had to break it out of a link. Instructables injects some other code that messes up the link. Thanks.
7 years ago
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
7 years ago
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