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 Raspberry Pi.
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 command
$ sudo apt install mosquitto mosquitto-clients
which will install both the Broker software and the client software. Check the installation with
$ mosquitto -v
1581877602: mosquitto version 1.5.7 starting
1581877602: Using default config.
1581877602: Opening ipv4 listen socket on port 1883.
1581877602: Error: Address already in use
1581877602: mosquitto version 1.5.7 starting
1581877602: Using default config.
1581877602: Opening ipv4 listen socket on port 1883.
1581877602: 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
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.
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 recieved 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.
No 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 !