HowTo: Install FHEM – MQTT module

In the article RaspberryPi - Install MQTT Server on the RaspberryPi I had written a little about MQTT and explained how to install an own MQTT server on the RaspberryPi.

In order to be able to use an MQTT server from FHEM, you have to install a few FHEM modules. How to do that and what you have to pay attention to I have described in the following article.


Overview

Bevor mit der Installation des MQTT Moduls begonnen werden kann solltet ihr den RaspberryPi soweit vorbereitet haben, dass FHEM auf diesem installiert und über das Webmenü konfigurierbar ist.
The following three articles describe what needs to be done to prepare the RaspberryPi:
RaspberryPi – setup for nerdiys!
RaspberryPI - The First Configuration!
RaspberryPi – Control the RaspberryPi via SSH
FHEM – Installation auf dem RaspberryPi
RaspberryPi - Install MQTT Server on the RaspberryPi

Required tool:
-no-

Required material:

In the following list you will find all the parts you need to implement this article.


Log in to the RaspberryPi via SSH

To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.

After entering your username and password you can enter the first commands.

Update package management

The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.

To start the update of the package management you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long ago your last update of the package management was, this process can now take a while. The lists that refer to the individual package sources are updated first.
After that, the packages themselves are updated. Since additional memory is occupied, you will be asked again for your consent. You have to confirm this with a "J" and "Enter".
Once the update is complete, you will see a small summary of the duration and scope of the update.

Install Perl MQTT packages

Damit FHEM mit dem MQTT Server kommunizieren kann müssen zwei Perl Paket installiert werden.
Dazu müsst ihr euch mit Putty auf dem RasPi einloggen und folgende Befehle eingeben:

sudo cpan install Net::MQTT:Simple sudo cpan install Net::MQTT:Constants

Die Nachfrage “Would you like to configure as much as possible automatically? [yes]” bestaetigt ihr mit “yes” und Enter.

The installation of the two packages took me about five minutes. So time enough to get a new coffee 🙂 .

FHEM neu starten

Damit die neu installierten Pakete von FHEM erkannt werden muss FHEM einmal neu gestartet werden. Dazu müsst ihr die folgenden beiden Befehle nacheinander eingeben. Dadurch wird der Server erst gestoppt und dann wieder gestartet. Weiter Infos zum stoppen und starten findet Ihr auch in dem Artikel FHEM – Aufbau, Module & Wissenswertes

sudo /etc/init.d/fhem stop sudo /etc/init.d/fhem start


MQTT Clienten definieren

Damit Ihr nun über FHEM MQTT Daten senden und empfangen könnt muss sich FHEM gegenüber dem MQTT Server als Client anmelden. Diesen Clienten müsst Ihr nur einmal anlegen. Alle in FHEM angemeldete Sensoren bzw. Aktoren werden dann später über diesen Clienten mit Daten versorgt bzw. können über diesen Ihre Daten versenden.

Um den MQTT Clienten in FHEM anzumelden müsst ihr auf die Weboberfläche von FHEM wechseln.
Dort ist im oberen Bereich ein Eingabe Feld über das wir die Definition des MQTT Clienten eingeben können.

Die Defintion lautet(zum Beispiel):

define mqttClient MQTT 127.0.0.1:1883

Durch den Befehl wurde der MQTT Client in FHEM erstellt. Dieser kann so aber noch nicht viel.


Um die Funktion zu testen erstellen wir ein MQTT_ DEVICE. Über dieses Modul können in FHEM MQTT-Topics abonniert und auch Daten veröffentlicht werden.

Die Definition zu unserem kleinen Test-Modul lautet:

define mqttTest MQTT_DEVICE; attr mqttTest subscribeReading_testTopic testTopic; attr mqttTest publishSet_testTopic testTopic;
Diese gebt ihr wieder im Textfeld auf der Webkonfigurationsseite von FHEM ein.

Nach der Definition des Test-Moduls werdet ihr nicht direkt auf dessen Konfigurationsseite weitergeleitet. Ihr findet es im “Raum” „Unsorted“ und dann unter „mqttTest“.

Die Konfigurationsseite des eben definierten Moduls.

Send test message and check reception

Um nun eine Testnachricht zu senden und zu empfangen müsst ihr auf die Konfigurationsseite des definierten Moduls wechseln. Dort tragt ihr in das rot eingekreiste Textfeld einen beliebigen Text ein.

Diesen Text könnt ihr dann durch klicken auf „set“ absenden. Dieser wird dann über MQTT mit dem Topic „testTopic“ an den MQTT Server gesendet.

Da wir bei der Definition des Moduls das Topic „testTopic“ aber auch abonniert haben, bekommen wir direkt in diesem Augenblick ein Update auf dieses Reading “testTopic”. Der von uns abgesendete Text wurde also wieder empfangen. Die Konfiguration in FHEM funktioniert also. 🙂

Ihr habt eure FHEM Installation nun also MQTT fähig gemacht.

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.