Arduino esp8266 not connecting to MqttClient via client.connect(mqttDeviceID)


I’m attempting to use the blinds control .ino sketch, altering it a bit for a relay instead of a servo.

I set up Serial.prints to debug since mqtt.fx publishing to my topic wasn’t working

There seems to be a hang-up in the void connect() function, specifically the while loop testing for !client.connect(mqttDeviceID)

I set up the correct libraries according to the door sensor video, tried changing the mqttDeviceID, double checked my ip address, not sure what else to do.

Any advice on how to proceed would be great.

Here is my .ino file

/Only edit the settings in this section

/* WIFI Settings */
// Name of wifi network
const char* ssid = "*********";

// Password to wifi network
const char* password = "*********************"; 

/* Web Updater Settings */
// Host Name of Device
const char* host = "SpeakerSwitch1";

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = "/firmware";

// Username to access the web update page
const char* update_username = "******";

// Password to access the web update page
const char* update_password = "*********";

/* MQTT Settings */
// Topic which listens for commands
char* subscribeTopic = "home/bedroom/speakers"; 

//MQTT Server IP Address
const char* server = "";

//Unique device ID 
const char* mqttDeviceID = "SpeakerSwitch1"; 

/* Esp8266 pins */
const int relayPin = 4;  // Active high
const int ledPin = 2;  // Active low

/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */

ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect();

void setup() 
  Serial.println("Booting Wifi");
  WiFi.begin(ssid, password);
  client.begin(server, net);



  httpUpdater.setup(&httpServer, update_path, update_username, update_password);

  MDNS.addService("http", "tcp", 80);

void connect() 
  while (WiFi.status() != WL_CONNECTED) 
    Serial.println("Wifi connecting...");
  Serial.println("Wifi CONNECTED");
  while (!client.connect(mqttDeviceID)) 
    Serial.println("Mqtt connecting...");
  Serial.println("Mqtt CONNECTED");

void loop() 

    Serial.println("Mqtt Client connected");

void messageReceived(String &topic, String &payload) 
  String msgString = payload;
  if (msgString == "ON")
    Serial.println("messageRecieved: ON");
    digitalWrite(ledPin, LOW);      // LED is active-low, so this turns it on
    digitalWrite(relayPin, HIGH);
  else if (msgString == "OFF")
    Serial.println("messageRecieved: OFF");
    digitalWrite(ledPin, HIGH);     // LED is active-low, so this turns it off
    digitalWrite(relayPin, LOW);            
  else if (isValidNumber(msgString))
    Serial.print("Invalid Mqtt Message Format");         
    Serial.print("Error Reading Mqtt Message");            

boolean isValidNumber(String str)
   boolean isNum=false;
   if(!(str.charAt(0) == '+' || str.charAt(0) == '-' || isDigit(str.charAt(0)))) return false;

   for(byte i=1;i<str.length();i++)
       if(!(isDigit(str.charAt(i)) || str.charAt(i) == '.')) return false;
   return true;


I figured out that while setting up openhabian and installing the mosquitto broker, I had set a password.
Looking at the connect() function on github found here:

… shows that the function can take the clientId, as well as optional username and password.
changing the function to:
while (!client.connect(mqttDeviceID, mosquitto_username, mosquitto_password))
…fixed the connection and was checked with mqtt.fx

leaving up in case anyone else runs across this issue.