Esp32 and MQTT (Raspberry pi) connection issue

bilal_93
Posts: 1
Joined: Fri Jun 18, 2021 5:50 pm

Esp32 and MQTT (Raspberry pi) connection issue

Postby bilal_93 » Fri Jun 18, 2021 6:30 pm

Hello, I am trying to connect Esp32 to MQTT broker(Raspberry Pi) via Wi-Fi but I am facing issues. I am trying to send "Hello" from Esp32 to MQTT. Upon checking, Esp32 IP Address is: "192.168.0.105", Raspberry Pi IP: "192.169.0.106" and Wi-Fi IP is: "192.168.0.1". I don't know if this is the issue or something else. I am also posting the code below, kindly tell me what am I doing wrong or how can I solve this issue? Thank you.
  1. [code]
  2. #include <HardwareSerial.h>
  3. #include <PubSubClient.h> //Connect and publish data to the MQTT broker
  4. #include <WiFi.h> //Enables Esp32 to connect to the local network(via WiFi)
  5. #define RXD2 16
  6. #define TXD2 17
  7. //char myRcvChar;
  8. //WiFi
  9. const char* ssid = "Bilal-Asad-310";
  10. const char* wifi_password = "31019309";
  11. //MQTT
  12. const char* mqtt_server = "192.168.0.106";
  13. const char* message_topic = "test/esp32/message";
  14. //const char* bending_topic = "exoskeleton/walking/bending";
  15. //const char* streching_topic = "exoskeleton/walking/streching";
  16. const char* mqtt_username = "pi"; //MQTT username
  17. const char* mqtt_password = "raspberry"; //MQTT password
  18. const char* clientID = "first message";
  19.  
  20. //Initialise the Wifi and MQTT Client objects
  21. WiFiClient wifiClient;
  22. //1883 is the listener port for the broker
  23. PubSubClient client(mqtt_server,1883,wifiClient);
  24.  
  25.  
  26. //Custom function to connect to the MQTT broker via WiFi
  27. void connect_MQTT(){
  28.   Serial.print("Connecting to ");
  29.   Serial.println(ssid);
  30.  
  31.   //Connect to the WiFi
  32.   WiFi.begin(ssid, wifi_password);
  33.  
  34.   //wait until the connection has been confirmed before continuing
  35.   while(WiFi.status()!= WL_CONNECTED){
  36.     delay(500);
  37.     Serial.print(".");
  38.   }
  39.   //Debugging-Output the IP Address of the ESP32
  40.   Serial.println("WiFi connected");
  41.   Serial.println("IP Address");
  42.   Serial.println(WiFi.localIP());
  43.  
  44.   //Connect to MQTT Broker
  45.   //client.connect returns a boolean value to let us know if the connection was successful
  46.   //if the connection is failing, make sure you are using the correct MQTT Username and Password
  47.   if(client.connect(clientID,mqtt_username,mqtt_password)){
  48.     Serial.println("Connected to MQTT Broker");
  49.   }
  50.   else{
  51.     Serial.println("Connection to MQTT Broker failed...");
  52.   }
  53. }
  54.  
  55. void setup() {
  56.   // put your setup code here, to run once:
  57.   Serial.begin(115200);
  58.   Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  59.  //char myRcvchar;
  60.  
  61.  
  62. }
  63.  
  64. void loop() {
  65.   // put your main code here, to run repeatedly:
  66.   connect_MQTT();
  67.   Serial.setTimeout(2000);
  68.   Serial.println("Data Received: ");
  69.   Serial.println(Serial2.readStringUntil('\n'));
  70.  
  71.   const char* message = "Hello";
  72.   Serial.print("Message Received: ");
  73.   Serial.print(message);
  74.   //MQTT can only transmit strings
  75.   String ms = "Mess: "+String((char*)message);
  76.  
  77.   //Publish to MQTT broker
  78.   if(client.publish(message_topic, String(message).c_str())){
  79.     Serial.println("Message sent");
  80.   }
  81. //Again client.publish will return boolean value depending on whether it succeded or not.
  82. //if the message failed to send, try again, as the connection might have broken
  83. else{
  84.   Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
  85.   client.connect(clientID,mqtt_username,mqtt_password);
  86.   delay(10); //This delay ensures that client.publish does not clash with client.connect call
  87.   client.publish(message_topic,String(message).c_str());
  88. }
  89. client.disconnect(); //disconnect from the MQTT broker
  90. delay(1000*60); //print new values every 2 minutes
  91.  
  92.  
  93. }
  94. [/code]

Who is online

Users browsing this forum: Google [Bot] and 111 guests