Nodemcu cannot connect to mqtt

I am new to nodered and mqtt , i follow all the steps shows on youtube videos to connect nodemcu to nodered via mqtt broker but failed.

kindly see the attached pictures ..

  1. The Nodered is running on windows 10 laptop
  2. MQTT installed on laptop as well as in nodered
  3. Through Cmd command I can see mqtt is listening and connected
  4. In nodered flow I can see mqtt is connected.

But In arduino serial monitor I can see that mqtt is trying to connect to but failed.
I not understand why ?

Add the code you are running on the nodemcu so we can see what hostname/ip address you are using.

Its a simple code to read a temperature from nodemcu

Be careful - you are showing your WiFi credentials in the screen-shots !!!

Please don't post photos of your screen, copy and paste the text and use the toolbar to format it, that image is very hard to read.

It is also missing all the code that actually connects to the broker.

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

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "192.168.43.160"; ///MQTT Broker
int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  // Start the Serial Monitor
  Serial.begin(115200);
  sensors.begin();
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to");
  Serial.println(ssid);
  // Connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  Serial.println("In reconnect...");
  while (!client.connected()) {
  Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void loop() {
  char msg[10];
  char msgtext[25];
  String themsg;
  sensors.requestTemperatures(); 
  float celsius = sensors.getTempCByIndex(0);
  Serial.println(sensors.getTempCByIndex(0));
  char temperaturenow [15];
  dtostrf(celsius,7,3,temperaturenow); // convert floar to char
  client.publish("motor/temperature", temperaturenow);
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  delay(1000);
}

Your script works for me on a Wemos D1 Mini publishing to my MQTT broker (192.168.1.156:1883) on a Raspberry Pi. I removed the code for the DS18B20, as I hadn't got one to hand, and just put in a constant (21.6) to simulate the temperature. Looks like the issue could be with your network.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// #include <OneWire.h>
// #include <DallasTemperature.h>

// GPIO where the DS18B20 is connected to
//const int oneWireBus = 4;
//OneWire oneWire(oneWireBus);
//DallasTemperature sensors(&oneWire);
const char* ssid = "Teamwork";
const char* password = "***********";
const char* mqtt_server = "192.168.1.156"; ///MQTT Broker
int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  // Start the Serial Monitor
  Serial.begin(19200);
  //sensors.begin();
  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  // Connecting to a WiFi network
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  Serial.println("In reconnect...");
  while (!client.connected()) {
  Serial.println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void loop() {
  // char msg[10];
  //char msgtext[25];
  //String themsg;
  //sensors.requestTemperatures(); 
  //float celsius = sensors.getTempCByIndex(0);
  //Serial.println(sensors.getTempCByIndex(0));
  //char temperaturenow [15];
  //dtostrf(celsius,7,3,temperaturenow); // convert floar to char
  client.publish("motor/temperature", "21.6");
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  delay(1000);
}

Screen Shot 01-02-21 at 11.14 AM
Screen Shot 01-02-21 at 11.14 AM 001
Screen Shot 01-02-21 at 11.15 AM

So is 192.168.43.160 the address of the windows machine?

are you using 192.168.43.160 as the 'server' address in the mqtt-in node?

Have you trid adding an mqtt-out node andpublishing something to see that the message will arrive back in NR?

exactly my point .. if i use just use a serial monitor the code works fine.. but if i want to use it with mqtt broker and want to see the reading on nodered .. nodemcu shows reconnecting.. i new with nodered but work with arduino ide before so that's why i am confused.. i follow all the steps

yes 192.168.43.160 is the address of windows on which nodered and mqtt working..

and i know that address by cmd command ipconfig..

and i not tried mqtt-out node .. can you you explain me how ? i am new to nodered..

i have seen your previous replies to other problems thats why i came to know that i have to use the ip of the machine , before i was using the 127.0.... haha..

I thought the same that it could be network issue .. but how i find it .. i tried methods available on internet .. i have even done the handshake of mqtt on two cmd windows and work fine

As suggested earlier try using the full ip address in an mqtt In node in node-red to see if it connects (use the full ip address not localhost so it will go through the network stack).
If that doesn't work, does it work with localhost specified?

Also are you able to ping the nodemcu from the windows machine?

Is there a firewall running on your windows box that is blocking external access - (there should be :slight_smile: - you have to allow tcp port 1883 to be open.

i used the following command in cmd

"netstat -an | findstr 1883"

the following status come,

TCP 127.0.0.1:1883 0.0.0.0.0 LISTENING
TCP [::1] :1883 [::] :0 LISTENING

Just because it's listening, doesn't mean as dceejay said there may be a firewall blocking external access to the port.

You need an inject, mqtt-out, mqtt-in and a debug node (set to display the complete msg object`

  • connect the inject to the mqtt-out node.
  • edit the mqtt-out and set the topic to 'my/test'
  • enter the server IP address (the 192.168.... not tht 127.0.... address) and port 1883
  • connect the mqtt-in node to the debug node
  • edit the mqtt-in node and set the topic to 'my/#' (more about the # later)
  • select the server (the same one used in the mqtt-out node

Now do a deploy. You should see both mqtt nodes with a green dot and the word 'connected' under them. If you don't then you ca't get to the MQTT broker.

If you see the green dot and test it you should see the debug node output the time in msg payload and msg.topic

No result but mqtt shows connected ,
In Mqtt Server I write local host .. Is that could be problem ?

In firewall I already allow inbound rules and outbound rules but no success

you need to use 192.168.43.160 (the address of the device running the MQTT broker) not localhost in the mqtt nodes served setting
Screen Shot 2021-01-04 at 4.55.14 AM

great .. there is a problem .. after i changed the server from localhost to ip address ..
the connection got disconnected ...

Looks like something with the connection .