Skip to content

Managing the MQTT to MongoDB passthrough servers

Will Richards edited this page Sep 1, 2023 · 4 revisions

Author: Will Richards

Accessing the Server

To gain access to manage the pass through servers please talk to Chet Udell about getting an SSH user on the server. (FOR OPEnS Only)

General Server Configuration

On the server there are two main programs that we always want running to be able to service connections at any point. These two programs are Mosquittoand Node-RED. Mosquitto brokers the MQTT connections from the devices themselves while Node-RED handles the pass through from the MQTT broker to the MongoDB database for long-term storage.

As such both the Mosquitto broker and the Nod-RED instance must be running 24/7.

Mosquitto Config

This config is more or less just standard configuration for an unsecured, authenticated MQTT broker.

# Configure remote listener on any network interface
listener 1883 0.0.0.0

# Disable anonymous login
allow_anonymous false

# Select a password file to retain the credentials
password_file /etc/mosquitto/passfile.txt

To generate a password file follow this tutorial

Node-RED Config

To configure node-red a configuration will need to be downloaded from here. Pasted image 20230901064011 Setup the connections for MQTT and MongoDB to route to your specific server

Starting the Services (On CentOS)

Node-RED runs persistently using pm2 while Mosquitto is installed as a systemd service to start the Mosquitto services run

Starting under current user

Start Mosquitto Service
sudo systemctl start mosquitto.service
Start node-red
pm2 start /usr/local/bin/node-red

After you have started node-red you want to run

pm2 save

Doing so allows you to generate the startup command we need

Start at System Boot

Enable Mosquitto service
sudo systemctl enable mosquitto.service
Enable Node-RED
pm2 startup
<RUN THE COMMAND IT GIVE YOU>

Next you need to edit the file /etc/systemd/system/pm2-username.service

4: After=network.target
->
4: After=network.target mosquitto.service
And now it should run on boot!