HowTo: ESPEasy - Configure MQTT Server and Subscribe to Topics

ESPEasy can now read out very many different sensors and / or control actuators (see also ESPEasy - Sensors & Actuators). Mithilfe der “Rules” kann dann lokal auf dem ESP darauf reagiert und entsprechend gehandelt werden.

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.

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.

Sobald Ihr die Konfigurationswebseite geöffnet habt, müsst Ihr auf den “Controllers”-Tab wechseln. Klickt dazu auf den “Controllers”-Button.

Klickt nun auf den “Add”-Button in einer der noch leeren Zeilen.

This will redirect you to a page where a drop-down menu will be displayed.

Öffnet das Drop-Down-Menü indem Ihr darauf klickt und wählt den Eintrag “Home Assistant (openHAB) MQTT” aus.

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

Außerdem müsst Ihr die Logindaten zu Eurem MQTT-Server angeben, wenn dieser eine Authentifizierung erfordert. Tragt dazu Benutzername und Passwort in die entsprechenden Felder ein und setzt den Haken bei “Use Extended Credentials”.

You can configure the remaining settings as shown. For the sake of completeness, here is a short description.

  • “Controller Client ID”: Hier könnt Ihr den Namen/ID Eures Controllers gegenüber dem MQTT Server festlegen.
  • “Unique Client ID on Reconnect”: Ist dieser Haken gesetzt wird bei einem neuen verbindungsversuch eine zufällige ID angehängt. Dies verhindert (sehr wahrscheinlich), dass zwei Clienten mit der gleichen ID verbunden sind.
  • “Publish Retain Flag”: Ist dieser Haken aktiviert werden veröffentlichte Werte inkl. “Retain Flag” gesendet.
  • “Controller Subscribe”: Dieses Topic wird vom Controller aboniert. Über dieses Topic können dann zum Beispiel Befehle an ESPEasy gesendet werden. Informationen dazu findet Ihr auch im Artikel ESPEasy - Execute commands and actions via HTTP, MQTT, UDP and directly.
  • “Controller Publish”: Hier wird das Topic festgelegt unter dem Sensorwerte (falls aktiviert) veröffentlicht werden.
  • “Controller LWT Topic”: Über das LWT(=”Last Will/Testament”) kann der MQTT-Server über den Verbindungsstatus des Clienten berichten.
  • “LWT Connect / Disconnect Message”: Mithilfe dieser Option kann die Nachricht festgelegt werden, welche über das LWT Topic im Verbindungsfall oder bei Verbindungsabbruch gesendet wird.
  • “Send LWT to broker”: Aktiviert/Deaktiviert die LWT-Funktionalität
  • “Will Retain”: Setzt das “Retain Flag” für die über das LWT Topic versendeten Nachrichten.
  • “Clean Session”: Aktiviert / Deaktiviert eine Anmeldung einer “sauberen Sitzung”
  • “Enabled”: Aktiviert / Deaktiviert diesen Controller

Sobald Ihr alle Einstellungen nach Euren Wünschen konfiguriert habt, könnt Ihr die Einstellungen durch einen Klick auf “Submit” übernehmen.

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.

Wechselt dazu zunächst in den “Devices”-Tab indem Ihr auf den “Devices”-Button klickt.

If you have not already done so, you must first configure a sensor.

In this example the DHT11 is used.

Klickt dazu im “Devices”-Tab auf den “Add”-Button in der ersten leeren Zeile.

Konfiguriert dann die Option für “GPIO Data” und “Sensor model” Eurem Sensor entsprechend.

But now to the MQTT part:
Damit ESPEasy die Sensordaten an den MQTT-Server sendet müsst Ihr den Haken im Abschnitt “Send to Controller” aktivieren. Da Ihr die Daten an den first Controller senden wollt ist der Haken neben der “1” aufgeführt.

Mithilfe der “Interval”-Option könnt Ihr festlegen wie oft der Sensor ausgelesen wird. Die Daten werden dann im gleichen Intervall versendet. Im dargestellten Beispiel wird der Sensorwert also jede Sekunde ausgelesen und über MQTT versendet.

Speichert diese Einstellungen wieder durch einen Klick auf “Submit”.

Zurück im “Devices”-Tab könnt Ihr nun auch den eingetragenen Controller sehen.

An external MQTT client can then also be used to check whether the data is being sent.

Im Bild seht Ihr einen Ausschnitt aus der Ansicht des “MQTT Explorer”. Dieses Tool ist kostenlos und ist sehr hilfreich beim Umgang mit MQTT Botschaften.


Create MQTT Device to subscribe to a MQTT Topic

Mithilfe eines weiteren virtuellen “Devices” könnt Ihr auch bestimmte andere Topics abonnieren. Die darüber empfangenen Werte können dann zum Beispiel mit Regeln weiterverarbeitet werden.

Wechselt dazu wieder in den “Devices”-Tab indem Ihr auf den “Devices”-Button klickt.

Klickt dann auf den “Add”-Button in der ersten leeren Zeile.

Daraufhin werdet Ihr wieder zu einem DropDown-Menü weitergeleitet. Wählt hier den Eintrag “Generic – MQTT Import” aus.

In dem dann dargestellten Menü könnt Ihr bis zu vier Topics abonnieren. Tragt diese dazu unter “MQTT Topic 1 – 4” ein.

Im Abschnitt “Values” könnt Ihr dann noch die Namen der Variablen festlegen. Diese Variablen beinhalten später die über MQTT empfangenen Werte. Dies ist zum Beispiel wichtig, wenn Ihr mithilfe von “Rules” auf diese Werte reagieren wollt.

Wen Ihr alles wie gewünscht konfiguriert habt prüft nochmal, dass Ihr den Haken bei “Enabled” gesetzt habt und bestätigt die Einstellungen durch einen Klick auf “Submit”.

Nun könnt Ihr mithilfe von Regeln auf die abonierten Topics reagieren. Dazu müsst Ihr zunächst in den “Rules”-Tab wechseln.

If it is not visible, you have to activate it first.

Klickt dazu auf “Tools” um zum “Tools”-Tab zu wechseln.

Klickt dort auf “Advanced”…

…und setzt einen Haken bei der Option “Rules”.

Bestätigt diese Einstellung dann durch einen Klick auf den “Submit”-Button.

Nun sollte der “Rules”-Tab sichtbar sein.

Falls nicht sollte er spätestens nach einem “Reload” der Seite sichtbar werden.

On MQTT#Value_of_TestTopic1 do
if [MQTT#Value_of_TestTopic1]=1.00
pbani,5,150,0,0,0,0,0,
endif
endon

Im “Rules”-Tab könnt Ihr dann zum Beispiel die dargestellten Regeln eintragen. Diese bewirken, dass jedes Mal, wenn über das Topic “Value_of_TestTopic1” eine eins empfangen wird, der Befehl “pbani,5,150,0,0,0,0,0,” ausgeführt wird.

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

Buy Me a Coffee at ko-fi.com       

Kommentar hinterlassen

Your email address will not be published. Erforderliche Felder sind mit * markiert


The reCAPTCHA verification period has expired. Please reload the page.