Skip to main content

MQTT Publish

This block publishes a message to an MQTT topic. Use it to send data to MQTT brokers for communication with other devices and cloud services.

MQTT Publish Block

Overview

The MQTT Publish block sends messages to specified topics on your configured MQTT broker, enabling device-to-device communication and cloud integration.

Configuration

  • Topic: MQTT topic to publish to (e.g., "sensors/temperature")
  • Message: Payload to send (text, JSON)
  • QoS: Quality of Service level (0, 1, or 2)
  • Retain: Whether broker should retain message for new subscribers

Topic Structure

Design topic hierarchy:

device/sensor/temperature
device/sensor/humidity
device/status/online
alerts/critical/temperature

Use forward slashes (/) to organize topics hierarchically.

Quality of Service

Choose appropriate QoS level:

  • QoS 0: At most once delivery (fire and forget)

    • Fastest, lowest overhead
    • No delivery guarantee
    • Use for frequent, non-critical data
  • QoS 1: At least once delivery (acknowledged)

    • Guaranteed delivery
    • Possible duplicates
    • Use for important data
  • QoS 2: Exactly once delivery (assured)

    • Highest reliability
    • Highest overhead
    • Use for critical data

Message Format

Send various message types:

Plain Text

Temperature: 22.5

JSON

{"temperature": 22.5, "unit": "C"}

Retained Messages

Enable retain flag for:

  • Latest status updates
  • Configuration information
  • State that should persist

New subscribers immediately receive last retained message on topic.

Dynamic Topics

Build topics dynamically:

  • Include device ID: devices/${deviceId}/status
  • Include location: building1/floor2/room3/temp
  • Include timestamp: logs/2023/10/09/events

Use variables to construct topic names.

Use Cases

Common MQTT publish scenarios:

  • Send sensor readings to cloud
  • Publish device status updates
  • Broadcast alerts and alarms
  • Share data between devices
  • Log events to central server

Message Payload

Prepare message payload from:

  • Variable values
  • Sensor readings
  • Formatted strings
  • JSON objects
  • Calculated values

Error Handling

Handle publish failures:

  • Connection lost: Message queued for retry
  • Invalid topic: Check topic format
  • Authentication error: Verify broker credentials
  • QoS timeout: Check network reliability

Performance Considerations

Optimize MQTT publishing:

  • Use QoS 0 for high-frequency data
  • Batch data when possible
  • Minimize payload size
  • Avoid publishing in tight loops

Security

Secure MQTT publishing:

  • Use TLS/SSL encryption
  • Implement authentication
  • Restrict topic permissions
  • Validate message content

Troubleshooting

Common issues:

  • Message not published: Check MQTT connection status
  • No subscribers receiving: Verify topic name matches subscriptions
  • High latency: Reduce QoS level or check network
  • Authentication failed: Verify broker credentials

See Also