HL340 RS232 Modul HiveMQ MQTT Beispiel
Tasmota,  UART seriell

Tasmota HiveMQ RS232 MQTT Beispiel

Tasmota Telemetrie Clients MQTT infrastructure
HiveMQ MQTT

Im folgenden Beispiel zur MQTT Anbindung eines Tasmota Moduls wird zwar HiveMQ als MQTT Broker verwendet, im Grunde ist die Einbindung innerhalb Tasmota stets identisch.

Grundlegend basiert die Tasmota MQTT Anbindung auf den typischen Parametern

  • MQTT Broker Adresse / Url
  • MQTT Benutzername
  • MQTT Passwort

Ferner gibt es weitere Optionen, beispielsweise im Bezug HiveMQ wird nur das MQTT Protokoll über TLS unterstützt. Dementsprechend muß die Tasmota Firmware über MQTT TLS verfügen, im Detail bei der Kompilierung die Option

#define USE_MQTT_TLS

aktiviert sein.

Oftmals wird ein MQTT Broker fälschlicherweise auch als ein “Server mit einem Webinterface” beschrieben. Ein MQTT Broker ist lediglich als Vermittler zwischen einer Vielzahl verschiedener Teilnehmer zu verstehen. Das ganz basiert dabei auf einer einfachen Grundlage es wird

  • abonniert
  • publiziert

stets basierend auf Topics / Themen.

Übersetzt man nun die Topics auf Tasmota Module in Kombination mit typischer Infrastruktur, PC, Tablet, Mobiltelefon entspricht jedwede Hardware einem MQTT Client. Jeder MQTT Client meldet sich über die jeweilige URL / IP Adresse typischerweise über einen Benutzrnamen sowie Passwort beim MQTT Broker an.

Nach erfolgreicher Authentifizierung wird em MQTT Broker mitgeteilt welche Topics man abonnieren, bzw. in welche Topics publiziert werden soll.

Basierend auf diesen Grundlagen erfüllt der MQTT Server seine Funktion als Broker, Daten gezielt durchzureichen.

HiveMQ MQTT

In the following example of the Tasmota Module MQTT connection  HiveMQ is used as the MQTT broker, but the integration within Tasmota is basically always identical.

The Tasmota MQTT connection is fundamentally based on the typical parameters

  • MQTT broker address/url
  • MQTT username
  • MQTT password

There are also other options, for example with HiveMQ only the MQTT protocol over TLS is supported. Accordingly, the Tasmota firmware must have MQTT TLS, in detail the option during compilation

#define USE_MQTT_TLS

be activated.

An MQTT broker is often incorrectly described as a “server with a web interface”. An MQTT broker is simply to be understood as an intermediary between a large number of different participants. This is all based on a simple basis

  • subscribed
  • published

always based on topics.

If you now translate the topics to Tasmota modules in combination with typical infrastructure, PC, tablet, mobile phone, … any hardware corresponds as a MQTT client. Each MQTT client logs in to the MQTT broker via the respective URL / IP address, typically using a user name and password.

After successful authentication, the MQTT broker is informed which topics subscribe to and/or which topics should be published in.

Based on these basics, the MQTT server fulfills its function as a broker to pass data through in a targeted manner.

HiveMQ MQTT Broker Cluster setup
MQTT Broker einrichten

Die Einrichtung eines MQTT Brokers unter HiveMQ ist denkbar einfach.

Zunächst, insofern man noch nicht über ein HiveMQ Benutzerkonto verfügt muss man sich hier entsprechend registrieren, bzw. anmelden.

Nachdem man sich registriert und angemeldet hat wird man direkt auf die Cluster Verwaltung geleitet, hier lediglich den Button CREATE NEW CLUSTER klicken.

Es erscheint die Übersicht der verfügbaren Cluster Pakete, hier das Paket Serverless FREE durch klick auf Get Started auswählen.

Es erscheint das MQTT Cluster Management wichtig ist hier der Eintrag Cluster URL, das ist die zu verwendende Server URL / MQTT IP Adresse.

Nun wird noch ein MQTT Benutzer sowie Passwort benötigt hierzu ganz oben im Menü von OVERVIEW auf ACCESS MANAGEMENT wechseln.

Im Access Management nun einen Username sowie Password festlegen und im DropDown Permission Publish and Subscribe auswählen. Selbstredend sollte man sich Benutzername und Passwort zur weiteren Verwendung entsprechend kopieren / als Text ablegen.

Die Benutzerdaten durch klicken auf CREATE CREDENTIAL bestätigen, in der Benutzerübersicht erscheint nun der oben festgelegte MQTT Benutzer.

Das war auch schon alles, der MQTT Broker ist aktiv sodass entsprechende Clients angebunden werden können.

MQTT Broker Setup

Setting up an MQTT broker under HiveMQ is  easy.

First of all, if you do not yet have a HiveMQ user account, you must register or log in here.

After you have registered and logged in you will be taken directly to the cluster administration, just click the CREATE NEW CLUSTER button.

The overview of the available cluster packages appears, select the Serverless FREE package by clicking on Get Started.

The MQTT Cluster Management appears. The  Cluster URL entry appears, which is the server URL / MQTT IP address to be used.

Now you need an MQTT user and password to switch from OVERVIEW to ACCESS MANAGEMENT at the top menu.

Now set a username and password in Access Management and select Publish and Subscribe from the Permission drop-down. Of course, you should copy your username and password for further use / save them as text.

Confirm the user data by clicking on CREATE CREDENTIAL; the MQTT user specified above will now appear in the user overview.

That’s all, the MQTT broker is active so that corresponding clients can be connected.

MQTT Explorer Setup HiveMQ MQTT Broker connection
MQTT Explorer

Wie im Vorfeld erwähnt dient der MQTT Broker lediglich als Vermittler zwischen MQTT Clients. Hier im Beispiel wird nun der MQTT Explorer als MQTT Client verwendet.

Der MQTT Explorer ist hier für jedes gängige Desktop Betriebssystem verfügbar.

Nach erfolgreicher Installation müssen lediglich die erforderlichen Parameter, basierend auf dem HiveMQ MQTT Broker eingepflegt wie folgt eingepflegt werden:

  1. klicke auf das Plus Zeichen neben Connections
  2. Lege einen Namen für die Verbindung unter Name fest beispielsweise HiveMQTest
  3. deaktiviere die Checkbox Validate certificate
  4. aktiviere die Checkbox Encryption (tls)
  5. wähle im Protocol DropDown mqtt://
  6. füge im Textfeld Host die Cluster URL vom HiveMQ MQTT Broker ein
  7. gebe im Textfeld Port 8883 ein
  8. gebe im Textfeld Username den zuvor im HiveMQ Cluster festgelegten Username ein
  9. gebe im Textfeld Password das zuvor im HiveMQ Cluster festgelegte Passwort ein
  10. klicke auf save um die gemachten Angaben zu speichern
  11. klicke auf connect

Der erste MQTT Client ist nun mit dem MQTT Broker verbunden. An dieser Stelle sei erwähnt beim MQTT Explorer ist der Name Programm, es werden sämtliche MQTT Client – Verbindungen sowie damit verbundene Topics angezeigt.

MQTT Explorer

As mentioned before, the MQTT broker only serves as an intermediary between MQTT clients. Here in the example the MQTT Explorer is now used as the first MQTT client.

The MQTT Explorer is available here for every common desktop operating system.

After successful installation, all you need to do is enter the required parameters based on the HiveMQ MQTT Broker as follows:

  1. click on the plus sign next to Connections
  2. Set a name for the connection under Name, for example HiveMQTest
  3. uncheck the Validate certificate checkbox
  4. activate the checkbox Encryption (tls)
  5. select mqtt:// in the Protocol DropDown
  6. In the Host text field, insert the cluster URL from the HiveMQ MQTT broker
  7. enter port 8883 in the Port text field
  8. In the Username text field, enter the username previously defined in the HiveMQ cluster
  9. In the Password text field, enter the password previously set in the HiveMQ cluster
  10. click on save to save the information provided
  11. click on connect

The first MQTT client is now connected to the MQTT broker. At this point it should be mentioned that the name of the MQTT Explorer says it all; all MQTT client connections and associated topics are displayed.

Tasmota HiveMQ MQTT Broker configuration Tasmota MQTT first start
Tasmota MQTT Konfiguration

Im Vorfeld sei erwähnt, die Tasmota Firmware muß MQTT TLS unterstützen, andernfalls erfolgt keine Anmeldung am HiveMQ MQTT Broker.

Die Anbindung eines Tasmota Moduls an einen HiveMQ MQTT Broker erfolgt gleichermassen basierend auf den typischen MQTT Parametern unter Verwendung des Tasmota Webinterface:

  1. klicke auf das Configuration
  2. Klicke im Configuration Menu auf MQTT
  3. gebe in das Textfeld Host die Cluster URL vom HiveMQ MQTT Broker ein
  4. gebe im Textfeld Port (1883) 8883 ein
  5. aktiviere die Checkbox MQTT TLS. Ist diese Checkbox nicht vorhanden muss eine Tasmota Firmware mit MQTT TLS Unterstützung installiert werden, beispielsweise über die Tasmota Firmware Upgrade Funktion
  6. gebe im Textfeld User (DVES_USER) dan zuvor im HiveMQ Cluster festgelegten Username ein
  7. gebe im Textfeld Password das zuvor im HiveMQ Cluster festgelegte Passwort ein
  8. klicke auf save um die gemachten Angaben zu speichern

Das Tasmota Modul führt nun einen Neustart aus und verbindet sich mit dem HiveMQ MQTT Broker.

Wechselt man in den MQTT Explorer sieht man bereits die Topic tele/tasmota_??????/LWT.

Tasmota MQTT configuration

It should be mentioned in advance that the Tasmota firmware must support MQTT TLS, otherwise there will be no connection established to the HiveMQ MQTT broker.

The connection of a Tasmota module to a HiveMQ MQTT broker is based on the typical MQTT parameters by using the Tasmota web interface:

  1. click on  Configuration
  2. Click MQTT in the Configuration Menu
  3. enter the cluster URL of the HiveMQ MQTT Broker in the Host text field
  4. enter 8883 in the Port (1883) text box
  5. activate the checkbox MQTT TLS. If this checkbox is not present, Tasmota firmware with MQTT TLS support must be installed, for example via the Tasmota firmware upgrade function
  6. In the User (DVES_USER) text field, enter the user name previously specified in the HiveMQ cluster
  7. In the Password text field, enter the password previously set in the HiveMQ cluster
  8. click on save to save the information provided

The Tasmota module now restarts and connects to the HiveMQ MQTT broker.

If you switch to the MQTT Explorer you can already see the topic tele/tasmota__??????/LWT

Tasmota send RS232 to MQTT Tasmota publish data to MQTT
Tasmota MQTT publish

Zwar verwendet man Tasmota häufig in Kombination mit Sensoren, hier im Beispiel wurde jedoch gezielt ein EI-OT RS232 WLan Bridge Modul verwendet.

Typische Tasmota Sensor Module interagieren meist nur in eine Richtung, es werden Sensordaten publiziert. Der Ablauf ist denkbar einfach gemäß vorgegebenen Intervall erfasst das Tasmota Modul Sensordaten und publiziert diese Daten über die Topic tele und Benennung seiner Modul ID also Topic

tele/tasmota_?????/sensors

Übersetzt auf das Tasmota RS232 Modul erzeugen wir nun die Daten indem wir serielle Daten auf den DB9 Anschluss des RS232 Moduls senden (siehe im nebenstehenden Bild Terminal Texteingabe send a string from ….)

Sobald die Taste ENTER im Terminal gedrückt wird (siehe unteres Bild), werden die Daten (der String)

  • über den HL340 USB TTL Konverter auf
  • die DB9 Buchse des Tasmota RS232 WLan Moduls ausgeben
  • über den 3232 Level Konverter
  • auf GPIO1 und GPIO3 des ESP-01+ gesendet
  • in der Tasmota Console ausgegeben

Betrachtet man sich nun den MQTT Explorer so erkennt man nun eine neue Topic

tele/tasmota_?????/RESULT und sieht dort den String der zuvor über den den seriellen Terminal an das Rs232 Modul / Tasmota gesendet wurde.

Der String wurde im Detail nachdem er empfangen wurde / in der Tasmota Console erschien

  • über WLan an den Router
  • an den HiveMQ MQTT Broker

durchgereicht, es wurde unter der Topic tele/tasmota_?????/RESULT publiziert.

Tasmota MQTT publish

Although Tasmota is often used in combination with sensors. In this example an EI-OT RS232 WiFi bridge module was specifically used.

Typical Tasmota sensor modules usually only interact in one direction and sensor data is published. The process is very simple, according to the specified interval, the Tasmota module collects sensor data and publishes this data via the topic tele and naming its module ID, i.e. topic

tele/tasmota_?????/sensors

Translated to the Tasmota RS232 module, we now generate the data by sending serial data to the DB9 connection of the RS232 module (see terminal text input send a string from …. top image).

As soon as the ENTER key is pressed in the terminal (see image below), the data (the string)

  • via the HL340 USB TTL converter
  • output the DB9 socket of the Tasmota RS232 WiFi module
  • via the 3232 level converter
  • sent to GPIO1 and GPIO3 of the ESP-01+
  • output in the Tasmota Console

If you take a look at the MQTT Explorer you will now see a new topic

tele/tasmota_?????/RESULT including the string that was previously sent via the serial terminal to the RS232 module / Tasmota.

The string was after it was received/appeared in the Tasmota Console

  • via WiFi to the router
  • passed through to the HiveMQ MQTT broker

it was published under the topic tele/tasmota_?????/RESULT.

MQTT send a string thru Tasmota to serial Terminal Serial Terminal receives RS232 Signal thru Tasmota over MQTT
Tasmota MQTT subscribe

Im Eigentlichen basiert das MQTT Prinzip nunmal auf Publizieren und Abonnieren. In Anlehnung an das obige Beispiel müsste nun zunächst das Tasmota Modul eine MQTT Topic abonnieren um etwas zu empfangen.

Das Abonnieren einer Topic ist bereits mit der MQTT Konfiguration innerhalb der Tasmota Firmware erfolgt, bzw. im Detail handelt es sich um die Topic cmnd innerhalb von Tasmota. Im Detail werden sämtliche in diese Topic gesendeten Daten als Tasmota Commands abgearbeitet. 

cmnd/tasmota_?????/

Wechselt man nun in den MQTT Explorer, scrollt im rechten Bereich nach unten bis der Reiter Publish erscheint kann man direkt in MQTT publizieren. Im Detail

cmnd/tasmota_?????/ gefolgt vom Tasmota Command, in unserem Beispiel SerialSend1 um einen String

  • mittels Tasmota UART
  • auf den 3232 Konverter zu senden
  • das RS232 Signal auf den DB9 Anschluss
  • auf den HL340 USB TTL Konverter auszugeben
  • um im seriellen Terminal zu empfangen

Sobald der Button Publish im MQTT Explorer geklickt wird (siehe unteres Bild), werden die Daten (der String)

  • vom MQTT Explorer
  • über Netzwerk
  • an den HiveMQ MQTT Broker geschickt
  • auf Topic cmnd/tasmota_?????/ durchgereicht
  • vom Tasmota Modul empfangen
  • der Befehle SerialSend1 ausgeführt
  • der String über UART
  • auf den 3232 IC ausgegeben
  • zum DB9 Anschluss durchgereicht
  • vom HL340 USB TTL Konverter empfangen
  • im seriellen Terminal empfangen

Aus Sicht des MQTT Explorer wurde publiziert, aus Sicht des Tasmota Moduls wurde abonniert.

Tasmota MQTT subscribe

Actually, the MQTT principle is based on publishing and subscribing. Based on the example above, the Tasmota module would first have to subscribe to an MQTT topic in order to receive something.

Subscribing to a topic has already been done with the MQTT configuration within the Tasmota firmware, or in detail it is the topic cmnd within Tasmota. In detail, all data sent to this topic is processed as Tasmota commands.

cmnd/tasmota_?????/

If you now switch to the MQTT Explorer, scroll down in the right area until the Publish tab appears, you can publish directly in MQTT. In detail

cmnd/tasmota_?????/ followed by the Tasmota Command, in our example SerialSend1 by a string

  • using Tasmota UART
    to send
  • to the 3232 converter
  • the RS232 signal to the DB9 connection
  • to the HL340 USB TTL converter
  • to receive in the serial terminal

As soon as the Publish button is clicked in the MQTT Explorer (see image below), the data (the string)

  • from MQTT Explorer
  • via network
  • sent to the HiveMQ MQTT broker
  • passed through to topic cmnd/tasmota_?????/
  • received from the Tasmota module
  • the SerialSend1 command is executed
  • the string via UART
  • output to the 3232 IC
  • passed through to the DB9 connector
  • received from the HL340 USB TTL converter
  • received in the serial terminal

From the MQTT Explorer perspective it was published, from the Tasmota module perspective it was subscribed.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert