Post

MQTT

Control devices and IoT applications using MQTT protocol with publish/subscribe messaging.

MQTT

MQTT: IoT Messaging and Integration

MQTT (Message Queuing Telemetry Transport) integration enables Krill to communicate with IoT devices and applications using the lightweight publish/subscribe messaging protocol. Perfect for connecting smart home devices, Zigbee networks, and distributed sensor systems.

Overview

MQTT is the standard messaging protocol for IoT applications, providing efficient, reliable communication between devices and applications. Krill’s MQTT integration allows you to publish data point updates to MQTT topics and subscribe to topics to receive data from external devices.

Key Features

  • Publish/Subscribe Pattern: Efficient many-to-many communication
  • Data Point Integration: Automatically publish data point changes
  • Topic Subscriptions: Receive data from external MQTT sources
  • Zigbee Support: Integrates with Zigbee2MQTT for smart home devices
  • Low Bandwidth: Efficient protocol for constrained networks
  • Quality of Service: Configurable message delivery guarantees
  • Retained Messages: Persist important state information
  • Broker Integration: Connect to any MQTT broker (Mosquitto, HiveMQ, etc.)

MQTT Communication Flow

graph TD
    A[Krill Data Point Update] --> B[MQTT Publisher]
    B --> C[MQTT Broker]
    C --> D[MQTT Topics]
    D --> E[External Devices]
    D --> F[IoT Applications]
    D --> G[Smart Home Systems]
    
    H[External Sensors] --> I[MQTT Publish]
    I --> C
    C --> J[Krill MQTT Subscriber]
    J --> K[Update Data Points]

How It Works

Publishing (Krill → MQTT):

  1. Data Point value updates in Krill
  2. MQTT Publisher detects change
  3. Publishes value to configured topic on broker
  4. Subscribed devices/applications receive update

Subscribing (MQTT → Krill):

  1. External device publishes to MQTT topic
  2. Krill MQTT Subscriber receives message
  3. Parses message payload
  4. Updates corresponding Data Point
  5. Triggers any connected automation

Configuration

FieldDescriptionRequired
brokerUrlMQTT broker addressYes
clientIdUnique client identifierYes
publishTopicsTopics to publish data toNo
subscribeTopicsTopics to subscribe toNo
usernameBroker authentication usernameNo
passwordBroker authentication passwordNo
qosQuality of Service level (0, 1, 2)No

Quality of Service Levels

QoSDescriptionUse Case
0At most once (fire and forget)Non-critical data
1At least once (acknowledged)Important data
2Exactly once (guaranteed)Critical data

Use Cases

  • Smart Home Integration: Control lights, switches, thermostats via Zigbee2MQTT
  • Sensor Networks: Collect data from distributed MQTT-enabled sensors
  • Industrial IoT: Monitor equipment with MQTT-compatible devices
  • Home Automation: Integrate with Home Assistant, OpenHAB, Node-RED
  • Alert Systems: Publish alerts to MQTT topics for mobile apps
  • Data Distribution: Share sensor data across multiple systems
  • Device Control: Send commands to MQTT-controlled actuators

Example Workflows

Zigbee Light Control:

  1. Trigger: Button or Threshold
  2. Executor: Logic Gate (determine state)
  3. MQTT: Publish to zigbee2mqtt/bedroom_light/set
  4. Payload: {"state": "ON", "brightness": 255}

Temperature Monitoring:

  1. MQTT: Subscribe to sensors/temperature/room1
  2. Data Point: Update temperature value
  3. Trigger: High Threshold (temperature > 25°C)
  4. MQTT: Publish to climate/ac/set{"power": "on"}

Motion Detection:

  1. MQTT: Subscribe to zigbee2mqtt/motion_sensor
  2. Data Point: Update motion state
  3. Trigger: Value change to “detected”
  4. Executor: Pin Control (turn on lights)
  5. MQTT: Publish event to home/security/motion

Multi-Room Climate Control:

  1. MQTT: Subscribe to sensors/+/temperature (wildcard)
  2. Multiple Data Points: One per room
  3. Executor: Calculation (average temperature)
  4. Trigger: Threshold
  5. MQTT: Publish to hvac/control/setpoint

Zigbee2MQTT Integration

Krill works seamlessly with Zigbee2MQTT for smart home devices:

Common Zigbee Topics:

  • zigbee2mqtt/[device]/set - Send commands to devices
  • zigbee2mqtt/[device] - Receive device state updates
  • zigbee2mqtt/bridge/devices - List of available devices
  • zigbee2mqtt/bridge/info - Coordinator information

Supported Device Types:

  • Lights: Philips Hue, IKEA Tradfri, etc.
  • Switches: Smart switches and buttons
  • Sensors: Temperature, humidity, motion, door/window
  • Plugs: Smart outlets and power monitoring
  • Thermostats: Climate control devices

Topic Patterns

Publishing Data:

1
2
3
krill/datapoints/temperature → {"value": 23.5, "unit": "C"}
krill/sensors/pressure → 1013.25
krill/status/system → {"online": true, "uptime": 86400}

Subscribing to Data:

1
2
3
sensors/environment/# → Receive all environment sensors
home/+/temperature → Temperature from all rooms (+ is single level)
zigbee2mqtt/# → All Zigbee device updates

Message Format

JSON Payloads:

1
2
3
4
5
6
{
  "value": 23.5,
  "timestamp": 1643652000,
  "unit": "celsius",
  "sensor_id": "temp_01"
}

Simple Values:

1
2
3
23.5
ON
true

Integration Points

  • Data Points: Automatic publish/subscribe integration
  • Triggers: React to MQTT messages
  • Executors: Publish to MQTT as part of workflows
  • Lambda: Custom MQTT message processing
  • Webhooks: Forward MQTT data to cloud services

Best Practices

  • Topic Structure: Use hierarchical topics (home/room/device/property)
  • Retained Messages: Use for device state that should persist
  • Will Messages: Configure last will for connection monitoring
  • Security: Use authentication and TLS for production
  • Topic Naming: Be consistent and descriptive
  • Wildcards: Use + for single level, # for multi-level subscriptions
  • QoS Selection: Match QoS to data criticality
  • Payload Format: Use JSON for structured data

MQTT Broker Options

BrokerDescriptionUse Case
MosquittoOpen-source, lightweightSelf-hosted, local networks
HiveMQEnterprise-gradeLarge-scale deployments
EMQXScalable, distributedIoT cloud platforms
AWS IoT CoreCloud-basedAWS integration
Azure IoT HubCloud-basedAzure integration

Example: Complete Smart Home Integration

1
2
3
4
5
6
7
8
9
Zigbee Motion Sensor
  └─> MQTT Topic: zigbee2mqtt/motion_sensor
      └─> Krill Data Point: Motion Detected
          ├─> Trigger: Value = "detected"
          │   └─> MQTT Publish: zigbee2mqtt/bedroom_light/set
          │       (Turn on light)
          └─> Trigger: Silent Alarm (5 min, no motion)
              └─> MQTT Publish: zigbee2mqtt/bedroom_light/set
                  (Turn off light)

Monitoring and Debugging

  • Monitor broker logs for connection issues
  • Use MQTT clients (MQTT Explorer, mosquitto_sub) for testing
  • Log published and received messages
  • Verify topic subscriptions are active
  • Check QoS delivery confirmations
  • Monitor connection state

MQTT integration transforms Krill into a comprehensive IoT automation platform, enabling seamless communication with smart home devices, industrial sensors, and distributed applications.

This post is licensed under CC BY 4.0 by the author.