ioBroker and MQTT adapter

One of the most useful tools for connecting our homemade smart home devices is the ioBroker MQTT adapter. MQTT enables two-way communication with modest resource requirements, making it a good choice for exchanging data between a server and a microcontroller.

Difficulty: Intermediate

Created: January 2021

Reviewed: November 2026

Prerequisite: An installed ioBroker server.

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol commonly used in IoT (Internet of Things) applications for communication between devices. Its low bandwidth requirements and small code footprint make it suitable for hobby projects using microcontrollers such as the ESP8266 and ESP32, as well as for industrial devices.

MQTT communication requires a broker. Clients publish messages to the broker and receive messages from it. They do not exchange MQTT messages directly with one another.

MQTT uses a publish–subscribe model over TCP/IP. The default port is 1883, while 8883 is commonly used for TLS-encrypted connections. The ioBroker MQTT adapter can also accept WebSocket connections. By default, its WebSocket port is one higher than its configured MQTT port, so with the default settings it is 1884.

A conventional MQTT broker primarily forwards messages. It stores the latest message for a topic when that message is published with the retained flag. The ioBroker MQTT adapter behaves differently in broker mode because it also saves incoming values in the ioBroker states database. Its broker mode therefore does not behave exactly like a standalone MQTT broker.

Clients can publish messages to a topic or subscribe to topics. To receive a message from the broker, a client must subscribe to the relevant topic.

Quality of Service (QoS) levels:

  • QoS 0: At most once. The message is sent without delivery acknowledgement.
  • QoS 1: At least once. Delivery is acknowledged, but duplicate messages are possible.
  • QoS 2: Exactly once at the MQTT protocol level.

MQTT was designed with constrained devices and networks in mind. It is useful where bandwidth is limited or connections are unreliable. Features such as persistent sessions can also help clients resume communication after reconnecting, provided the client and broker are configured to use them.

Installing the MQTT adapter

Prerequisites

To use the MQTT adapter, you need a working ioBroker server.

If you have not installed ioBroker yet, see the installation guide.

Adding an MQTT adapter instance

First, add an instance of the MQTT adapter. Open the Adapters page, search for “MQTT broker”, and select the MQTT Broker/Client adapter.

MQTT adapter in the ioBroker Adapters panel
The MQTT adapter in the ioBroker Adapters panel

Click the blue circular button to turn the adapter card over, then click the “+” button to add an instance.

Adding an MQTT adapter instance
Adding an MQTT adapter instance

Wait for the process to finish. The MQTT adapter instance is now installed.

MQTT adapter installation in progress
MQTT adapter installation in progress

Configuring the MQTT adapter instance

The ioBroker MQTT adapter can operate as either a broker or a client. The first step on its settings page is to choose the operating mode.

Server/broker mode

Once the installation is complete, open the Connection tab on the settings page and select Server/broker from the drop-down list. Then choose the IP address and port on which the MQTT broker should listen.

You can also set a username and password. If you do not want to use authentication for this example, you can leave these fields empty.

If you enable username and password authentication, use it together with a properly configured TLS connection. Sending credentials over an unencrypted connection is not secure.

MQTT adapter connection settings
MQTT adapter connection settings

Enable the SSL option if you want to encrypt the connection. You will also need to select the appropriate certificates from the available certificate settings.

SSL certificate settings for the ioBroker MQTT adapter
SSL certificate settings for the ioBroker MQTT adapter

You will find additional options on the MQTT Settings tab.

Additional options on the MQTT Settings tab
Additional options on the MQTT Settings tab

Mask to publish own states: This setting determines which of ioBroker’s own states the adapter publishes to MQTT clients. If left empty, the adapter can publish all applicable own states. Entering a pattern lets you limit them. For example, mqtt.2.light.* selects states below mqtt.2.light. You can enter multiple comma-separated patterns. With mqtt.2.*, shelly.0.*, states below both mqtt.2 and shelly.0 are selected.

Sensor readings and status reports received from Shelly devices may have ack=true in ioBroker. To publish those states as well, enable Send states (ack=true) too.

Send states (ack=true) too: This option also publishes states marked with ack=true. Without it, the adapter publishes only the states covered by its default acknowledgement setting.

Publish/update only in case of change: When enabled, the adapter sends an update to clients only when a state’s value actually changes. This can reduce unnecessary network traffic.

Publish own states on connect: When an MQTT client connects, the adapter publishes the current values of states matching Mask to publish own states. The client receives only messages that match its subscriptions.

Keeping the last value: In broker mode, the ioBroker MQTT adapter stores incoming values in the ioBroker states database, so the most recent value of a state remains visible later. Depending on the adapter settings, a newly subscribing MQTT client may also receive that value as a message. This is useful for status information, but command topics require care: a client program could interpret an old value as a new command.

Use different topic names for set and get: This option separates reading and writing. The adapter uses the plain topic for reading and expects a /set suffix for commands, such as mqtt/2/Lighting and mqtt/2/Lighting/set. This helps avoid feedback loops and unintended switching caused by previously stored values.

When you have finished configuring the adapter, click Save and close.


The MQTT adapter instance now appears on the Instances page. Here you can see information about it and start, stop, restart, or delete it. Click the wrench icon to return to its settings.

MQTT adapter instance on the Instances page
The MQTT adapter instance on the Instances page

On the Objects page, you can find the states associated with devices connected to this MQTT adapter instance.

MQTT adapter states on the ioBroker Objects page
MQTT adapter states on the Objects page

Client/subscriber mode

The ioBroker MQTT adapter can also operate as a client. This is useful when you want to connect ioBroker to another MQTT broker or simulate an MQTT-connected device.

On the Connection tab, select Client/subscriber from the drop-down list. Enter the broker address and port. If the broker uses an encrypted connection, enable SSL. If required, enter the username and password for the broker.

Connection tab in MQTT client mode
The Connection tab in MQTT client mode

You will find more important options on the Client settings tab.

Options on the Client settings tab
Options on the Client settings tab

Subscribe patterns: Enter one or more comma-separated topic patterns to which the client should subscribe. With test/mySwitch, the client subscribes only to that specific topic. With test/#, it subscribes to the topics below test.

Client ID: This identifier is used when the client connects to the MQTT broker.

Connecting your own device to the MQTT broker

The hardware

Build the simple circuit shown below. For this example, we will use a NodeMCU ESP8266 and an LED with a series resistor.

NodeMCU ESP8266 with an LED and series resistor
NodeMCU ESP8266 with an LED

The Arduino sketch

Enter your Wi-Fi credentials and your ioBroker server’s IP address in the example sketch below, then upload it to the NodeMCU ESP8266.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

constexpr char ssid[] = "mySSID";
constexpr char password[] = "myPassword";
constexpr char mqtt_server[] = "192.168.1.23";
constexpr char clientId[] = "ESP8266-LedTest";

constexpr uint16_t ledPin = 5;  // GPIO5 -> D1

unsigned long lastReconnectAttempt = 0;
unsigned long lastWifiReconnectAttempt = 0;

WiFiClient espClient;
PubSubClient client(espClient);


void reconnect() 
{
  Serial.println(F("Connecting to the MQTT server..."));
  if(!client.connected()) 
  {
    if(client.connect(clientId))
    {
      Serial.println(F("MQTT server is connected!"));
      client.subscribe("test/led");
    } 
    else 
    {
      Serial.print(F("MQTT connection failed, retrying in 5 seconds"));
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length)
{
  if(strcmp(topic, "test/led") == 0)
  {
    if(strncmp((char*)payload, "false", length) == 0 && length == 5)
    {
      digitalWrite(ledPin, LOW);
      Serial.println(F("Led is off!"));
    } 
    else if(strncmp((char*)payload, "true", length) == 0 && length == 4)
    {
      digitalWrite(ledPin, HIGH);
      Serial.println(F("Led is on!"));
    }
  }
}

void setup() 
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  
  Serial.begin(115200);
  delay(500);

  Serial.println();
  Serial.println(F("Connecting to the wifi network..."));
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("Wifi is connected!"));

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() 
{
  unsigned long now = millis();

  if(WiFi.status() != WL_CONNECTED)
  {
    if(now - lastWifiReconnectAttempt > 5000)
    {
      lastWifiReconnectAttempt = now;
      Serial.println(F("WiFi connection lost! Reconnecting..."));
      WiFi.disconnect();
      WiFi.begin(ssid, password);
    }
    return;
  }

  if(!client.connected()) 
  {
    if(now - lastReconnectAttempt > 5000)
    {
      lastReconnectAttempt = now;
      reconnect();
    }
  }
  else
  {
    client.loop();
  }
}

After the Wi-Fi and MQTT connections have been established, we can check the result in ioBroker.

LED state created for the MQTT example in ioBroker
The LED state in ioBroker

After connecting to Wi-Fi and MQTT, the ESP8266 subscribes to the test/led MQTT topic. In the tested ioBroker setup, the corresponding mqtt.2.test.led state appeared on the Objects page with a null value. The sketch does not publish its own message to this topic. The MQTT adapter’s instance number may differ in your installation. If the state does not appear, check the connection and adapter settings.

Set the value of mqtt.2.test.led to true to turn the LED on, or to false to turn it off.


← More ioBroker articles