[SOLVED] MQTT not connecting to nodemcu

I installed mosquitto on my ubuntu 18.04 and uploaded the following code into my nodemcu using Arduino IDE.

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

#define MQTT_SERVER ""
const char* ssid = "wifi";
const char* password = "*******";

//LED on ESP8266 GPIO2
const int lightPin = 2;

char* lightTopic = "/test/light1";

WiFiClient wifiClient;
void callback(char* topic, byte* payload, unsigned int length);
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);

void setup() {
  //initialize the light as an output and set to LOW (off)
  pinMode(lightPin, OUTPUT);
  digitalWrite(lightPin, LOW);

  //start the serial line for debugging

  //start wifi subsystem
  WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server

  //wait a bit before starting the main loop

void loop(){

  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {reconnect();}

  //maintain MQTT connection

  //MUST delay to allow ESP8266 WIFI functions to run

void callback(char* topic, byte* payload, unsigned int length) {

  //convert topic to string to make it easier to work with
  String topicStr = topic; 

  //Print out some debugging info
  Serial.println("Callback update.");
  Serial.print("Topic: ");

  //turn the light on if the payload is '1' and publish to the MQTT server a confirmation message
  if(payload[0] == '1'){
    digitalWrite(lightPin, HIGH);
    client.publish("/test/confirm", "Light On");


  //turn the light off if the payload is '0' and publish to the MQTT server a confirmation message
  else if (payload[0] == '0'){
    digitalWrite(lightPin, LOW);
    client.publish("/test/confirm", "Light Off");


void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
    Serial.print("Connecting to ");

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {

    //print out some more debug once connected
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if(WiFi.status() == WL_CONNECTED){
  // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      // Generate client name based on MAC address and last 8 bits of microsecond counter
      String clientName;
      clientName += "esp8266-";
      uint8_t mac[6];
      clientName += macToStr(mac);

      //if connected, subscribe to the topic(s) we want to be notified about
      if (client.connect((char*) clientName.c_str())) {
        Serial.print("\tMTQQ Connected");

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}

//generate unique name from MAC addr
String macToStr(const uint8_t* mac){

  String result;

  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);

    if (i < 5){
      result += ':';

  return result;

When I see on the serial monitor, the nodemcu is connecting to my wifi and showing this massage


Ip address given in code and the ip shown in the serial monitor is different. Please help me with this

#define MQTT_SERVER ""
const char* ssid = "wifi";
const char* password = "*******";

Your need to uncomment the DEFINE MQTT_SERVER line and put in the IP address of your mqtt broker/server

#define is not a comment

Yes sorry, long time I touched my arduino code. in the nodemcu
On what IP is your mqtt broker?

Also, remove the starting / from your topics
It adds a level to the topics and can lead to errors and confusion

char* lightTopic = "test/light1";
    client.publish("test/confirm", "Light On");
    client.publish("test/confirm", "Light Off");

I am not an exprienced in coding I am an Electrical engineer actually. I installed mosquitto following theses instructions here https://www.vultr.com/docs/how-to-install-mosquitto-mqtt-broker-server-on-ubuntu-16-04

And not set any passwords to it

I dont know on what IP the mqtt broker is. How can I know that?

On which computer did you install mosquitto on?

I installed it on my ubuntu computer

In your computer terminal enter ifconfig
It will give you your computer IP address in your lan. It should be something like 192.168.x.x

Yes I got one with and another one with

Put in your nodeMCU always means “THIS DEVICE” so for your computer in the computer itself. You can also use localhost. Same thing.

The nodeMCU will NOT understand as it is NOT a computer with an OS with IP addresses resolving utilities. It’s only a microcontroller…

It failed again with same massage

Is mosquitto running?

how can I ensure of that?

Go back to:

Yes it is running

Did you change you line to

#define MQTT_SERVER ""

What IP address does the NodeMCU says it is? (The one you hid)
It’s inside your lan - no one can use that info

It says
I think I already tested it once. But let me check it once

It failed again