[Solved] MQTT ESP32 Thing is not connected to the server

Dear Experts,

My openHAB and MQTT is working fine. Now I am trying to connect ESP32 thing to MQTT. The serial monitor is showing that WIFI is connected but MQTT is not connected. I will be thankful if anybody suggests me. The program is attached. Thanks

Program 2 Program Serial

Hi,
first of all it’s a good idea to hide your Wifi password in screenshot.
second: it’s only a small part of your sketch I think. So I can guess only what the problem can be.

How did you construct the client?
PubSubClient client(wclient);

Did you set the IP adress of the broker?

client.setServer(broker, 1883);

I am not such Code, however stating the IP of the broker separated with commas instead of points looks odd.

Thanks Daniel,

I Set the broker as client.setServer(broker, 1883); Please find my full code and suggest.

#include <WiFi.h>
#include <PubSubClient.h>

const char *ssid = “…”; // name of your WiFi network
const char *password = “…”; // password of the WiFi network

const byte SWITCH_PIN = 0; // Switch Pin to control the Relay
const byte LIGHT_PIN = 5; // Pin to control the Relay
const char *ID = “openHAB2”; // Name of our device, must be unique
const char *TOPIC = “/habib/arduino/command”; // Topic to subcribe to
const char *STATE_TOPIC = “/habib/arduino/state”; // Topic to subcribe to

IPAddress broker(192,168,2,102); // IP address of your MQTT broker eg. 192.168.1.50
WiFiClient wclient;

PubSubClient client(wclient); // Setup MQTT client
bool state=0;

// Connect to WiFi network
void setup_wifi() {
Serial.print("\nConnecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password); // Connect to network

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

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

// Reconnect to client
void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Attempt to connect
if (client.connect(ID)) {
Serial.println(“connected”);
Serial.print("Publishing to: ");
Serial.println(TOPIC);
Serial.println(’\n’);

} else {
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
}

}
}

void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud
pinMode(SWITCH_PIN,INPUT); // Configure SWITCH_Pin as an input
pinMode(LIGHT_PIN, OUTPUT); // Configure LIGHT_PIN as an output
digitalWrite(SWITCH_PIN,HIGH); // enable pull-up resistor (active low)
delay(100);
setup_wifi(); // Connect to network
client.setServer(broker, 1883);
}

void loop() {
if (!client.connected()) // Reconnect if connection is lost
{
reconnect();
}
client.loop();

// if the switch is being pressed
if(digitalRead(SWITCH_PIN) == 0)
{
state = !state; //toggle state
if(state == 1) // ON
{
client.publish(TOPIC, “on”);
Serial.println((String)TOPIC + " => on");
}
else // OFF
{
client.publish(TOPIC, “off”);
Serial.println((String)TOPIC + " => off");
}

while(digitalRead(SWITCH_PIN) == 0) // Wait for switch to be released
{
  // Let the ESP handle some behind the scenes stuff if it needs to
  yield(); 
  delay(20);
}

}
}

void callback(char* topic, byte* payload, unsigned int length) {
String response;

for (int i = 0; i < length; i++) {
response += (char)payload[i];
}
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
Serial.println(response);
if(response == “on”) // Turn the light on
{
digitalWrite(LIGHT_PIN, HIGH);
client.publish(STATE_TOPIC,“on”);
}
else if(response == “off”) // Turn the light off
{
digitalWrite(LIGHT_PIN, LOW);
client.publish(STATE_TOPIC,“off”);
}
}

Hi @habib,
on first sight the code seems ok. Are you sure the ID is unique?
You use the same ID in mqtt.fx and arduino… I’ve testet this scenario and the broker reject the connection.

Yes man, I tested again with different ID such as ID in MQTT fx: openHAB2 and program: openHABIB, Still same problem.

I did but then MQTT connection failed. It said: “MQTT Exception”. I also wrote there localhost, but still it is not resolved. Thanks

Hi @habib,
the IPAddress Constructor is correct.
I tried your code with an ESP8266 laying around and it runs fine.

Connecting to Networkname
.......
WiFi connected
IP address: 192.168.xxx.61
Attempting MQTT connection…connected
Publishing to: /habib/arduino/command

so I’ve no idea, what can be wrong.
Can you see an logfile from your MQTT Broker?

I think there is no firewall between your ESP32 and the broker?
Or did you run the MQTT.fx Client on same host with the broker and you have enabled an firewall that prevent the esp32 from connecting?

Waow,…Thanks my friend, I am excited to see “MQTT is connected now”

Thankful to you. Serial

My openHAB was working since last one month but yesterday suddenly showed this error “MQTT broker: OFFLINE-COMMUNICATION ERROR” and even mqtt.fx is not connected. I cleared the Cache and tmp file (inside). Please help me what can I do?