MQTT, Raspberry Pi with Nodejs

James Best
Salted Bytes
Published in
4 min readFeb 11, 2018

Note: (07/12/18) Updated the node version.

If you have spent any time building things for the IoT then you will have likely heard of MQTT. You will also have likely heard of the Raspberry Pi. The credit card (and smaller) size computer that shock up the world of makers all around the globe.

In this post, I am gonna run through how to set up an MQTT broker on a Raspberry Pi. The broker is written in Nodejs.

First, let’s talk about what the MQTT protocol actual is. Below is a copy pasta from the mqtt.org website as they will be able to more succinctly describe exactly what mqtt is.

MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging “machine-to-machine” (M2M) or “Internet of Things” world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.

The key bits to take from this are that it is extremely lightweight and fault tolerant.

Why I am choosing to run this in Node. Well, no other reason then I like writing javascript. This package has no advantages that I have come across over the very popular Mosquitto package that seems to be available on every platform.

The first step into MQTT heaven is to get your Raspberry Pi ready to go. I prefer to work in the terminal and so tend to set my Raspberry Pi’s up in a headless fashion. This is how we will be setting things up. Please remember that you can do exactly the same things mentioned through the Raspbian desktop by running the commands in a terminal. But because we will only be ssh’ing into the server there is no need to install the full version of Raspbian. Download the lite version and burn it to your micro sd card. I use Etcher for all my image burning needs. It is simple and works flawlessly.

Once you have burnt the image to the card we need to let the pi know that we intend to use ssh so, before we put the card in the pi you will need to drop a blank file named ssh on to the root of the boot directory. This tells the pi to enable ssh straight away. Once the pi has booted for the first time and enabled ssh it will delete this file.

On a mac:

$ cd /Volumes/boot
$ touch ssh

Now that we have a working raspberry pi, booted and waiting for us to start tinkering. We need to access the pi. As I mentioned we will be logging in to the pi via ssh. I use a Mac and so the following instructions will be for Mac, although they will likely work most Linux distributions (sorry Windows users. Check out putty for ssh).

To be able to ssh into the pi we need its IP address. This can be found, either by plugging in a screen to the pi and watching the terminal output or using an app. I use Fing. It is available on all mobile platforms and is really good for this sort of thing. Below you can see it in action

Open a new terminal window and type:

$ ssh pi@192.168.1.1

The default user for the pi is pi and the password is raspberry. Be sure to change that password and user if you intend to have this installed in your home for any amount of time.

The next thing we need is Nodejs. We can just install the node version from the website as it not compiled for the arm architecture the raspberry pi runs on so enter the following commands. This will download the node package, extract it and move it into your PATH, making it accessible anywhere.

$ https://nodejs.org/dist/latest-v10.x/node-v10.14.1-linux-armv7l.tar.xz
$ tar -xvf node-v8.9.1-linux-armv7l.tar.xz
$ cd node-v8.9.1-linux-armv7l
$ sudo cp -R * /usr/local/

Let check it has installed correctly.

$ node -v
$ npm -v
$ sudo npm install npm@latest -g

The last command above updates to the latest version of npm.

We have to install Raspian and Node, now all we need to do is install the broker itself. You can think of the broker as an old fashion telephone exchange. All the data is published to it and is then pushed to any subscribers.

We are using Mosca as our broker. There are a number of ways the Mosca can be integrated into a system. It can be integrated directly into an application or used as a standalone broker. We are doing the latter.

To get Mosca running we need to install it.

sudo npm install mosca bunyan -g

This installs the Mosca and Bunyan. Bunyan is a JSON logging library giving us nicely formatted logging in the terminal.

mosca -v | bunyan

We are starting Mosca passing in the -v flag. This gives us verbose logging and then piping the output to Bunyan to pretty print it for us. We now have a working MQTT broker.

To make the broker more fault tolerant still, use something like forever(https://www.npmjs.com/package/forever) to make sure the broker stays up and running. This will require, moving your Mosca settings into a script that forever can attach itself too. Check out both these packages documentation for the information you need.

MQTT is an amazing protocol for the IoT and the Raspberry Pi is the perfect host.

--

--

James Best
Salted Bytes

Tech and photography loving developer @candideTech. A host on the @salted-bytes