ESPEasy can read out a large number of different sensors and / or control actuators (see also ESPEasy – Sensors & Actuators). With the help of the “Rules” it is then possible to react to this locally on the ESP and act accordingly.
But the collected sensor data can also be sent to other devices. For example, you can collect or process this data in your SmartHome system. Sending data via the MQTT protocol is ideal for this.
How to register your ESPEasy instance to an MQTT server is described in the following article.
- 1 Safety instructions
- 2 Affiliate links / advertising links
- 3 Requirements
- 4 Which MQTT server?
- 5 Configure the login to the MQTT server
- 6 Configure the sending of sensor data
- 7 Create an MQTT device to subscribe to an MQTT topic
- 8 Additional sensors for connection to the ESP8266 / ESP32
I know the following hints are always a bit annoying and seem unnecessary. But unfortunately, many people who knew it "better" from carelessness lost their eyes, fingers or other things or hurt themselves. In comparison, a loss of data is almost not worth mentioning, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Even the coolest project is worth no injury or other annoyance. https://www.nerdiy.de/en/sicherheitshinweise/
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and shop via this link, Nerdiy.de receives a commission from the online shop or provider concerned. The price doesn't change for you. If you do your purchases via these links, you will support Nerdiy.de in being able to offer further useful projects in the future. 🙂
You can find additional tips on ESPEasy in the following articles.
- ESPEasy – Execute commands and actions via HTTP, MQTT, UDP and directly
- ESPEasy – Compile firmware, flash it and configure WiFi access data
- Category – ESPEasy
– none –
Which MQTT server?
There are now several MQTT servers that can be used. For example, there are also public MQTT servers that can be accessed via the Internet.
Personally, I don’t want to use this because I think it has two drawbacks:
- My data leaves my network and ends up on a foreign server. Even if the data protection standards are now very high, attacks / errors can still result in my data being published.
- The public MQTT server can only be reached if there is also an internet connection to this server. Should this fail, the forwarding of the data to that point would also fail. This can also have the consequence that possibly related automations no longer work.
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 information about this in the article RaspberryPi – Install MQTT Server on the RaspberryPi.
Configure the login to the MQTT server
So that you can enter the MQTT server in your ESPEasy instance, the ESPEasy firmware should already be installed on the ESP8266 and accessible. You can find information about this in the article ESPEasy – Compiling, flashing and configuring WLAN access data.
First open the configuration website of your ESP8266 programmed with ESPEasy by entering its IP address in your web browser. Information on how to find out the IP address is also summarized in the article Displaying / Finding out the IP address of the devices in the network.
As soon as you have opened the configuration website, you have to switch to the “Controllers” tab. To do this, click on the “Controllers” button.
Now click on the “Add” button in one of the empty lines.
This will take you to a page with a drop-down menu.
Open the drop-down menu by clicking on it and select the entry “Home Assistant (openHAB) MQTT”.
You will then be redirected to the configuration page of this controller (= MQTT server). First, you can choose here whether you want to enter the address of the MQTT server as a host name or IP address.
In the example shown here, I’m using a hostname. But you can also use an IP address. If you do that, you should configure your router so that the IP address is fixed.
You can adopt the other settings as shown.
It is important that the port is set on the configured port of your MQTT server (1883 by default).
You also have to enter the login data for your MQTT server if it requires authentication. To do this, enter your user name and password in the appropriate fields and check the box next to “Use Extended Credentials”.
You can configure the remaining settings as shown. For the sake of completeness, a brief description is given below.
- “Controller Client ID”: Here you can specify the name / ID of your controller for the MQTT server.
- “Unique Client ID on Reconnect”: If this checkmark is set, a random ID is appended when a new connection attempt is made. 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. This topic can then be used, for example, to send commands to ESPEasy. You can find information about this in the article ESPEasy – Executing commands and actions via HTTP, MQTT, UDP and directly.
- “Controller Publish”: Here the topic is specified under which the sensor values (if activated) are published.
- “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 specify the message that is sent via the LWT Topic in the event of a connection or a 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 registration of a “clean session”
- “Enabled”: Activates / deactivates this controller
As soon as you have configured all settings according to your wishes, you can apply the settings by clicking on “Submit”.
Your controller should then be listed in the controller overview.
If you have configured more than one controller, you should note the number of the line in which the MQTT server is configured. In this example, the MQTT server is the first controller.
Configure the sending of sensor data
After the MQTT server has been configured, you can now also send the data from a connected sensor to your previously configured MQTT server via MQTT.
First switch to the “Devices” tab by clicking on the “Devices” button.
If you haven’t already done so, you must first configure a sensor.
In this example the DHT11 is used.
To do this, click in the “Devices” tab on the “Add” button in the first empty line.
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 have to activate the check mark in the “Send to Controller” section. Since you want to send the data to the first controller, the tick is listed next to the “1”.
With the “Interval” option you can determine how often the sensor is read out. The data is then sent in 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 entered controller.
An external MQTT client can then also be used to check whether the data is being sent.
In the picture you can see an excerpt from the view of the “MQTT Explorer”. This tool is free and is very helpful when dealing with MQTT messages.
Create an MQTT device to subscribe to an MQTT topic
With the help of another virtual “device” you can also subscribe to certain other topics. 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 entry “Generic – MQTT Import”.
You can subscribe to up to four topics in the menu that is then displayed. Enter this under “MQTT Topic 1 – 4”.
In the “Values” section you can then specify the names of the variables. These variables later contain the values received via MQTT. This is important, for example, if you want to react to these values using “Rules”.
If you have configured everything as desired, check again that you have ticked “Enabled” 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 this is not visible, you have to activate it first.
To do this, click on “Tools” to switch to the “Tools” tab.
Click on “Advanced” …
… and check the “Rules” option.
Confirm this setting by clicking on the “Submit” button.
The “Rules” tab should now be visible.
If not, it should be visible after a “reload” of the page at the latest.
In the “Rules” tab you can then, for example, enter the rules shown. These have the effect that every time a one is received via the “Value_of_TestTopic1”-topic, the “pbani, 5,150,0,0,0,0,0,” command is executed.
On MQTT#Value_of_TestTopic1 do
This command is part of the pxlBlck plugin. Information about this can be found here pxlBlck – commands for configuring the pxlBlck
Additional sensors for connection to the ESP8266 / ESP32
You can find an overview of other articles for connecting sensors and actuators to ESPEasy in the following category.
I hope everything worked as described. If not or you have any other questions or suggestions, please let me know in the comments. Also, ideas for new projects are always welcome. 🙂
P.S. 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 appreciate it that I share these information with you, I would be happy about a small donation to the coffee box. 🙂