# Nodemcu cannot connect to mqtt

**URL:** <https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384>\
**Category:** General\
**Created:** [2 January 2021 08:43 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384 "2021-01-02T08:43:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 08:43 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/1 "2021-01-02T08:43:32Z")

</div>

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 ?

 ![nodemcu cannot connect to mqtt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/432fab9717e5bd4110b7cf9013381fcb328e0036.jpeg)

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [2 January 2021 08:44 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/2 "2021-01-02T08:44:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 09:01 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/3 "2021-01-02T09:01:17Z")

</div>

Its a simple code to read a temperature from nodemcu

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [2 January 2021 09:04 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/4 "2021-01-02T09:04:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [2 January 2021 09:34 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/7 "2021-01-02T09:34:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 10:20 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/8 "2021-01-02T10:20:30Z")

</div>

```auto
#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);
}

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [2 January 2021 11:23 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/9 "2021-01-02T11:23:20Z")

</div>

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.

```auto
#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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/326a641d82f6f1ad0bb3cd398177f54c5cc229d5.jpeg)  
 ![Screen Shot 01-02-21 at 11.14 AM 001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41823ee23134fa71375127893131a40695d8ca47.jpeg)  
 ![Screen Shot 01-02-21 at 11.15 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c8e61f6f9cc05ce7dca783e48011e29a0ee56ba.jpeg)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 January 2021 12:26 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/10 "2021-01-02T12:26:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 13:49 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/11 "2021-01-02T13:49:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 13:52 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/12 "2021-01-02T13:52:26Z")

</div>

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..

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [2 January 2021 13:54 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/13 "2021-01-02T13:54:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 January 2021 13:59 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/14 "2021-01-02T13:59:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 January 2021 14:07 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/15 "2021-01-02T14:07:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [3 January 2021 01:50 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/16 "2021-01-03T01:50:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [3 January 2021 09:08 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/17 "2021-01-03T09:08:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 January 2021 10:44 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/19 "2021-01-03T10:44:16Z")

</div>

> [@sam\_r](#):
>
> i not tried mqtt-out node .. can you you explain me how ? i am new to nodered..

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

 ![Screen Shot 2021-01-03 at 5.39.43 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f555307dfcedd5dfebef427112ec6d3f996ab290.png)

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [4 January 2021 09:36 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/20 "2021-01-04T09:36:06Z")

</div>

![test](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/0284d44eda9f63aba9b26b9cf47a4fe69c126312.jpeg)

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

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [4 January 2021 09:38 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/21 "2021-01-04T09:38:15Z")

</div>

![IMG_20210103_164127](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a9ad17c568bd94d936de5330df1190bba3d71e2d.jpeg)

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 January 2021 09:55 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/22 "2021-01-04T09:55:46Z")

</div>

> [@sam\_r](#):
>
> Mqtt Server I write local host .. Is that could be problem

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4ca5f40dc9dded28c125b5c1b043aa1ff37e97c.png)

---

<div class="post-metadata">

**Author:** ![sam\_r](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sam\_r](https://discourse.nodered.org/u/sam_r)\
**Post date:** [4 January 2021 12:43 UTC](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384/23 "2021-01-04T12:43:39Z")

</div>

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 .

 ![IMG_20210104_193418](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/420365612c1bb8547251b2e6ddb92c032f999518.jpeg) ![IMG_20210104_193701](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f267f961817e1adbac22248d7d532653e691287.jpeg)

[Next page](https://discourse.nodered.org/t/nodemcu-cannot-connect-to-mqtt/38384.md?page=2)
