Ayce

Blog

Installing the Mosquitto MQTT Messaging Broker and Node RED Client on CentOS 7

(Part 1) Step 1 — Installing Mosquitto

 Log in with your non-root user and use the yum package manager to install the epel-release package.

$ sudo yum -y install epel-release

This adds the EPEL repository information to our system. The -y option automatically answers yes to a few prompts throughout the process. Now we can install the mosquitto package.

$ sudo yum -y install epel-release

Step 2 — Configure Mosquitto

 Edit  the  /etc/mosquitto/mosquitto.conf

We will have persistent storage for client subscribing to the queues. It sounds good to activate client expiration to ensure we won’t have unlimited persistence.

 persistent_client_expiration 15d

 The messages are firstly stored in memory then saved on disk after the autosave interval second. The default value is 30 minutes (1800s) and it create a risk of large loss. A reduction to 1-minute sound safer.

autosave_interval 60persistence truepersistence_file mosquitto.dbpersistence_location /var/lib/mosquitto/

Create the directory for persistence DB if not yet existing

mkdir /var/lib/mosquitto/chown mosquitto:mosquitto /var/lib/mosquitto/

Step 3 — Start Mosquitto

 $ service mosquitto start

$ systemctl enable mosquitto

 

Topics are labels that you publish messages to and subscribe to. They are arranged as a hierarchy, so you could have sensors/outside/temp and sensors/outside/humidity, for example. How you arrange topics is up to you and your needs. Throughout this tutorial we will use a simple test topic to test our configuration changes.

Log in to your server a second time, so you have two terminals side-by-side. In the new terminal, use mosquitto_sub to subscribe to the test topic:

$ mosquitto_sub -h localhost -t test

-h is used to specify the hostname of the MQTT server, and -t is the topic name. You’ll see no output after hitting ENTER because mosquitto_sub is waiting for messages to arrive. Switch back to your other terminal and publish a message:

$ mosquitto_pub -h localhost -t test -m “hello world”

The options for mosquitto_pub are the same as mosquitto_sub, though this time we use the additional -m option to specify our message. Hit ENTER, and you should see hello world pop up in the other terminal. You’ve sent your first MQTT message!

Enter CTRL+C in the second terminal to exit out of mosquitto_sub, but keep the connection to the server open.

 

(Part 2) Step 1 — Installing Node RED

Requirements:

  • JS (version 10 or above)
  • NPM (version 5 or above)

 once you have centos installed you will need to make sure it has been updated.

$ sudo yum update -y

 

install Node.JS and npm

$ sudo yum install nodejs

install Node-Red

$ sudo npm install -g node-red

 

Step 2 — Starting as Service

Now let’s set it up as a service so that it will automatically start

sudo vi /etc/systemd/system/node-red.service

[Unit]Description=Node-REDAfter=network.target [Service]Type=simpleExecStart=/usr/local/bin/node-red-pi –max-old-space-size=128 -vRestart=on-failureKillSignal=SIGINT WorkingDirectory=/home/amolUser=amol [Install]WantedBy=multi-user.target

 

Save that and enable it….

$ sudo systemctl daemon-reload$ sudo systemctl enable node-red.service$ sudo systemctl start node-red.service$ sudo systemctl status node-red.service

 

Save Now you can access the Node-Red on localhost:1880 as 1880 is the default port for

Node-red.