Esp32 and MQTT not connecting

Hello all,
I am trying to connect Esp32 to the MQTT broker(Raspberrry Pi) but I am having issues in it. I am trying to send a simple "Hello" message to the MQTT broker via Wi-Fi but it does not work. Upon checking the IP Address of Esp32 it is "192.168.0.105" and the raspberry pi IP is "192.168.0.106" and the Wi-Fi IP is "192.168.0.1". I am confused that maybe this is the error because of IP adressed or is it something else. Esp32 does connects to the Wi-Fi and so does the Raspberry Pi with MQTT broker but on different IPs. MQTT broker is also running fine on RPi, but I don't know where the issue is. Can anyone help me with this?

I am posting the Esp32 code below:

#include <HardwareSerial.h>
#include <PubSubClient.h> //Connect and publish data to the MQTT broker
#include <WiFi.h> //Enables Esp32 to connect to the local network(via WiFi)
#define RXD2 16
#define TXD2 17
//char myRcvChar;
//WiFi
const char* ssid = "xxxxxxxxxxx";
const char* wifi_password = "xxxxxx";
//MQTT
const char* mqtt_server = "192.168.0.106";
const char* message_topic = "test/esp32/message";
//const char* bending_topic = "exoskeleton/walking/bending";
//const char* streching_topic = "exoskeleton/walking/streching";
const char* mqtt_username = "pi"; //MQTT username
const char* mqtt_password = "raspberry"; //MQTT password
const char* clientID = "first message";

//Initialise the Wifi and MQTT Client objects
WiFiClient wifiClient;
//1883 is the listener port for the broker
PubSubClient client(mqtt_server,1883,wifiClient);


//Custom function to connect to the MQTT broker via WiFi
void connect_MQTT(){
  Serial.print("Connecting to ");
  Serial.println(ssid);

  //Connect to the WiFi
  WiFi.begin(ssid, wifi_password);

  //wait until the connection has been confirmed before continuing
  while(WiFi.status()!= WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  //Debugging-Output the IP Address of the ESP32
  Serial.println("WiFi connected");
  Serial.println("IP Address");
  Serial.println(WiFi.localIP());

  //Connect to MQTT Broker
  //client.connect returns a boolean value to let us know if the connection was successful
  //if the connection is failing, make sure you are using the correct MQTT Username and Password
  if(client.connect(clientID,mqtt_username,mqtt_password)){
    Serial.println("Connected to MQTT Broker");
  }
  else{
    Serial.println("Connection to MQTT Broker failed...");
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
 //char myRcvchar;


}

void loop() {
  // put your main code here, to run repeatedly:
  connect_MQTT();
  Serial.setTimeout(2000);
  Serial.println("Data Received: ");
  Serial.println(Serial2.readStringUntil('\n'));

  const char* message = "Hello";
  Serial.print("Message Received: ");
  Serial.print(message);
  //MQTT can only transmit strings
  String ms = "Mess: "+String((char*)message);

  //Publish to MQTT broker
  if(client.publish(message_topic, String(message).c_str())){
    Serial.println("Message sent");
  }
//Again client.publish will return boolean value depending on whether it succeded or not.
//if the message failed to send, try again, as the connection might have broken
else{
  Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
  client.connect(clientID,mqtt_username,mqtt_password);
  delay(10); //This delay ensures that client.publish does not clash with client.connect call
  client.publish(message_topic,String(message).c_str());
}
client.disconnect(); //disconnect from the MQTT broker
delay(1000*60); //print new values every 2 minutes
  

}

The MQTT broker username and password are not usually the same as the RPi root user.
This link should help with creating a user/password for your MQTT broker.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.