How does it work?
A device publishes data to a topic, for example factory/line1/temperature. The broker forwards it to everyone subscribed to that topic. The sender does not need to know who the receivers are.
- Topic: A name organised like a folder path.
+ is a single-level wildcard, # matches all lower levels. - QoS 0 / 1 / 2: The message is delivered at most once, at least once or exactly once.
- Retain: The broker keeps the last value; a new subscriber receives it immediately.
- LWT (Last Will): A message the broker publishes when a device disconnects unexpectedly, so you know the device is offline.
In industrial use, JSON or Sparkplug B is commonly chosen to standardise the data format.
Hardware architecture
Sensor / PLC / Devicedata source→IoT GatewayModbus → MQTT→NetworkEthernet, Wi-Fi, 4G→MQTT Brokerlocal server or cloud→SCADA / Applicationssubscribers
- Devices: Devices with native MQTT, or IoT gateways that convert Modbus devices to MQTT.
- Broker: Runs on a server at the site (e.g. Eclipse Mosquitto) or in the cloud. For critical systems it should be redundant.
- Connection direction: Devices connect outbound to the broker, so no inbound port needs opening at the site. This is a big advantage for remote sites on 4G.
How to test MQTT
With the steps below you can run an end-to-end test on one computer in a few minutes.
- Install a broker. The most common open-source broker is Eclipse Mosquitto, available for Windows, Linux and Docker. For quick trials you can use the public broker
test.mosquitto.org, but do not send real plant data there; anyone can see it.# Linux
sudo apt install mosquitto mosquitto-clients
# Docker
docker run -it -p 1883:1883 eclipse-mosquitto
- Allow remote connections. By default Mosquitto 2.x only accepts connections from the same computer. To connect from other devices, add these lines to
mosquitto.conf (test environments only):listener 1883
allow_anonymous true
- Subscribe in one terminal. The
-v option also shows the topic of each message.mosquitto_sub -h localhost -t "factory/line1/#" -v
- Publish from a second terminal. The message should appear in the subscriber terminal straight away.
mosquitto_pub -h localhost -t "factory/line1/temperature" -m "23.5"
# With JSON data
mosquitto_pub -h localhost -t "factory/line1/motor1" -m '{"current":12.4,"state":1}'
# With retain and QoS 1
mosquitto_pub -h localhost -t "factory/line1/status" -m "running" -r -q 1
- Check retain. Close and reopen the subscriber terminal; the last value sent with
-r should arrive immediately. - Watch with a visual tool. Free tools such as MQTT Explorer or MQTTX show the whole topic tree and incoming values.
- Try username/password and TLS. Once the test works, disable anonymous access, create users and test again with TLS on port 8883.
Testing MQTT with ATLAS SCADA
You can also test the MQTT connection with ATLAS SCADA. This confirms not only that data reaches the broker, but also that it appears correctly on the SCADA screen.
- In ATLAS SCADA, define an MQTT connection with the broker address, port, client ID and credentials if any.
- Link the topic you want to test (e.g.
factory/line1/temperature) to a tag. - Send a value with
mosquitto_pub and watch it change on the ATLAS SCADA screen. - To test the other direction, listen with
mosquitto_sub and check that a value published from ATLAS SCADA reaches the terminal.
Tip: If the connection fails, check in order: is the broker running, is the port open, is another client connected with the same client ID, and do the topic's upper/lower case letters and slashes match exactly?