How to Setup MQTT connection between ESP32 and OpenHAB2

HELLO,
Platform information:
Hardware: Raspberry Pi 4
OS:openHABian
openHAB version: OpenHAB 2.5

I am trying to make MQTT connection between ESP32 and openHAB version 2.5
Someone please guide me with the steps to establish MQTT connection between ESP32 and openHAB

Arduino code for ESP32:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//#include <WiFi.h>
#include <WiFiClient.h>

// Update these with values suitable for your network.
//#define status_led BUILTIN_LED
#define load1 2
#define input1 4

const char* ssid = “makerdemy2”;
const char* password = “india123”;
const char* mqtt_server = “192.168.1.5”;

WiFiClient espClient;
PubSubClient client(espClient);

IPAddress ip(192, 168, 1, 2);
IPAddress gateway_dns(192, 168, 1, 1);

long lastMsg = 0;
char msg[50];
int value = 0;

int last_pub_time=0;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.config(ip, gateway_dns, gateway_dns);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

randomSeed(micros());

Serial.println("");
Serial.println(“WiFi connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
if ((char)payload[0] == ‘1’)
digitalWrite(load1, HIGH);
else
digitalWrite(load1, LOW);

}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Create a random client ID
String clientId = “lens_RtyALk5DrbsZ62QgebwDxBBaJdB”;
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),“openhabian”, “openhabian”)) {
Serial.println(“connected”);
// Once connected, publish an announcement…
// client.publish(“outTopic”, “hello world”);
// … and resubscribe
client.subscribe("/home/esp8266/switch/command");
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
// pinMode(status_led, OUTPUT);
pinMode(load1, OUTPUT);
pinMode(input1, INPUT_PULLUP);

Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {

if (!client.connected()) {
reconnect();
}
if(!digitalRead(input1) && (millis()- last_pub_time > 3000)){
last_pub_time=millis();
client.publish("/home/esp8266/switch/state",“1”);
}
client.loop();
}

Thanks in advance.

From the information you gave, you may want to revisit the docs and read this: How to ask a good question / Help Us Help You

Also, pls re-read the basics how MQTT operates. You will need to make sure that three parts work together via two communication channels: OH2 will need to connect to an MQTT broker and your device will need to connect to an MQTT broker. There are external brokers, they can reside on your pi, on a different machine or in the cloud (and OH2 provides options for that too).

Hence a few questions: which broker do you use, is it running, are the IPs all correctly configured, are there error in the log file, can OH2 connect to the broker (OH2 log file will tell you), what happens at your esp32, did you see on the console that your esp32 (a) connects to the WIFI, and (b) to the broker? On the OH2 side, did you configure your thing and items correctly?

Please note also that this is an OH2 forum, the docs here will give you information for OH2, the support for esp may be limited and you may need to get back to the source where you found your esp32 code.

Lastly, search the forum, but please be aware that a lot of changes happened over the last years as to how MQTT works with OH2. Make sure that you focus on MQTT for OH2.5, earlier versions have different configuration files which can become very confusing.

Just use esphome. No need to code simple mqtt switches or lights by yourself

1 Like