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.
If ioBroker is not already installed, click here.
myQ Chamberlain Smart Garage Control
advertising
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.
Click the “+” button:
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.
We’re done. You can use the MQTT adapter. Click the “Play” button.
The MQTT adapter in the Object panel is not connected yet.
Let’s see our first tool, like this one.
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.
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
ad
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.