✔ Communication Protocol

What is MQTT? How do you test it? A lightweight publish/subscribe protocol for IoT and cloud connections

MQTT is a lightweight publish/subscribe protocol designed for low-bandwidth and unreliable networks.

Devices do not connect to each other directly; they all connect to a central broker. Whoever sends data publishes it, and whoever needs it subscribes.

Highlights

  • TCP 1883
  • 8883 with TLS
  • Broker
  • Topic
  • QoS 0/1/2
  • Sparkplug B

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 sourceIoT GatewayModbus → MQTTNetworkEthernet, Wi-Fi, 4GMQTT Brokerlocal server or cloudSCADA / 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.

Cable and material selection

  • On site: Cat6 Ethernet; shielded cable in noisy areas.
  • Remote sites: An industrial 4G router or a 4G IoT gateway. An external antenna placed outside the cabinet greatly improves connection quality.
  • Wireless sensors: LoRaWAN or Wi-Fi sensors where cables cannot be run; data is passed from the gateway via MQTT.
  • Broker server: Power it through a UPS; size disk and memory according to the number of connected devices.
  • Distance: MQTT runs over TCP/IP, so the protocol itself has no distance limit; only the network sets the limit.

Advantages and disadvantages

Advantages

  • Very lightweight; works well on low bandwidth and weak links
  • Easy to scale to many devices
  • Report-on-change avoids unnecessary traffic
  • Easy integration with cloud and IT systems
  • No inbound ports needed at the site

Disadvantages

  • The broker is a single point of failure; redundancy may be needed
  • The protocol does not define a data format; a common structure must be agreed
  • Security is not on by default; TLS and user authorisation must be set up
  • Control needs separate acknowledgement and timeout logic

MQTT compared with similar protocols

MQTTModbus TCPOPC UA
ModelPublish / subscribeRequest / responseClient / server (and PubSub)
Data deliveryOn changeConstant pollingOn change via subscriptions
BandwidthVery lowMediumMedium–high
Opening ports at remote sitesNot neededNeeded (or VPN)Needed (or VPN)
Data definitionFree-form (JSON, Sparkplug B)Numbered registersRich data model
Best forIoT, cloud, scattered sitesField devicesPLC–SCADA–MES

How to test MQTT

With the steps below you can run an end-to-end test on one computer in a few minutes.

  1. 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
  2. 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
  3. Subscribe in one terminal. The -v option also shows the topic of each message.
    mosquitto_sub -h localhost -t "factory/line1/#" -v
  4. 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
  5. Check retain. Close and reopen the subscriber terminal; the last value sent with -r should arrive immediately.
  6. Watch with a visual tool. Free tools such as MQTT Explorer or MQTTX show the whole topic tree and incoming values.
  7. 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.

  1. In ATLAS SCADA, define an MQTT connection with the broker address, port, client ID and credentials if any.
  2. Link the topic you want to test (e.g. factory/line1/temperature) to a tag.
  3. Send a value with mosquitto_pub and watch it change on the ATLAS SCADA screen.
  4. 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?

Let's review your project together

Let's work out together which architecture, hardware and cabling fit your facility.

Let's talk about your project