NodeMCU ESP8266 to ioBroker, connect relay via MQTT

I would like to show you how easy it is to create various smart devices using NodeMCU ESP8266 in an arduino framework and integrate them into ioBroker using the MQTT protocol. In the first part, we will turn on an LED and a relay.


The circuit only needs a few components:

ESP8266 NodeMCU-12E

3mm and 5mm Diffused and Clear Assorted LED Kit 5 Colors

5V Relay Module with Optocoupler

Resistor Kit Assortment, 0 Ohm-1M Ohm

Breadboard Jumper Wires Ribbon CablesKit – M2M / F2M / F2F

Breadboards Kit


Put it together on a breadboard:

NodeMCU 8266 and the LED
NodeMCU 8266 and the LED

That is all. It’s not complicated, is it?

In the next step, upload the following code to the NodeMCU 8266.
We will use the Arduino development environment (IDE) for this purpose.

I assume you have the ESP8266 motherboard and the PubSubClient library installed.


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

const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "MQTT.Server.IP.Address";

int Led = D8;

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 = "ESP8266TestClient";
    
    if (client.connect(clientId.c_str()))
    {
      client.subscribe("topic_name");
    } 
    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 = "MQTT.Server.IP.Address";
int Led = D8;
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 = "ESP8266TestClient";
if (client.connect(clientId.c_str()))
{
client.subscribe("topic_name");
} 
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 == "topic_name") 
{
if(strPayload == "false") 
{
digitalWrite(Led, LOW);
}
if(strPayload == "true") 
{
digitalWrite(Led, HIGH);
}
}
}
'; String strTopic = String(topic); String strPayload = String((char * ) payload); if(strTopic == "topic_name") { if(strPayload == "false") { digitalWrite(Led, LOW); } if(strPayload == "true") { digitalWrite(Led, HIGH); } } }

Instead of the LED, you can also switch a relay module.

NodeMCU8266 with Relay
NodeMCU8266 with Relay

With this relay we can also connect devices operating on mains voltage
e.g. lamp, fan, coffee maker, etc.


Attention!
Mains voltage is not a toy, it can cause a fatal electric shock or fire! Only at your own risk and only if you know what you are doing!


If you’re interested in how to connect to ioBroker, check out ioBroker and the MQTT adapter

You can find more interesting things here .