MQTT
Control devices and IoT applications using MQTT protocol with publish/subscribe messaging.
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):
- Data Point value updates in Krill
- MQTT Publisher detects change
- Publishes value to configured topic on broker
- Subscribed devices/applications receive update
Subscribing (MQTT → Krill):
- External device publishes to MQTT topic
- Krill MQTT Subscriber receives message
- Parses message payload
- Updates corresponding Data Point
- Triggers any connected automation
Configuration
| Field | Description | Required |
|---|---|---|
brokerUrl | MQTT broker address | Yes |
clientId | Unique client identifier | Yes |
publishTopics | Topics to publish data to | No |
subscribeTopics | Topics to subscribe to | No |
username | Broker authentication username | No |
password | Broker authentication password | No |
qos | Quality of Service level (0, 1, 2) | No |
Quality of Service Levels
| QoS | Description | Use Case |
|---|---|---|
| 0 | At most once (fire and forget) | Non-critical data |
| 1 | At least once (acknowledged) | Important data |
| 2 | Exactly 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:
- Trigger: Button or Threshold
- Executor: Logic Gate (determine state)
- MQTT: Publish to
zigbee2mqtt/bedroom_light/set - Payload:
{"state": "ON", "brightness": 255}
Temperature Monitoring:
- MQTT: Subscribe to
sensors/temperature/room1 - Data Point: Update temperature value
- Trigger: High Threshold (temperature > 25°C)
- MQTT: Publish to
climate/ac/set→{"power": "on"}
Motion Detection:
- MQTT: Subscribe to
zigbee2mqtt/motion_sensor - Data Point: Update motion state
- Trigger: Value change to “detected”
- Executor: Pin Control (turn on lights)
- MQTT: Publish event to
home/security/motion
Multi-Room Climate Control:
- MQTT: Subscribe to
sensors/+/temperature(wildcard) - Multiple Data Points: One per room
- Executor: Calculation (average temperature)
- Trigger: Threshold
- 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 deviceszigbee2mqtt/[device]- Receive device state updateszigbee2mqtt/bridge/devices- List of available deviceszigbee2mqtt/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
| Broker | Description | Use Case |
|---|---|---|
| Mosquitto | Open-source, lightweight | Self-hosted, local networks |
| HiveMQ | Enterprise-grade | Large-scale deployments |
| EMQX | Scalable, distributed | IoT cloud platforms |
| AWS IoT Core | Cloud-based | AWS integration |
| Azure IoT Hub | Cloud-based | Azure 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.