MQTT Example
MQTT Example
For this example we are using a Rocky 9 with a Mosquitto installation. For more information about Windows related installations refer to the internet. For more information about Mosquito refer to the Mosquitto Website.
In this tutorial, we'll install Mosquitto, retrieve SSL certificates from Let's Encrypt, and set up our broker to use SSL to secure our password-protected MQTT communications.
Requirements
- Rocky 9 basic server install
- Environment name
- Editor (nano / vim etc)
Installation
Mosquitto can be found in the epel repository of CentOS 7. To install Mosquitto run the following commands with root access:
dnf -y upgrade
dnf -y install epel-release
dnf -y install mosquitto
Enable/Start Mosquitto
To start and setup as a service, run the following commands:
systemctl enable --now mosquitto
Test the Mosquitto installation
To test your Mosquitto installation we are going to setup a subscription on a specific topic and send something to this topic. This is a Publish-Subscribe mechamism or PUBSUB as we say it.
Topics are descriptions that you announce communications to and subscribe to.
Login into your server as root or sudo to root. Use Putty or, if you use a GUI open 2 terminal sessions on your desktop, like this:
In the first window we subscribe to a topic. Run the following command:
mosquitto_sub -h localhost -t test
Now that we have subscribed to the topic test, lets send a text to this topic, in our second terminal, to see if we get any result.
mosquitto_pub -h localhost -t test -m "ConnectPlaza Mosquitto test"
If all is ok, you should see the next result:
Your server is up and running. You can disconnect from the subscriber using <CTRL-C>.
MQTT-Listener with ConnectPlaza
Download the following example: MQTT-Example.cinterface
Import this file in your test environment, bundle it and deploy this to your test environment.
The MQTT Listener subscribes as in the previous example to the topic 'test'. The message will be send to Analyze and a file will be produced with the content of the messages send to the topic via the mosquitto_pub command.
But first take a look at the MQTT listener:
Nothing special here. We connected to the URL on the localhost with the standard Mosquitto port number 1883. The Client URL can be anything. As you can see we subscribed to one topic, 'test', the rest is default.
De File Drop is simple, we save the file in the ConnectPlaza installation directory ./temp/output. This directory will be automatically created if it does not exist.
Now execute the following command.
mosquitto_pub -h localhost -t test -m "ConnectPlaza Mosquitto test"
In the output directory you will find a file like the following:
Open the file and you can read the message set in the publisher.
MQTT Producer with ConnectPlaza
Lets send the message via an MQTT Producer. We create a message file test.txt with the following text: ConnectPlaza Send Test. This file will be picked up by ConnectPlaza and a MQTT-Producer will publish the message to the topic 'test'. The MQTT Listener from the previous example will pick up the message and drop the file to the same ./temp/output directory.
This looks as follows:
The File Pickup will pick up the file out of the ./temp/input directory. This is configured like this:
We publish the message via the MQTT Sender to the 'test' topic:
Send the test.txt file to the input directory and check if you have a result in your output directory. If all went well, you have a file. Open this file and check if the message is the same as in the text file.