ioBroker and MQTT adapter

One of the most important accessories of our self-made Smart Home devices is the ioBroker MQTT adapter. MQTT enables low-resource two-way communication, so the MQTT protocol is a good option for data exchange between a server and a microcontroller.

ioBroker MQTT adapter

If ioBroker is not already installed, click here.

The MQTT adapter must be installed first. To do this, enter MQTT in the viewfinder in the adapter panel, then select the MQTT Broker / Client adapter.

ioBroker Adapter panel
ioBroker Adapter panel

Click the “+” button:

Installing the MQTT Adapter
Installing the MQTT Adapter
MQTT Adapter installation in progress ...
MQTT Adapter installation in progress…

When done, select Server / broker, select the IP address below. You can set a user name and password, but this is not required on an internal network.

MQTT settings
MQTT settings

We’re done. You can use the MQTT adapter. Click the “Play” button.

Start the MQTT adapter ...
Start the MQTT adapter…

The MQTT adapter in the Object panel is not connected yet.

The MQTT adapter in the Object panel
The MQTT adapter in the Object panel

Let’s see our first tool, like this one.

NodeMCU Esp8266 and an LED
NodeMCU Esp8266 and an LED

And the code:

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

const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.xxx.xxx";

int Led = D5;

WiFiClient espClient;
PubSubClient client(espClient);


void setup() 
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  
  pinMode(Led, OUTPUT);
  digitalWrite(Led, LOW);
}

void loop() 
{
  if (!client.connected()) 
  {
    reconnect();
  }
  client.loop();
}

void reconnect() 
{
  while (!client.connected()) 
  {
    String clientId = "ESP8266_LED_Client";
    
    if (client.connect(clientId.c_str()))
    {
      client.subscribe("LEDtopic");
    } 
    else 
    {
      delay(6000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) 
{
  payload[length] = '
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.xxx.xxx";
int Led = D5;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() 
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) 
{
delay(500);
}
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(Led, OUTPUT);
digitalWrite(Led, LOW);
}
void loop() 
{
if (!client.connected()) 
{
reconnect();
}
client.loop();
}
void reconnect() 
{
while (!client.connected()) 
{
String clientId = "ESP8266_LED_Client";
if (client.connect(clientId.c_str()))
{
client.subscribe("LEDtopic");
} 
else 
{
delay(6000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) 
{
payload[length] = '\0';
String strTopic = String(topic);
String strPayload = String((char * ) payload);
if(strTopic == "LEDtopic") 
{
if(strPayload == "false") 
{
digitalWrite(Led, LOW);
}
if(strPayload == "true") 
{
digitalWrite(Led, HIGH);
}
}
}
'; String strTopic = String(topic); String strPayload = String((char * ) payload); if(strTopic == "LEDtopic") { if(strPayload == "false") { digitalWrite(Led, LOW); } if(strPayload == "true") { digitalWrite(Led, HIGH); } } }

Quickly mount the NodeMCU on Esp8266 and see the result.

the new device in the MQTT adapter
the new device in the MQTT adapter

Our device has been released, now we can control it. In the text box, type “true” to turn it on or “false” to turn it off.


With this simple little MQTT client app we can connect our Android phone to the MQTT server

MQTT Client App is available on Google Play
MQTT Client App is available on Google Play
Beelink SER5 Mini PC

ad

Beelink SER5 Mini PC

AMD Ryzen 7 5800H Processor,Mini Computer with 16G DDR4 RAM/500GB M.2 NVMe 2280 SSD,4K FPS/Three-Screen Display/WiFi 6/BT5.2/Support Auto Power On.

Ideal for Smart Home, Mosquitto, ioBrokers or media servers.