Publishing the line to an MQTT broker

The program in the simulator's PLC panel keeps running here, and the simulator publishes its variables to an MQTT broker, one topic each. A dashboard, a historian or any MQTT client subscribes to watch the line, and publishes to press its buttons.

In this guide a client publishes one message, a belt carries a box to a sensor, and the program stops it. No PLC is needed, and it takes about ten minutes.

You need

1. Build the scene and wire it to the program

Start a new scene and place three parts from the library:

  1. A Belt Conveyor.
  2. A Box, sitting on the belt near the end it starts from.
  3. A Photo Eye beside the belt, about two thirds of the way along, facing across it.

Select each device, find the Comms group in the Inspector, and type the program variable it connects to:

PartFieldType inMeaning
Photo EyePresence TagMain.EyeTrue while the sensor sees the box.
Belt ConveyorRun TagMain.RunThe belt runs while it is true.
Belt ConveyorSpeed TagMain.SpeedThe belt's speed, a setpoint a client can change.
The Inspector's Comms group for the Belt Conveyor, with Speed Tag set to Main.Speed and Run Tag set to Main.Run.
The Belt Conveyor's Comms group, wired to the program.

These are names, not I/O addresses. The parts talk to the program running in the simulator, and the broker carries the program's variables. Nothing here travels over a fieldbus.

2. Write the program

Open the PLC panel, replace whatever is in its code editor with this, and check the panel reports no errors:

PROGRAM Main
VAR
  Start : BOOL;
  Eye : BOOL;
  Run : BOOL;
  Speed : REAL := 0.5;
END_VAR

Run := Start AND NOT Eye;
END_PROGRAM

The belt runs once Start is true and stops while the sensor sees the box. Nothing in the scene sets Start. A message from the broker does.

3. Start the broker

Install Mosquitto and start it. With no configuration file it listens on this computer only, on port 1883, and takes any client without a login, which is all this guide needs:

mosquitto -v

4. Switch on the publisher

Open Simulation > Controller Properties… (or double-click the Controller node at the top of the PLC panel's organizer). Leave Runs on at This simulator, and in the MQTT publisher section:

  1. Broker: mqtt://127.0.0.1:1883 is the default, a broker on this computer. For one elsewhere, type its address.
  2. Topic: oip is the default. Everything is published under it, so two simulators on one broker each need their own.
  3. Tick Publish the program's variables. The simulator connects at once; you do not need to press Play.
The MQTT publisher section of Controller Properties: Publish the program's variables ticked, Broker mqtt://127.0.0.1:1883, Topic oip, and the status line saying it is connected.
The MQTT publisher section, connected.

The status line under it should read:

Connected to mqtt://127.0.0.1:1883: 4 variables under oip/tags, 3 writable through oip/set. Stopped: oip/status reads stopped until Play.

Like the other connection settings, these are saved on this computer, not in the scene.

5. Watch from another client

In a terminal, subscribe to everything under the topic:

mosquitto_sub -h 127.0.0.1 -t "oip/#" -v

It prints the program's four variables and the simulator's state straight away, before anything changes:

oip/tags/Main/Eye false
oip/tags/Main/Run false
oip/tags/Main/Speed 0.5
oip/tags/Main/Start false
oip/status stopped

The layout is the same for every program:

TopicWhat it carries
oip/tags/Main/StartThe variable Main.Start. Each dot in a name is a level, so a timer's outputs sit under the timer's name.
oip/set/Main/StartPublish here to write Main.Start, with the new value as the message.
oip/statusrunning while the program runs, stopped while it does not, and offline once the simulator is gone.

6. Run the line from the client

  1. Press Play in the simulator. The subscriber prints oip/status running, and the belt waits.
  2. In a second terminal, press Start:
    mosquitto_pub -h 127.0.0.1 -t oip/set/Main/Start -m true
    The belt runs, the box reaches the sensor, the subscriber prints oip/tags/Main/Eye true, and the belt stops.
  3. Optional: press Stop and Play to start again, publish 0.2 to oip/set/Main/Speed and then true to oip/set/Main/Start. The belt carries the box at the new speed.
The box has ridden the belt conveyor and stopped in front of the photo eye.
After the message on oip/set/Main/Start, the belt carried the box to the photo eye and stopped.

The PLC panel's watch table shows what the client wrote, and Controller Properties shows the last write, for example Last write from the broker: Main.Start := TRUE.

MQTT has no way to answer a message, so a write that is not taken is shown there too. Publish true to oip/set/Main/Eye and it reads Last write refused: Main.Eye: it is read-only (a constant, or an input the scene feeds).

Stop and Play keep the connection. On Stop the state reads stopped and the values keep what they last were; on Play it reads running again. The simulator stays connected for as long as the box is ticked, not for one run, because a dashboard stays subscribed while you edit and restart. Close the app and the broker publishes offline for it.

From a browser tab

A browser tab cannot open a plain MQTT connection, but it can open a WebSocket, and brokers carry MQTT over one. Give Mosquitto a second listener for it. Save this as oip.conf:

listener 1883 127.0.0.1
listener 9001 127.0.0.1
protocol websockets
allow_anonymous true

Start the broker with it (stop the one from step 3 first, since both want port 1883):

mosquitto -c oip.conf -v

In the tab, set Broker to ws://127.0.0.1:9001 and tick the box. Everything else in this guide is the same, and the command-line clients still use port 1883.

The first time, Chrome or Edge may ask whether this site may reach this computer or your local network. Allow it. Without that the browser refuses the connection, and the status line reads as if no broker answered.

A page served over https reaches a broker on another computer only over wss://, so that broker's WebSocket listener needs a certificate. The desktop app takes ws:// and wss:// addresses as well.

A broker with a login

For a broker that wants one, fill in User name and Password in the same section. The user name is saved on this computer. The password is kept until the app closes and never saved, so it is typed again after a restart.

Over mqtt:// and ws:// nothing is encrypted, the password included. Use those on this computer or a test bench, and wss:// anywhere else.

Troubleshooting

"Not connected to ...: nothing is listening on ..."
The broker is not running, or the address or port is wrong. The publisher keeps trying every few seconds, so start the broker and it connects by itself.
"a browser tab cannot open a plain mqtt:// connection"
You are in a browser tab with an mqtt:// address. Use the broker's WebSocket address, as in From a browser tab, or the desktop app.
"no MQTT broker answered at ws://..."
The port is not a WebSocket listener. A broker's plain MQTT port (1883) does not take WebSocket connections: it needs a listener of its own, such as 9001 above. It also reads this way when the browser did not let the page reach this computer: check the site's permissions for local network access.
"the broker refused the user name or password", or "the broker does not allow this client"
The broker wants a login. Fill in User name and Password. A refused login is not tried again until one of them changes.
A message on a set topic does nothing.
Controller Properties says why under Last write refused: the program is not running (press Play), the variable is read only, the name is not one the program has, or the message is not a value of its type (a BOOL takes true, false, 1 or 0).
The subscriber shows values, but they never change.
Read oip/status. On stopped, press Play. On offline, the simulator is not connected and these are the values it left behind.
The variables are missing, or the status line says "no variables yet".
The program has not compiled. Fix the errors the PLC panel shows.