Setting up your own local, private and secure ‘Internet of Things’ (IoT) server is easy with Mosquitto, Node Red and a Raspberry Pi. Sometimes this route is preferable to a cloud-based IoT solution. Because ‘the cloud‘ is just someone else’s computer, and you never know where your data will end up or when they will alter or terminate their services.
In this tutorial we will set up a basic server/broker using a Raspberry Pi (any model), Mosquitto and Node Red.
Mosquitto MQTT broker
MQTT (Message Queue Telemetry Transport) is a machine-to-machine messaging protocol for use on top of the TCP/IP protocol. It is designed to provide lightweight publish/subscribe communication to “Internet of Things” devices. It is commonly used for geo-tracking fleets of vehicles, home automation, sensor networks, and utility-scale data collection.
Mosquitto is a popular MQTT server (‘broker’) that has great community support and is easy to install and configure.
Node-RED is a programming tool developed by IBM for wiring together hardware devices, APIs and online services. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes that can be deployed to its runtime in a single click.
Preparing the Raspberry Pi
Download Raspbian Lite and write the image to your SD card. Install the SD card in the Raspberry Pi and boot the system. Log in to your Pi locally with the username ‘pi‘ and password ‘raspberry‘.
Start the configuration tool ‘raspi-config’: sudo raspi-config
Change the username and password
Change the hostname
Go to ‘interfacing options’ > ‘SSH’ and enable SSH
Enabe I2C and SPI if you want to use the GPIO pins
Exit the configuration tool and reboot the Pi: sudo reboot
After rebooting, you can log in remotely using SSH and update the Raspbian package lists: sudo apt-get update
Installing the Mosquitto MQTT broker
Download and install the required packages: sudo apt-get install mosquitto mosquitto-clients
The MQTT broker is now operating on your Pi’s TCP port 1883.
By default, Mosquitto will allow anonymous access. We will now set Mosquitto up to require usernames and passwords from clients before they can connect. Use the utility mosquitto_passwd to generate the passwords file /etc/mosquitto/passwd: