ESPEasy can now read out very many different sensors and / or control actuators (see also ESPEasy - Sensors & Actuators). The "rules" can then be used to react locally on the ESP and act accordingly.
But the collected sensor data can also be sent. For example, this data can be collected and processed in the SmartHome system. Sending via the MQTT protocol is ideal for this purpose.
How to register your ESPEasy instance to a MQTT server is described in the following article.
Safety instructions
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
https://www.nerdiy.de/sicherheitshinweise/
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Requirements
Helpful articles:
Additional tips on ESPEasy can be found in the following articles.
- ESPEasy - Execute commands and actions via HTTP, MQTT, UDP and directly
- ESPEasy - Compile firmware, flash and configure WLAN access data
- Category - ESPEasy
Required tool:
- none -
Required material:
Which MQTT server to use?
Meanwhile, there are several MQTT servers that can be used. For example, there are also public MQTT servers accessible via the Internet.
Personally, I don't want to use them because I think that has two disadvantages:
- My data leaves my network and ends up on a foreign server. Even if the data protection standards are now very high, it can still happen through attacks / errors that my data could be published.
- The public MQTT server can only be reached if the Internet connection to this server is also established. If this were to fail, the forwarding of data to this server would also fail. This can also result in any related automations no longer working.
Both disadvantages can be avoided very well by using your own MQTT server. This can be installed on a RaspberryPi for example. You can find more information about this in the article RaspberryPi - Install MQTT Server on the RaspberryPi.
Configure login to the MQTT server
To be able to add the MQTT server to your ESPEasy instance, the ESPEasy firmware should already be installed on the ESP8266 and accessible. Information about this can be found in the article ESPEasy - Compile firmware, flash and configure WLAN access data.
First open the configuration web page of your ESP8266 programmed with ESPEasy by entering its IP address into your web browser. Information on how to find out the IP address can also be found in the article Display/find out the IP address of the devices in the network summarized.
As soon as you have opened the configuration website, you must switch to the "Controllers" tab. To do this, click on the "Controllers" button.
Now click on the "Add" button in one of the empty rows.
This will redirect you to a page where a drop-down menu will be displayed.
Open the drop-down menu by clicking on it and select the entry "Home Assistant (openHAB) MQTT".
You will be redirected to the configuration page of this controller (=MQTT server). First you can choose if you want to enter the address of the MQTT server as hostname or IP address.
In the example shown here I use a hostname. But you can also use an IP address. If you do, you should configure your router so that the IP address is fixed.
You can apply the other settings as shown.
It is important that the port is set to the configured port of your MQTT server (1883 by default).
You must also enter the login details for your MQTT server if it requires authentication. To do this, enter the user name and password in the corresponding fields and check the "Use Extended Credentials" box.
You can configure the remaining settings as shown. For the sake of completeness, here is a short description.
- "Controller Client ID": Here you can specify the name/ID of your controller to the MQTT server.
- "Unique Client ID on Reconnect": If this box is checked, a random ID is appended when a new connection is attempted. This prevents (very likely) that two clients with the same ID are connected.
- "Publish Retain Flag": If this checkmark is activated, published values including the "Retain Flag" are sent.
- "Controller Subscribe": This topic is subscribed to by the controller. Commands can then be sent to ESPEasy via this topic, for example. You can also find information on this in the article ESPEasy - Execute commands and actions via HTTP, MQTT, UDP and directly.
- "Controller Publish": The topic under which sensor values are published (if activated) is defined here.
- "Controller LWT Topic": The MQTT server can report on the connection status of the client via the LWT (="Last Will/Testament").
- "LWT Connect / Disconnect Message": This option can be used to define the message that is sent via the LWT topic in the event of a connection or disconnection.
- "Send LWT to broker": Activates/deactivates the LWT functionality
- "Will Retain": Sets the "Retain Flag" for the messages sent via the LWT Topic.
- "Clean Session": Activates / deactivates a "clean session" login
- "Enabled": Activates / deactivates this controller
As soon as you have configured all the settings as you wish, you can apply the settings by clicking on "Submit".
After that your controller should be listed in the controller overview.
If you have configured more than one controller, you should remember the number of parts in which the MQTT server is configured. In this example the MQTT server is the first Controller.
Configure sending sensor data
After the MQTT server is configured, you can now also send the data of a connected sensor via MQTT to your previously configured MQTT server.
To do this, first switch to the "Devices" tab by clicking on the "Devices" button.
If you have not already done so, you must first configure a sensor.
In this example the DHT11 is used.
To do this, click on the "Add" button in the first empty line in the "Devices" tab.
Then configure the option for "GPIO Data" and "Sensor model" according to your sensor.
But now to the MQTT part:
In order for ESPEasy to send the sensor data to the MQTT server, you must activate the checkbox in the "Send to Controller" section. Since you are sending the data to the first controller, the tick is listed next to the "1".
You can use the "Interval" option to specify how often the sensor is read out. The data is then sent at the same interval. In the example shown, the sensor value is read out every second and sent via MQTT.
Save these settings again by clicking on "Submit".
Back in the "Devices" tab, you can now also see the controller you have entered.
An external MQTT client can then also be used to check whether the data is being sent.
In the picture you can see a section of the "MQTT Explorer" view. This tool is free of charge and is very helpful when dealing with MQTT messages.
Create MQTT Device to subscribe to a MQTT Topic
You can also subscribe to certain other topics using another virtual device. The values received via this can then be further processed with rules, for example.
To do this, switch back to the "Devices" tab by clicking on the "Devices" button.
Then click on the "Add" button in the first empty line.
You will then be redirected to a drop-down menu. Select the "Generic - MQTT Import" entry here.
You can subscribe to up to four topics in the menu that then appears. Enter these under "MQTT Topic 1 - 4".
You can then define the names of the variables in the "Values" section. These variables will later contain the values received via MQTT. This is important, for example, if you want to react to these values using "Rules".
Once you have configured everything as desired, check again that you have checked the "Enabled" box and confirm the settings by clicking on "Submit".
Now you can react to the subscribed topics with the help of rules. To do this, you must first switch to the "Rules" tab.
If it is not visible, you have to activate it first.
Click on "Tools" to switch to the "Tools" tab.
Click on "Advanced" there...
...and check the "Rules" option.
Then confirm this setting by clicking on the "Submit" button.
The "Rules" tab should now be visible.
If not, it should be visible at the latest after a "reload" of the page.
On MQTT#Value_of_TestTopic1 do
if [MQTT#Value_of_TestTopic1]=1.00
pbani,5,150,0,0,0,0,0,
endif
endon
You can then enter the rules shown in the "Rules" tab, for example. These have the effect that every time the topic "Value_of_TestTopic1" a one is received, the command "pbani,5,150,0,0,0,0,0," is executed.
This command is part of the pxlBlck plugin. You can also find information about it here pxlBlck - Commands for configuring the pxlBlck
Additional sensors for connection to the ESP8266 / ESP32
An overview of other articles for connecting sensors and actuators to ESPEasy can be found in the following category.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂