Server Stuff 1:Setting up MQTT on Ubuntu (pt 1)

So I mentioned earlier in a post that I was interested in the MQTT protocol. I really like the pub/sub architecture and the fact that each device can be a data producer and consumer. The most interesting thing though is the way you can use MQTT to interface external resources like twitter, cosm, or any other web service you might want to get set up as a subscriber to your data.

This evenings exercise was to set up an MQTT server and give it a test run. I am running an ubuntu 13.04 server on digital ocean. I chose the open source MQTT server mosquitto and so far so good. I’m going to lay out the steps I went through to get it working to a minimum level.

Step 1: Installing the server

I followed the directions here for Ubuntu but I had to add 1 other step. I did not have the command apt-add-repository, and as the downloads page said I used apt-get to install python-software-properties, but that page didn’t mention I also needed software-properties-common. After that, I used the commands sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa and sudo apt-get update. On the downloads page they just seem to assume that we’ll figure out how to actually install the server itself, but here it is: sudo apt-get install mosquitto 

Step 2: Installing the clients

The downloads page does not list the client software, but it can be installed with the command sudo apt-get install mosquitto-clients. This gives you access to the commands mosquitto_pub and mosquitto_sub for publish and subscribe respectively. The man pages for each are here.

Step 2: Testing the install

As far as I’ve gotten on this first run is to set up the server, start it, and publish and subscribe one thing.

1. >mosquitto –daemon –verbose #stars the server as a daemon, verbose is optional.

2. >mosquitto_sub -t test -d #subscribe to the topic ‘test’ with debugging info.

3. Create a file, publish.txt with the word ‘hello’ in it.

4. >mosquitto_pub -t test -f publish.txt -d

You should see some text like the following in the window running mosquitto_sub:

Client mosqsub/31280-mynamehere sending CONNECT
Client mosqsub/31280-mynamehere received CONNACK
Client mosqsub/31280-mynamehere sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)
Client mosqsub/31280-mynamehere received SUBACKSubscribed (mid: 1): 0
Client mosqsub/31280-mynamehere received PUBLISH (d0, q0, r0, m0, ‘test’, … (6 bytes))hello

There you go! You got MQTT up and running on your machine. The next step is to implement your own python publisher/subscriber using the mosquitto library described here. Now I didn’t do anything with the configuration file, I’m not using SSL/TLS or even a username and password but this is the most basic example, but now that this is working the sky is the limit! How about that, a short, sweet, to the point post. Might oughta make this a thing!


5 thoughts on “Server Stuff 1:Setting up MQTT on Ubuntu (pt 1)

  1. This is great work. I am a novice in Linux and had problems with the instructions for installing mosquito on their site. I followed the instructions above and it worked! It is now a broker for node-red and soon will replace the cloud servers on ninja blocks.

    • Awesome! This was ages ago that I wrote it and I didn’t end up using it for a project but I was considering going back to it for an arduino project and I am glad I wrote it up because otherwise I would have totally forgotten it 😉 Glad it was of use, I forget this blog is still out there sometimes, I should update it more…

  2. Hi I am stuck with error “”Error: Unknown option ‘-daemon’.””. Any help. I am newbie to Linux and Musquitto.

  3. Hi Naveen, sorry it took so long but my guess is that you only have a single dash in front of daemon. The typical linux/unix paradigm is that command parameters have a short and long version (or sometimes only one or the other), and the short version is usually 1 character and looks like >somecommand -d (single dash in front of the d). The long version is usually 2 dashes, like >somecommand –daemon. This is common when wanting to find out the version of an executable like >node -v and >node –version which both do the same thing for the nodejs executable. Hope this isn’t too late to be of help!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s