Mosquitto MQTT Server is a message broker which works over MQTT protocol. MQTT is lightweight messaging protocol that is ISO standard for use on top of TCP/IP protocol. It is mostly used for communicating with Internet of Things devices. We are going to install Mosquitto on a Ubuntu 16.04 server and we are going to send messages from MQTT-spy software. Mosquitto is an Eclipse project and it is distributed under EDL license. So lets start.
Compiling Mosquitto MQTT server from source
To get mosquitto installed we need to do following steps. First update sources lis and get dependencies
apt-get install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
Add mosquitto user, because by default it runs as non-root.
For convenience we will ad sudo rights to this user, because we will do rest of install process as this user
usermod -aG sudo mosquitto
Then we will log in as this user and cd to his home dir
Next we will download Mosquitto source code, latest version is 1.4.9 at the time of writing, but I suggest you check the download page to see if there is new version. So lets download the latest version
Lets unpack it and cd into directory
tar xvzf mosquitto-1.4.9.tar.gz
And then we compile and install the software
make && sudo make install
Configuring Mosquitto MQTT Server
After install is done, we need to make password for new user.
sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
You will be prompted to make password for new mqtt-spy user that we will use to connect from client. That is different that mosquito user, that is system user for running mosquitto server. We need to add permissions to this mosquitto user to all relevant directories
sudo mkdir /var/lib/mosquitto/
sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/
We next need to make configuration file for Mosquitto MQTT Server, so lets use nano to make new file
sudo nano /etc/mosquitto/mosquitto.conf
There you can paste this
Config seems long but we added more verbose logs and password file.
After the config is saved, we run ldconfig
Lets add systemd unit file
sudo nano /etc/systemd/system/mosquitto.service
Paste this there:
Description=Insite MQTT Broker
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Lets start the service
sudo systemctl start mosquitto.service
And check its status
systemctl status mosquitto.service
To make it start on boot do this command
sudo systemctl enable mosquitto.service
Setting up MQTT-Spy and connectiong
MQTT-Spy is java based client that we will use to connect to MQTT server. You can get it from github but note that you must have Oracle JDK installed. It does NOT work with OpenJDK. First thing to do after staring MQTT-spy is to create new connection. You add your server IP and default port 1883.
Then you add password and user name we made earlier, in my case it is mqtt-spy and password is password.
Then we can open terminal window on server and type following command
mosquitto_sub -v -t 'linoxide/topic' -u mqtt-spy -P password
To explain flags, -v is for verbosity, -t is for topic followed by topic inside quotes, -u is for user and -P is password. After typing this command it will seeming hang, but then we need to use MQTT-spy to create new topic and send a message to same topic, like on picture bellow.
We have successfully installed Mosquitto MQTT server that enables you to have network of connected IoT devices over MQTT 3.1 protocol. We installed it on Ubuntu on classic x86 PC which is what most people use for development and learning purposes. For real IoT you would want an ARM device. This is all for this article thank you for reading.