Nodemcu cannot connect to mqtt

Since I don't have a windows computer, there is not much else I can offer except suggest you install mosquitto on another device and see if you can connect to that device with it's IP address (a Pi would work)

I appreciate .. Thanks

Can you check, that your mosquitto.conf file contains the following lines:

allow_anonymous true
listener 1883

If not, add these.

Regards

1 Like

Before the config file is like
before edit (2)

I edit as you said

But still port is listening to 127.0.0.1 , not the ip address of machine ,still i am unable to connect node-red mqtt flow if i use machine ip as mqtt server. still fail

port 1883 listening (2)

Hi .. thats not how Firewizard52 wrote the two commands
Lose the # infront of the commands, as these comment out the line.

allow_anonymous true
listener 1883

after that restart your mosquitto server and try reconnecting with the 192.168.43.160 ip

Oh sorry .. my mistake yeah that's works fine after editing and got connected to ip address.

But on arduino side nodemcu still cannot connect to mqtt broker..

serial monitor

Here is the arduino program ,

#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 = "xxxxxx";
const char* password = "xxxxxx";
const char* mqtt_server = "192.168.12.140"; ///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);
}

I dont know much about arduino but it seems that you need to fill in your wireless network information.
the SSID and password .. (unless you xxxxxx it just for the post)

ps. and make sure the wireless network is in the same ip range as your mqtt broker

I don't think it is your program (unless you have chnaged it) as I checked it (with the DS18b20 removed) on my setup and it worked fine. See post number 6 and 7 in this thread.
I'm sure it is a networking issue - but (sorry) I have no idea what.

I think he has put in the "xxxxx" to hide the ssid and password.

this i remove for safety reason .. offcourse i fill the wireless network detail.. but thanks for the help in connecting the broker

yes yes code is ok .. i still suffering for connectivity problem

what ip range do you get from your wireless network ?
is it the same range 192.168.12.x as the mqtt broker ?

no mqtt broker is on different wifi and nodemcu is connected to different wifi

When I ran your program the other day my Wemos got assigned an IP address from the router.
Is that bit working on your setup as I haven't seen that in any of the listings in your posts??

ok .. so thats the problem .. different networks
can you connect the arduino on the same wireless as the broker ?

yes when nodemcu connect to wifi , any ip has assigned to it.. you haven't seen because it moves up with continous alarm ..

You can turn-off scrolling in the serial monitor window so you can see what happens at the start of the sequence and make sure an IP in the correct range is being assigned.

what is meant by correct ip , nodemcu have to connect to mqtt broker(which have a fix ip) , does that matter what ip nodemcu have ?

The Wemos or NodeMCU needs an IP to enable it to 'talk' to the network. It doesn't matter what IP is assigned to it... providing it is unique (doesn't clash with another device on the network) and is in the same domain-range as the rest of the system.

As you say, such a setup will not work (unless you setup a VPN between the networks). Alternatively, @sam_r could use a mqtt cloud service that both nodemcu and Node-RED would connect to