This tutorial demonstrates messaging using MQTT
with broker software managing the exchange.
Clients publish and subscribe to the same topic
Broker and client software can be on one or multiple computers
MQTT can exchange data on internet, cloud based networks.
In this tutorial everything is on a local network without connection to the internet
To duplicate this setup you will need
the ability to use Machine Expert software,
manage MQTT broker and client software,
and set up a local area network.
Skills learned include how to use the examples
included with Machine Expert software
how to convert the controller hardware if it initially doesn't match
how to use the visualizations for operator interface
and the basics of messaging with MQTT.
The techniques covered can be useful
for machine data exchange both locally
and remotely including connectivity over the internet.
This tutorial will use simple, manually entered messages.
In machine automation it is common to exchange
live data points and controller variables in real time.
To use the MQTT handling example
start a new project from example.
There are many great examples included with Machine Expert.
There are examples associated with different controllers in the list.
Or you can filter the list by example name.
Select the MQTT handling example.
Select a controller type from the list.
If the selected controller is not what you are using
Right click on "MyController".
A tools list will appear.
Select the "Convert device" tool.
Select the controller you are using.
Double-click or press OK.
The conversion takes a few moments...
Go to the "Applications Tree"
Go to GVL (Global Variables)
Set xUseTlsIfPossible constant to FALSE.
This will disable Transport Layer Security (TLS)
Normally, TLS should be enabled for machines with network connection.
Transport Layer Security uses certification for cybersecurity to help prevent hacking.
Cybersecurity is an important protection.
So, please consider the network environment in use.
TLS is disabled for this tutorial to focus on MQTT messaging
The controller is not running a machine.
So, cybersecurity is a very low risk in this example.
The example project includes user visualizations
for viewing and operating the example.
Notice the examples in the "Tools Tree".
The "Main" visu is the one we will use.
OK. Let's get connected to the PLC.
In the "Device Tree" double-click "MyController"
Then connect to your PLC using the settings
that match your cables and connection type.
Wait for the connection.
Then run the controller.
Go to the visualization.
Now, open up a 3rd party MQTT client for testing.
Open a publish window with a topic. (Machine Example)
Add a "Subscribe" window with the same topic. (Machine Example)
Subscribe.
Publish a test message. (Test message from PC)
Notice the message appear in the subscribe window.
The test works. Go back to the PLC in Machine Expert.
In the visualization set the IP address to match the broker software.
Then connect.
This starts the MQTT service in the PLC.
Now, subscribe to the same topic (Machine Example)
Go back to the MQTT client software.
Publish a new message. (Test message 2 from PC.)
Return to the visualization of the PLC in Machine Expert.
Click on "Get messages" and notice the message. Good.
Go to the "Publish" tab.
Enter the topic (Machine Example)
Notice the Data fields.
I'm simplifying the message.
In a running machine the data fields
could be live status from the PLC.
Press "Publish"
Go back to the "Subscribe" tab.
Get messages.
Go back to the MQTT Broker software to see the message.