Node-Red & Mosquitto quick install, Linux, Ubuntu, Raspberry OS

A fantastic way to distribute data between devices is using MQTT. In this post I will do a quick and simple setup with Node-Red(NR) and the Mosquitto(MQ) broker on a Linux machine.
Very short, the Broker, installed on a server, handles incoming Publishing and Subscriptions from clients. Every published Message is "linked" to a Topic. All Subscribers use a Topic to "link" to the right Message.
How to install Node-Red(NR) is found here and the MQ manual here.
Install Mosquitto
In a terminal/ssh session, issue the commands
sudo apt update && sudo apt install mosquitto mosquitto-clients
which will install both the Broker software and the client software. Check the installation with
(Disable the service with "sudo systemctl stop mosquitto && sudo systemctl disable mosquitto")
Manual test of Mosquitto
Local host
Very short, the Broker, installed on a server, handles incoming Publishing and Subscriptions from clients. Every published Message is "linked" to a Topic. All Subscribers use a Topic to "link" to the right Message.
How to install Node-Red(NR) is found here and the MQ manual here.
Install Mosquitto
In a terminal/ssh session, issue the commands
sudo apt update && sudo apt install mosquitto mosquitto-clients
which will install both the Broker software and the client software. Check the installation with
mosquitto -v
1757430955: mosquitto version 2.0.18 starting
1757430955: Using default config.
1757430955: Starting in local only mode. Connections will only be possible from clients running on this machine.
1757430955: Create a configuration file which defines a listener to allow remote access.
1757430955: For more details see https://mosquitto.org/documentation/authentication-methods/
1757430955: Opening ipv4 listen socket on port 1883.
1757430955: Error: Address already in use
1757430955: Opening ipv6 listen socket on port 1883.
1757430955: Error: Address already in use
The error means that the broker is already in use. Enable the systemd service with (Will autostart the MQ at power on and restart at failure)sudo systemctl enable mosquitto && sudo systemctl start mosquitto
and could be checked with
sudo systemctl status mosquitto(Disable the service with "sudo systemctl stop mosquitto && sudo systemctl disable mosquitto")
Manual test of Mosquitto
Local host
Before You build an application with NR You could test MQ from the command line. Start a Subscriber with Topic "test/message"
mosquitto_sub -h localhost -t "test/message"
This will lock the screen ....
Start a new terminal/ssh session and issue the command
mosquitto_pub -h localhost -t "test/message" -m "Hello, world"
which will Publish the Topic "test/message" with the Message "Hello, world" and You will se the message appear in the Subscription session. End the Subscription session with Ctrl + c.
Remote host
mosquitto_sub -h localhost -t "test/message"
This will lock the screen ....
Start a new terminal/ssh session and issue the command
mosquitto_pub -h localhost -t "test/message" -m "Hello, world"
which will Publish the Topic "test/message" with the Message "Hello, world" and You will se the message appear in the Subscription session. End the Subscription session with Ctrl + c.
Remote host
Allowing accessing from a remote client you must handle authentication. I started up with "Unauthenticated access" which is enabled via the "mosquitto.conf" file.
I copied the example file with "sudo cp /usr/share/doc/mosquitto/examples/mosquitto.conf /etc/mosquitto/conf.d/" and edited with "sudo nano /etc/mosquitto/conf.d/mosquitto.conf".
Below line "# General configuration" change from "#allow_anonymous" to "allow_anonymous true"
Below line "# Listeners" change from "#listener" to "listener 1883".
Restart with "sudo systemctl restart mosquitto". Install the clients on the remote computer with
sudo apt update && sudo apt install mosquitto-clients
and then use the above commands. Note that the sub command shall use the host with the broker.
Node-Red test of Mosquitto
In NR I built a test flow which every 5:th second will Publish the Topic "test/message" with a random number, 1-10, as Message. In the same flow I will Subscribe to the Topic "test/message" and display the received number in the debug sidebar tab.

Download the flow from here and check out the setup.
A more in depth analys and setup can be found here at Steve Copes site.
In NR I built a test flow which every 5:th second will Publish the Topic "test/message" with a random number, 1-10, as Message. In the same flow I will Subscribe to the Topic "test/message" and display the received number in the debug sidebar tab.

Download the flow from here and check out the setup.
A more in depth analys and setup can be found here at Steve Copes site.
Comments
Post a Comment
Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !