HowTo: FHEM – Installing MQTT module

In the article RaspberryPi – Install MQTT server on the RaspberryPi I had written a little to MQTT and how to install your own MQTT server on the RaspberryPi.

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


Safety instructions

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/sicherheitshinweise/


Overview

Before you can start installing the MQTT module, you should have prepared the RaspberryPi to the extent that FHEM is installed on it and can be configured via the web menu.
The following three articles describe what to do to prepare the RaspberryPi so far:
RaspberryPi – Setting up for Nerdiys!
RaspberryPi – The first configuration
RaspberryPi – Controlling the RaspberryPi via SSH
FHEM – Installation on the RaspberryPi
RaspberryPi – Install MQTT server on the RaspberryPi

Required tools:
-none-

Required material:

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


Log in via SSH on the RaspberryPi

To get started, you first need to log in to RasPi with SSH on Putty. How to do it is described in the article RaspberryPi - Controlling the RaspberryPi via SSH.

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

Update package management

The package management in Linux is a "central point" over which various software packages can be installed. For this to work reliably, the lists and sources of package management should be updated before installing any new packages.

To start the package management update you have to enter the following command.
sudo apt-get update && sudo apt-get upgrade
Depending on how long your last update of the package management is, this process can take some time. First, the lists are updated in which the individual repositories are referenced.
Then the packages themselves are updated. As this additional memory is occupied, you will again asked for your consent. You have to confirm this with a "J"(in German) or a "Y" and "Enter".
When the update is complete, you will see a small summary of the duration and scope of the update.

Install Perl MQTT packages

In order for FHEM to communicate with the MQTT server, two Perl packages must be installed.
To do this you must log in with Putty on the RasPi and enter the following commands.

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

The question “Would you like to configure as much as possible automatically? [Yes]” is confirmed with “yes” and Enter.

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

 

 

 

 

 

 

 

 

 

 


Restart FHEM

In order for the newly installed packages to be recognized by FHEM, FHEM must be restarted once. To do this you must enter the following two commands one after the other. This stops the server and then restarts it. Further information about stopping and starting can be found in the article FHEM – Structure, Modules & Worth Knowing.

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

 

 

 

 

 

 

 

 

 


Define MQTT client

In order for you to be able to send and receive data via FHEM-MQTT, FHEM must log on to the MQTT server as client. You only have to create this client once. All sensors or actuators registered in FHEM are then later supplied with data via this client or can use this to send their data.

In order to register the MQTT client in FHEM you have to switch to the web interface of FHEM.
There is an input field in the upper area where we can enter the definition of the MQTT client.

The definition is (for example):

define mqttClient MQTT 127.0.0.1:1883

The command created the MQTT client in FHEM. But this one can not do much.


To test the function, we create an MQTT_ DEVICE. Through this module you can subscribe to FHEM MQTT topics and also publish data.

The definition for our small test module is:

define mqttTest MQTT_DEVICE;
attr mqttTest subscribeReading_testTopic testTopic;
attr mqttTest publishSet_testTopic testTopic;
You enter this again in the text field on the web configuration page of FHEM.

After defining the test module, you will not be redirected directly to its configuration page. You find it in the “room” “Unsorted” and then under “mqttTest”.

The configuration page of the just defined module.

Testnachricht senden und Empfang prüfen

In order to send and receive a test message you have to change to the configuration page of the defined module. There you enter in the red circled text box any text.

You can send this text by clicking on “set”. This is then sent via MQTT with the topic “testTopic” to the MQTT server.

Since we also subscribed to the topic “testTopic” in the definition of the module, we get an update on this reading “testTopic” right now. The text sent by us was therefore received again. The configuration in FHEM works. 🙂

You have now made your FHEM installation MQTT capable.

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

Fab

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

Buy Me a Coffee at ko-fi.com

Leave a Reply

Your email address will not be published. Required fields are marked *