Skip to content

MQTT broker

Thomas T. Jarløv edited this page May 23, 2019 · 4 revisions

NimHA depends on a MQTT broker, which connects all the different modules. The current setup requires Mosquitto. In the future Mosquitto will be replaced by a Nim MQTT broker.

Mosquitto

Mosquitto is a message broker that implements the MQTT protocol versions 3.1 and 3.1.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.

Install Mosquitto

Install using your package manager (sudo apt install mosquitto, sudo pacman -S mosquitto, etc.).

Add a password for connecting to the broker

NimHA uses 3 different clients for connecting to the broker. You have to add all 3 users and it is required, that they use the same password.

Usernames:

  • nimha
  • nimhagate
  • nimhawss

If you are going to use Owntracks, you can already now add another user for this. The password for this user should not be the same as the ones above. Call the user owuser. See the section below to Owntracks.

Navigate to the Mosquitto folder

cd /etc/mosquitto

Open the password file

sudo nano passwd

Add users

Insert the usernames and your password (in clear text). The format needs to be username:password.

nimha:exampePassword
nimhagate:exampePassword
nimhawss:exampePassword

Run

Passwords are stored in a similar format to crypt.

sudo mosquitto_passwd -U passwd

Setup configuration

Open the configuration file

sudo nano /etc/mosquitto/mosquitto.conf

Insert the data at the bottom

password_file /etc/mosquitto/passwd   
allow_anonymous false
port 1883 localhost
listener 8883

Config should look like this

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true

include_dir /etc/mosquitto/conf.d

password_file /etc/mosquitto/passwd
allow_anonymous false
port 1883 localhost
listener 8883

Autorun Mosquitto

Make sure that Mosquitto startup on boot:

sudo systemctl enable mosquitto
sudo systemctl daemon-reload
sudo systemctl start mosquitto

Owntracks

You need to open your firewall and/or forward you routers port to allow outside connections. This is required, if you are going to use Owntrack.

Test the MQTT

Listen

mosquitto_sub -d -t "#" -u USERNAME -P PASSWORD -h 192.168.1.160 -p 1883

Send

mosquitto_pub -d -t TOPIC -m MESSAGE -u USERNAME -P PASSWORD -h 192.168.1.160 -p 1883

Extra (not relevant)

Add another user

-b does not to work ?? using hack:

sudo mosquitto_passwd -c tmppwdfile username
# enter passwd
# copy content of tmppwdfile
# insert into real passwd file