Can't connect ESP32 to MQTT Broker (Mosquitto)

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

Can't connect ESP32 to MQTT Broker (Mosquitto)

Postby Pyshco » Mon Jul 31, 2017 9:33 pm

I'm trying to follow this tutorial from IoT Sharing: http://www.iotsharing.com/2017/05/how-t ... esp32.html. Now the thing is, my ESP32 does not connect to the MQTT Broker, always shows a error message in the Serial Monitor. I don't know what i'm doing wrong, can you guys help me?!

PD: I ignored the part of the temperature thing, is not on the code.

Code Here:

Code: Select all


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


/* change it with your ssid-password */
const char* ssid     = "CLAROQNK37";
const char* password = "9j98g4NuEaq496Yk";
/* this is the IP of PC/raspberry where you installed MQTT Server 
on Wins use "ipconfig" 
on Linux use "ifconfig" to get its IP address */
const char* mqtt_server = "10.0.0.5";

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

/*LED GPIO pin*/
const char led = 4;

/* topics */
#define LED_TOPIC "Bulb/Room1"/*Bulb/Room1*/ /* 1=on, 0=off */

long lastMsg = 0;
char msg[20];

void receivedCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message received: ");
  Serial.println(topic);

  Serial.print("payload: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  /* we got '1' -> on */
  if ((char)payload[0] == '1') {
    digitalWrite(led, HIGH); 
  } else {
    /* we got '0' -> on */
    digitalWrite(led, LOW);
  }

}

void mqttconnect() {
  /* Loop until reconnected */
  while (!client.connected()) {
    Serial.print("MQTT connecting ...");
    /* client ID */
    String clientId = "ESP32Client";
    /* connect now */
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      /* subscribe topic with default QoS 0*/
      client.subscribe(LED_TOPIC);
    } else {
      Serial.print("failed, status code =");
      Serial.print(client.state());
      Serial.println("try again in 5 seconds");
      /* Wait 5 seconds before retrying */
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  /* set led as output to control led on-off */
  pinMode(led, OUTPUT);

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

  /* configure the MQTT server with IPaddress and port */
  client.setServer(mqtt_server, 1883);
  /* this receivedCallback function will be invoked 
  when client received subscribed topic */
  client.setCallback(receivedCallback);
}
void loop() {
  /* if client was disconnected then try to reconnect again */
  if (!client.connected()) {
    mqttconnect();
  }
  /* this function will listen for incomming 
  subscribed topic-process-invoke receivedCallback */
  client.loop();
  }

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Can't connect ESP32 to MQTT Broker (Mosquitto)

Postby tele_player » Tue Aug 01, 2017 12:23 am

I just tried the code from the tutorial, modified only to comment out the DHT code, and use a random number for temperature. Installed mosquitto on a Raspberry Pi. Installed android app on my phone.

Works fine here, temperature updates display correctly on my phone. No error messages in serial monitor.

I'll try to debug your code next, now that I know my setup works.

Edit: I ran your code, works here.

Have you entered the correct IP address and port for you mosquito server? Are you sure mosqutto is running? What errors are you seeing?

User avatar
Pyshco
Posts: 28
Joined: Wed Jul 19, 2017 2:36 pm

Re: Can't connect ESP32 to MQTT Broker (Mosquitto)

Postby Pyshco » Tue Aug 01, 2017 1:28 pm

My mistake was on the port, mosquitto doesn't have the default port and i change it for 1993, thanks for all!

Who is online

Users browsing this forum: Bing [Bot] and 76 guests