MQTT

From SweepMe! Wiki
Jump to navigation Jump to search

The MQTT module enables connection to an MQTT broker, a server that retains the latest values of topics. New data for a topic overwrites the previous value. You can publish data to update the broker's current values or subscribe to topics to receive notifications when related data changes.

Using MQTT greatly extends SweepMe!'s functionality. It allows data handling outside the program without interfering with measurements, for example, building web apps to monitor status, sending emails post-measurement, or forwarding data to a database for automatic storage.

Graphical User Interface

Mode

Select whether to use the MQTT module for publishing or subscribing.

  • Publish: Sends data to the MQTT broker.
  • Subscribe: Receives data from the broker. Call the module regularly in your sequencer (e.g., within a Loop) for continuous updates.

Host

Enter the MQTT broker's IP address (e.g., localhost if running locally, 192.168.0.102 for IPv4, or a domain name).

Port

Most brokers use port 1883. Contact the administrator if it fails.

User

Enter a username if access is restricted.

Password

Provide the password associated with the username.

Topic

Enter the base topic for the module; it prefixes all data sets. Supports subtopics like "main/sub/subsub". Leading and trailing slashes are handled automatically.
See publishing or subscribing sections for details.

Quality of Service (QoS)

MQTT supports three QoS levels for data exchange reliability.

QoS Level Description Use Case
0 (At most once) Sent once; no retry on failure. Frequent, loss-tolerant data with stable connections.
1 (At least once) Retries without acknowledgment; may send duplicates. Ensures delivery; duplicates acceptable.
2 (Exactly once) Handshake ensures single delivery. Critical data (e.g., database storage without duplicates or loss).

Retain

(Publish only) Retained messages are stored by the broker as the last known good value and sent immediately to new subscribers.

Publishing

Select variables to publish. The full topic combines the base Topic with each variable's subtopic. Right-click a variable to copy its topic.

Subscribing

Subscribe to broker topics. The module returns the latest values updated by broker messages.

MQTT Brokers

Mosquitto (https://mosquitto.org/), a free open-source broker from the Eclipse Foundation, runs locally (localhost) for testing or on a central machine (e.g., Raspberry Pi) in production—use its IP address.

Test broker: https://test.mosquitto.org/. Avoid sensitive data, as it's publicly accessible.

Node-RED Integration

Node-RED (https://nodered.org/) can be used to create browser-based user interfaces for MQTT data.