MQTT
MQTT stands for MQ Telemetry Transport. It is a communication protocol specifically designed to minimize the bandwidth required for communication in a high-latency network. Thus, the protocol is ideal for use in machine-to-machine communications or IoT, which stands for "Internet Of Things".
Installing MQTT server - Debian 9
All commands below are executed by the root user or by a user with administrative rights (for example, using the sudo command).
To begin, we will download the key:
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
When download is finished install the key:
apt-key add mosquitto-repo.gpg.key
Create the file with the list where the repository of the files to be installed is located:
nano /etc/apt/sources.list.d/mosquitto.list
After opening an empty file writen the following using our preferred editor:
deb http://repo.mosquitto.org/debian stretch main
Then update the information related to packages:
apt update
In case the command result ends with an error due to the absence of the driver method /usr/lib/apt/methods/https
the installation of the following package will be necessary:
apt install apt-transport-https
After that repeat the command to update the information related to packages:
apt update
Now install the mosquitto server and client:
apt install mosquitto mosquitto-clients
After installation, we check if the mosquitto server is running:
systemctl status mosquitto
If the service/server is not active, we will start the server/service:
systemctl start mosquitto
To ensure the service starts automatically at system boot, we will use the command:
systemctl enable mosquitto
Next, we will test the operation of the service/server by creating a subscription with the topic test:
mosquitto_sub -h localhost -t test
to which we will publish the message hello from another terminal/console, possibly by another user:
mosquitto_pub -h localhost -t test -m "salut"
the message that will appear in the first console:
indicating the correct functioning of the mqtt service/server; closing the open subscription is done with the keys Ctr+c:
Considering that publishing to a topic is not secure, we will create a file regarding authorization for this service/server, using your preferred editor, in my case nano:
nano /etc/mosquitto/conf.d/auth.conf
where we will write the following two lines:
allow_anonymous false
password_file /etc/mosquitto/passwd
Create the passwd file mentioned earlier and the user esp8266 with the corresponding password:
mosquitto_passwd -c /etc/mosquitto/passwd esp8266
after which we restart the service/server:
systemctl reload mosquitto
Then after restart test the capability of the newly created user esp8266 to create subscriptions:
mosquitto_sub -h localhost -t test -u "esp8266" -P "parola"
and to publish, using another console, possibly with another user:
mosquitto_pub -h localhost -t test -m "hello" -u "esp8266" -P "password"
The correct operation will be indicated by the appearance of the message in the first console, just like in the previous test.