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.
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/
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
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.
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.
sudo apt-get update && sudo apt-get upgrade
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.
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;
Testnachricht senden und Empfang prüfen
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. 🙂
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. 🙂