Can't connect ESP32 to MQTT Broker (Mosquitto)

User avatar
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: ... 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 = "";

/* 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.print("payload: ");
  for (int i = 0; i < length; i++) {
  /* 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())) {
      /* subscribe topic with default QoS 0*/
    } else {
      Serial.print("failed, status code =");
      Serial.println("try again in 5 seconds");
      /* Wait 5 seconds before retrying */

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

  WiFi.begin(ssid, password);

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

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

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

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
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: No registered users and 24 guests