EMQX Dedicated New Feature: Event History is available for private beta test. →

MQTT with openHAB: A Step-by-Step Tutorial

EMQX Cloud Team
Mar 27, 2024
MQTT with openHAB: A Step-by-Step Tutorial

Introduction

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for IoT and M2M communication. It operates on a publish-subscribe model, facilitating efficient message exchange between devices and a central broker. MQTT is known for its simplicity, low bandwidth usage, support for various Quality of Service levels, asynchronous communication, and robust security features, making it a popular choice for IoT applications requiring reliable messaging over constrained networks.

OpenHAB is an open-source home automation platform designed to integrate and control diverse smart home devices and technologies. It offers unified control through a central hub, supports various protocols for device integration, enables advanced automation, provides flexibility for customization, and benefits from a thriving community of users and developers.

In this article, we will use an MQTT broker to connect to openHAB, greatly leveraging openHAB's extension capabilities. Once we associate MQTT devices with openHAB, we can link these devices with other smart home devices to complete smart home management and coordination.

Why Use MQTT in openHAB?

  1. Interoperability: MQTT is widely supported, enabling seamless integration of various smart home devices within openHAB.
  2. Flexibility: MQTT's lightweight design allows for adaptable communication between devices, enabling custom automation rules.
  3. Scalability: MQTT scales well for both small and large smart home setups, ensuring efficient communication and control.
  4. Reliability: MQTT's Quality of Service levels guarantee message delivery, maintaining system stability even in challenging network conditions.
  5. Community Support: Both MQTT and openHAB have active communities, providing resources and assistance for setting up and customizing smart home automation.

A Step-by-Step Guide on Integrating MQTT with openHAB

Prepare an MQTT Broker

Before proceeding, please ensure you have an MQTT broker to communicate and test with.

We will use EMQX Serverless in this article. EMQX Serverless is an MQTT broker offering on the public cloud with all the serverless advantages. You can start the Serverless deployment in seconds with just a few clicks. Additionally, users can get 1 million free session minutes every month, sufficient for 23 devices to be online for a whole month, making it perfect for tiny IoT test scenarios.

Set up an EMQX Serverless Deployment

Register for an account to access EMQX Console.

Try EMQX Serverless for Free
No credit card required
Get Started →

1. Create a Serverless Deployment

Log in to the Console and click the “New Deployment” button to begin creating a new deployment. Select the “Serverless” plan to deploy in the region close to your service.

EMQX Serverless

Set the Spend Limit to 0, which means we can have a MQTT Serverless deployment for free. Then, click “Deploy” to initiate the serverless deployment.

Set the Spend Limit to 0

2. Add a Credential for the MQTT Connection

In the Console, navigate to “Authentication & ACL” from the left menu, and then click “Authentication” in the submenu. Click the “Add” button on the right and provide a username and password for the MQTT connection. For this example, we will use emqx as the username and public as the password for the MQTT client connection.

Add Authentication

With these steps, your MQTT Serverless is now operational and ready for use.

Deployment status

Download and Install OpenHAB

Download OpenHAB (Download page) and install it. Once openHAB is installed and started, launch the user interface by navigating to http://localhost:8080 (if not running locally, replace localhost with the server's address or hostname accordingly).

Install MQTT Binding

Navigate to Add-on Store, find MQTT Binding in openHAB Distribution. Click “INSTALL” button to install.

openHAB Install MQTT Binding

Add Things from MQTT Binding

In openHAB, "Things" are the physical or virtual devices integrated into the home automation system, such as lights, sensors, or switches. They serve as the interface between openHAB and these devices, enabling users to monitor and control them within the openHAB environment.

Navigate to Settings - Things page, click “+” to create a Thing from MQTT Binding we installed in the last step.

Add Things from MQTT Binding

On the next page, we add a MQTT Broker.

Add a MQTT Broker to openHAB

In the configuration, enter the Broker host and port which can be found on EMQX Serverless deployment overview page. Enable Secure Connection and Select TCP as protocol.

Enable **Secure Connection** and Select **TCP** as protocol.

MQTT 3 is the established version of the protocol, featuring QoS levels, retained messages, and clean sessions. MQTT 5, the latest version, introduces advanced features like user properties, shared subscriptions, message expiry, topic aliases, and response codes, enhancing flexibility and scalability.

MQTT offers three levels of Quality of Service (QoS):

  1. QoS 0: "At most once" - Messages are delivered without acknowledgment.
  2. QoS 1: "At least once" - Messages are guaranteed to be delivered, possibly resulting in duplicates.
  3. QoS 2: "Exactly once" - Ensures each message is delivered exactly once, with the highest reliability.

QoS

Input Username and Password, which can be found on the EMQX Serverless Authentication page.

Input Username and Password

Following the setup, we'll have an operational MQTT Broker successfully linked with openHAB.

MQTT Broker successfully linked with openHAB EMQX Console

Add Channel for MQTT Device

In openHAB, "Channels" act as communication links between Things and Items, representing specific functionalities or data points of a Thing. They enable users to control or monitor the capabilities of Things by sending commands or receiving updates.

Add Channel for MQTT Device

For MQTT Broker, there is one channel type: Publish trigger. This channel is triggered when a value is published to the configured MQTT topic on this broker connection. Then we need to define an MQTT Topic. Any message sent to this topic will trigger the the channel event.

Add emqx_channel

Assign an item to the channel. Here, we can specify its type and category.

Add a new Item

This completes the connection of a simple smart home device. When a device connects to EMQX Serverless and sends a message to the t/emqx topic, it triggers the Channel and sends instructions to the corresponding device.

Conclusion

The usage of MQTT in openHAB facilitates seamless communication and control among smart home devices within the openHAB ecosystem. It enables device integration, message passing, topic-based communication, event triggering, and remote access/control. MQTT's role in openHAB enhances interoperability and flexibility, empowering users to create sophisticated smart home automation solutions.

EMQX Serverless simplifies the deployment and management of MQTT messaging infrastructure, offering powerful capabilities. By leveraging EMQX Serverless, you can streamline development processes and seamlessly integrate with openHAB to build comprehensive solutions.

Related Resources

Talk to an Expert
Contact Us →

Related Posts

Jun 3, 2023Saiteng You
How to Use MQTT on Raspberry Pi with Paho Python Client

The Paho Python Client provides a client class with support for both MQTT v3.1 and v3.1.1 on Python 2.7 or 3.x. This article introduces how to use the Paho MQTT client library in the Python project.