MQTT is an extremely simple and lightweight messaging protocol.
This messaging protocol is useful integrating multiple data platforms, with very little
development time and cost.
Messaging in MQTT is based upon Topics that can either be Subscribed or Published to from
a central repository.
This central repository topics is called a Broker.
The devices Subscribing or Publishing to this broker are called Clients.
Each client can publish or subscribe to one or more of the topics on the Broker.
First you must have a BRX CPU that has version 2.3 software and firmware.
The BRX CPU can only work as a client.
You must have already setup a MQTT Broker to handle the messaging.
There are plenty of Brokers out there and there is too many to cover in this video.
Please see MQTT.org for more information on this topic.
Some of the limitations on Do-more BRX, when using MQTT, is that the BRX does not support
SSL or any encryption method for MQTT.
Also, the BRX CPU does not allow for wildcards when selecting topics.
You must have the exact name of the topic you are trying to publish or subscribe to.
Once you have your broker setup and know its name or IP address, I would advise first testing
the connection.
I am using a free MQTT client called MQTTfx.
I select here and create a new connection.
I will be using the direct IP address of the Broker, as I am on an isolated network.
Then Select Connect.
Once you are connected you will see a green light here.
To verify your connection, go to the Broker status and select subscribe.
Now we know we have a good connection let’s publish our first topic to the broker that
then can be subscribed or read to, from the BRX CPU.
I am selecting that this topic to be retained by the broker.
If you do not select this option this Topic will go away from the topic list in the broker
after a period of time defined by the broker.
Now select Publish.
Now that we have the initials setup, let’s dive into the BRX CPU setup.
Once you have your project open, select the system configuration and open the Device configuration.
Select new device.
Then the MQTT Client option.
Give this device a name.
This is what you will use in all the Publish and subscribe instructions, set it up one
time and then just reuse the client name.
If your Broker is on a device that is using a naming convention, place that here, if you
are using this option, make sure you setup the DNS server on your BRX CPU.
Since I am just using a direct IP address, I will enter that here.
The standard Server port for MQTT is 1883, consult your broker information, to ensure
that this number is correct.
I will use the default Timeout setting.
If your Broker requires username and password place that here.
This last will option here is used to notify that there has been a disconnect of this client.
This is a topic that will be maintained at the Broker, and not published unless the Client
has dropped connection.
Now that this is setup go ahead and select Ok, and Ok once again to exit the system configuration.
There is a very powerful tool in Do-more that I would like to highlight called Do-more logger.
This logger if enabled and you have the logger open, the CPU will dump diagnostic data to
this logger.
This is extremely helpful in trouble shooting email or MQTT messaging.
To enable messaging dumping to the logger a bit called ST36 must be turned on.
I am going to trigger this bit, at the top of my ladder, by just putting an always on
bit in front of it.
Now I am going to Subscribe to the Topic we sent, earlier in this video.
I will place a triggering contact for my ladder code and then I am going to add a MQTT subscribe
instruction.
We select the MQTT client.
This option can be used to reduce some of the topic name length.
Now, down here you can subscribe to up to 50 Topics per instruction.
Now, I am going to setup our TEST topic.
Since I am using text here, I have to place this inside of quotation marks, if you want
to use a string tag here, just exclude the quotation marks, and enter in your string
tag.
I am placing the exact text from our earlier message, make sure to verify this, if this
is not correct you will not get your message, and then I am going to select the Topic payload
to be written to SS0.
Accept the instruction and write this project to the CPU.
Now, I am going to open up a Data Monitor and enable this instruction.
As we can see now the payload from our topic, was downloaded to the string tag.
Now let’s open up the logger from the debug menu and see what this type of communication
looks like.
That handles the subscribe from the BRX CPU.
Now, let’s Publish a message to the broker.
I am going to place a triggering contact, and then add in the MQTT Publish instruction.
These are the options for enabling the Publish.
I will stay with the default of publishing, this Topic, every 1 second.
This instruction can publish up to 50 topics in 1 instruction.
To start a topic, select here.
Once again, I am using text here so it must be contained inside of quotation marks.
If you want to use a tag exclude the quotation marks.
The message I will be posting to this topic will be contained in the string tag SS1.
Selecting retain will force the topic to be maintained by the broker.
These options determine the publish interval.
Now accept the instruction and write that to the CPU.
I am going to add the enabling contact and the string tag to our Data view.
I am going to write some text into the string tag, and now enable the instruction.
Let’s go back into our PC based client.
Enter in the exact text from the publish instruction and then select to subscribe.
And that is it, we have a published and subscribed a topic, from a BRX CPU.
Now, let’s go back to the designer software one more time, so you see what a successful
communication looks like in the Do-more logger.
If you have any question on this video please feel free to comment below or call into our
tech support team.
Please follow this link to see more videos on the Do-more platform and select here to
subscribe to our YouTube channel for upcoming news and solutions.