The MQTT protocol


IoT and the MQTT protocol

Emulating Internet of Things devices

IoT devices in Simumatik can be any component that uses the MQTT communication driver. In this course, we will take a look at a simple home automation setup and make some modifications.

Install an MQTT broker

The first step is to install Mosquitto. Mosquitto is an MQTT broker that will be handling the communication between all of our IoT devices. After installation, Mosquitto will be running in the background as a service.

Emulate IoT devices in Simumatik

Now open the system “Showcase: MQTT” in Simumatik. This system includes a couple of IoT enabled devices. The sensor is set up to communicate with the lamp, and the wall switch communicates with one of the sockets (to which the fan is connected).

Let’s start by testing the system to make sure everything works. Remember to connect the gateway, this allows the components in Simumatik to send messages to the MQTT broker we just installed.

Now start the emulation and drop a product from the product_entry, the IoT_Lamp should light up (yellow). Then try pressing the switch, the fan should turn on.

The devices in this system are communicating by publishing and subscribing to topics. Right now the setup is very simple. The IoT_Lamp subscribes directly to the topic that the IoT_Sensor is publishing to, and the same thing between the IoT_Wall_switch and the IoT_Socket. 

In the following chapter, we will take a look at how these topics can be modified to give us more control of the system.

Creating a control script

Now we are going to modify the system so that there are two switches controlling the lamp, and the fan should be turned on when the sensor is off and vice versa. This means that the switch and sensor can’t publish directly to the same topics that the lamp and socket are listening to, we need a control script in between.

Let’s start by changing the topic of the switch. Click on the switch to see its public variables, and change the topic to “switch_1”.

Now we can duplicate the switch. This is done from the context menu of the switch in the assembly panel.

Make sure to change the position of the new switch a little bit, and change the topic to “switch_2”. Also, connect the new switch to the ports from the main socket to give it power.

The next thing we need to do is change the topic of the sensor to be “sensor”. This way we can subscribe to the “sensor” topic from our control script and switch the status of the socket (to which the fan is connected) by publishing to the “power” topic.

Now that we have all the topics configured, it’s time to create and run the control script. You can download a script to start from by pressing¬†this link. Before running the script you will need to have python installed, and run the following command in the terminal:

					 py -m pip install paho-mqtt

After installing paho-mqtt, you can start running the script by entering the following command in a command-line (in the same directory as the previously downloaded file):


Now that the script is running, start the emulation in Simumatik again. Both switches should now be controlling the lamp, and the fan should stop when the sensor detects a product.

Using a smartphone to control the system

By just changing a couple of settings in the Mosquitto config file (mosquitto.conf, located in the Mosquitto installation directory), it’s possible to access the broker from other devices in the network. A simple way to get started is to uncomment `listener 1883` and also `allow_anonymous true`. Use the search functionality in your text editor to find the lines with those texts, and remove the hashtag at the start of those lines.

					listener 1883
allow_anonymous true

After changing the configuration, restart the mosquitto service to apply the changes. Then, download an app that can connect to mqtt brokers (“MQTT Dash” is a popular Android app, but any similar app will do the trick). In the app, configure a client to talk to the address of you pc with the Mosquitto broker (you can find the ip by running `ipconfig` in the command prompt).

Here is an example of how the final setup can look (the black screen to the left is the smartphone).