Documentation search search close
CONTENTS

    Open this example in MATLAB

v10.0.1.x for R2026a
View other versions

MQTT Loopback

MQTT is an Ethernet communication protocol based on a publish-subscribe architecture. Clients connect with the broker and publish data for specific topics. The broker manages all the topics and passes the data to the other clients that subscribe to these topics.
This example demonstrates the communication between one MQTT Client running on the Speedgoat real-time target machine and an external MQTT broker device.

Setup

Prerequisites

You will require the following to run this example:
  • Speedgoat real-time target machine with at least one unused Ethernet interface
  • "Runtime License File" that includes one MQTT Client license. Refer to the Runtime Licensing documentation for more information
  • An external MQTT broker device listening on IP address 192.168.10.50 TCP port 1883
Eclipse Mosquitto™ is an open source MQTT broker supporting different operating systems. The binary package for Windows is ready to use as a broker for the present example. Install and start the broker on a separate Windows computer by following the steps below:
  1. Download the Mosquitto MQTT broker from https://mosquitto.org/download
  2. Install the application to a folder that provides the needed write permission, C:\Program Files\mosquitto, for example
  3. Copy the configuration file MQTT_Broker.conf to a folder that provides the needed write permission, C:\Data\MQTT_Broker.conf, for example
  4. Open the Network & Security settings
  5. Under Advanced network settings click Change adapter options
  6. Double-click the adapter that you want to use for MQTT communication
  7. In the Networking tab, select Internet Protocol Version 4 (TCP/IPv4) and click Properties
  8. Select Use the following IP address and Use the following DNS server addresses
  9. Enter 192.168.10.50 and 255.255.255.0 in the fields IP address and Subnet mask
  10. Leave the other fields empty
  11. Confirm and close all opened dialogs in the Network & Security settings
  12. Open the Command Prompt app
  13. Navigate to the mosquitto installation folder by calling cd C:\Program Files\mosquitto
  14. Start the broker by calling mosquitto -c C:\Data\MQTT_Broker.conf -v
  15. The output of a properly started broker looks like this
mqtt_loopback_cmd.png

Wiring of Target Machine

Use a CAT5 cable or newer to connect the target machine's Ethernet interface with the Ethernet adapter of the broker.

Ethernet Configuration of the Client and Servers

Use the Speedgoat Ethernet Configuration Tool to assign the client's IP address to one unused Ethernet interface (onboard or IO71x/IO79x) on the target machine.
The IP address assigned should be 192.168.10.1. The netmask for this local address is 255.255.255.0.
ethernet_configuration_tool_comm_protocols.png
% Configure the Ethernet interface for the MQTT example
speedgoat.configureEthernet; % Open the Speedgoat Ethernet Configuration tool

Initialize and Open the Simulink Model

% Open Simulink model
modelName = 'sgMdl_MQTT_Loopback';
open_system(modelName);

Model Description

The model sets up the communication for the MQTT Client and the remote MQTT Broker. The Station ID and Connection ID parameters are used to build up an internal relationship of the Setup, Connection, Publish and Subscribe blocks in the model. Both the Station ID and Connection ID are 0 as the model simulates only one MQTT client connected to one MQTT Broker. The MQTT client is represented to other devices in the network by the Client Identifier parameter in the Setup block.
Every 10 milliseconds, the client publishes data to the broker who passes this data back to the client as the client acts as a subscriber as well. The data is structured as follows:
  • Counter signal of type uint32 for topic 'position', updated every 0.02 seconds
  • Sine wave of type double for topic 'speed', updated every 0.01 seconds

Build, Download, and Run the Example

To run the example, either run the following code section or click the Run on Target button in the REAL-TIME tab in the Simulink model.
% Build the Simulink model
slbuild(modelName); % this will create the real-time application file (.mldatx)
 
% Create and connect to the Speedgoat real-time target machine
tg = slrealtime;
tg.connect;
 
% Download the real-time application to the target machine
tg.load(modelName);
 
% Set the stop time
tg.setStopTime(20); % seconds
 
% Prepare the Simulink Data Inspector to plot signals
Simulink.sdi.clearAllSubPlots;
Simulink.sdi.setSubPlotLayout(2,1);
Simulink.sdi.view;
 
% Start the real-time application
tg.start;
 
% Select the signals for plotting, set the axis limits and line colors
sdiRunIDs = Simulink.sdi.getAllRunIDs;
sdiLatestRun = Simulink.sdi.Run.getLatest;
 
s = sdiLatestRun.getSignalsByName('Position');
plotOnSubPlot(s,1,1,true);
s = sdiLatestRun.getSignalsByName('Speed');
plotOnSubPlot(s,2,1,true);
 
Simulink.sdi.setSubplotLimits(1,1,'AllRange',[0,20,-100,20100]);
Simulink.sdi.setSubplotLimits(2,1,'AllRange',[0,20,-110,110]);
The output is then visible in the Simulink Data Inspector (SDI). You should see the 4 signals the first server receives from the client and the 4 signals the client receives from the second server.
mqtt_loopback_plot.png

Restore Target Machine Configuration

To restore the default configuration, you can simply reopen the Speedgoat Ethernet Configuration Tool and tick the Show Advanced Settings checkbox. Then click the Restore button in the bottom right.
This will erase the IPs of every Ethernet interface and set the host link IP to 192.168.7.5 and the netmask to 255.255.255.0.
We recommend restoring the default settings to reduce possible configuration errors.

Additional References