Home Brewing and Tech Gizmos

Hand Crafted Beer with a touch of IoT

Installing Mosquitto MQTT Server

In order to experiment with more integrations of my brewing system I am setting up a Mosquitto MQTT server which will provide the central infrastructure for more automation and information flow. Besides being used for brewing related things it is also an excellent backbone for use in home automation.

I have an old mid-2011 Mac Mini running various automation tasks already so this is a good candidate for also running the MQTT server since it really shouldn’t require that much of power. Fortunately it is able to run MacOS 10.13 High Sierra which is the minimum recommended version by the Homebrew package manager.

Installing Homebrew

The easiest, and also the recommended way to install Mosquitto is by using Homebrew. Homebrew is a package manager for macOS and has as such nothing to do with home brewing except for the naming of artefacts such as bottles, kegs, taps, etc.

To install Homebrew, simply run the following from the command line:

$ /bin/bash -c "$(curl -fsSL \
https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Installing Mosquitto

Once the Homebrew package manager is installed, it can be used for installing the Mosquitto software package from the command line:

$ brew install mosquitto

This will install the Mosquitto server and all the dependencies. If everything goes well the last couple of lines in the install log provides information about where the configuration file is installed and how to start mosquitto:

==> mosquitto
mosquitto has been installed with a default configuration file.
You can make changes to the configuration by editing:
  /usr/local/etc/mosquitto/mosquitto.conf
To have launchd start mosquitto now and restart at login:
  brew services start mosquitto
Or, if you don't want/need a background service you can just run:
  mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf

Generally the configuration file can be left as is, but there are a few recommended updates. First, since it will be running automatically upon boot, the pidfile setting is recommended to be updated. Additionally basic authentication using user+password is also a good idea for minimum security:

$ nano /usr/local/etc/mosquitto/mosquitto.conf
...
#pidfile
pidfile /var/run/mosquitto.pid
...
#password_file
password_file /usr/local/etc/mosquitto/users.passwd

The server should generally also be secured using SSL, but since it’s only deployed on a home network behind a firewall I’ll leave the security settings a bit relaxed for now. The password file can be created using the following command to enable the mqtt_user to login. Leave the -c option out for adding additional users to the same file.

$ mosquitto_passwd -c /usr/local/etc/mosquitto/users.passwd mqtt_user
> Password:
> Reenter password:

The server can now be started with the following command which will also start it automatically upon boot:

$ brew services start mosquitto
...
==> Successfully started mosquitto (label: homebrew.mxcl.mosquitto)

The last step missing is to verify that the server is actually running. This can be done by opening a new terminal window and subscribing to a test topic. First as anonymous to verify the security setting, and then retrying as mqtt_user:

$ mosquitto_sub -t test/status
> Connection error: Connection Refused: not authorised.
$ mosquitto_sub -u mqtt_user -P <password> -t test/status

And in another terminal window, publish a message to the same topic:

$ mosquitto_pub -u mqtt_user -P <password> -t test/status -m "Hello World"

If everything is working as expected, the published message shows up in the subscriber.

The Mosquitto MQTT server is now installed and ready for integration with various MQTT enabled devices and service. Hope you found the information useful. Please provide any comments or suggestions below.

Leave a Reply

Your email address will not be published. Required fields are marked *