How to connect MQTT Mosquitto to SSLPi?
MQTT is a lightweight protocol, which can be used to exchange messages between sensors and actuators. In this article I introduce a simple integration with the SSLPi. A more detailed configuration of the MQTT broker is not discussed here.
When using the MQ Telemetry Transport Protocol (MQTT), there are 3 important components. A central broker, at least one subscriber/actuator and at least one publisher/sensor. The subscriber registers at the the broker and tells him which messages he wants to have. If a sensor now sends a message to the broker, it decides which subscriber is to receive it and sends it to the subscriber.
The Mosquitto Broker and Client are installed on the Raspberry pi. The module "python-mosquitto" is not required, because therefore we have the SSLPi server.
First install the Mosquitto MQTT Broker and Client on the Raspberry pi:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install mosquitto mosquitto-clients
After the installation the MQTT broker is already running.
The following commands can be used to stop, start or get the status of the MQTT Broker.
sudo service mosquitto status
sudo service mosquitto stop
sudo service mosquitto start
For a first test, please open a second SSH session on the Raspberry pi:
In one session, we start the subscriber with:
mosquitto_sub -d -h 127.0.0.1 -t test
In the second session, we will launch a publisher with:
mosquitto_pub -h 127.0.0.1 -t test -m 'Hello World'
If everything is OK, you should receive the message 'Hello World' in the Subscriber.
Now you can integrate the Mosquitto MQTT subscriber into the SSLPi server.
First, create a file in the directory of the SSPi/app (default directory is / home/pi/SSLPi/app):
sudo nano StartMQTTSub.sh
In this file we now call the Mosquitto Subscriber as mentioned in the test above. Here is the code:
#!/bin/sh
echo start MQTT Subscriber
mosquitto_sub -h 127.0.0.1 -t test
res=$?
if [ $res = "0" ]; then
exit 0
else
echo "Error" 1>&2
exit 1
fi
Then save with Ctrl + O and exit with Ctrl + X.
Then you have to make the file executable with
sudo chmod +x StartMQTTSub.sh
Next, you must configure the application that calls the MQTT Subscriber to the SSL Pi server.
With
cd ..
you Cchange to the directory where configuration file SSLPi.cfg is located and with
sudo nano SSLPi.cfg
you edit the config file.
At the end of the file add the following lines:
#Run Mosquitto Subscriber
17:MQTT;1;True;cd;1;0; /home/pi/SSLPi/app/
18:MQTT;2;False;./StartMQTTSub.sh;7;0
If you now restart the SSLPi server, the SLLPi TestClient or the Android SSLPi Client can start the subscriber using the command "MQTT". The clients now receive the 'Hello World' message as a push notification, as soon as the publisher is using the following command on the the Raspberry pi
mosquitto_pub -h 127.0.0.1 -t test -m 'Hello World'
At this point I should mention that Mosquitto still has a variety of further configuration options. You can get a pretty good insight on the wiki page.