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

I know this is an old post, but can you use ESPHome with OpenHAB? From what I’ve read it looks like that is a Home Assistant add-on. Is there something similar for OH?

You can use anything that talks MQTT with openHAB. The MQTT binding supports automatically discovering devices that follow either the Home Assistant or the Homie standards for MQTT. I’m certain ESPHome talks MQTT. I don’t know if it supports those other standards but if not, that just means you have to manually configure the MQTT Things.

Can you just use ESP32 to connect to MQTT to accomplish the same goal?

ESP32 running what? An ESP32 is just a microcontroller with some transceivers. You have to put some code on it to tell it what to do. ESPHome is one such. Tasmota and ESPEasy are two other choices. Or you can write something from scratch if that’s your thing.

There is someone who is using an ESP32, ESPHome and Home Assistant to control a BedJet. They flashed the ESP32 with the BedJet controller code.

If they flashed the ESP32 with BedJet controller code they are not using ESPHome, unless that is somehow a plug-in to ESPHome.

But I’ll say it again.

  • If it speaks MQTT OH can talk to it
  • ESPHome speaks MQTT
  • Therefore OH can talk with ESPHome.

Thank you!