Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mqtt client fail to connect (mosquitto broker) #864

Open
waxore opened this issue May 19, 2021 · 2 comments
Open

mqtt client fail to connect (mosquitto broker) #864

waxore opened this issue May 19, 2021 · 2 comments

Comments

@waxore
Copy link

waxore commented May 19, 2021

Hello everybody i'm currently working on a IOT project based on an esp32 that needs to be connected to a local mqtt broker and i have encounter a problem with the connection to the broker ,after some googling i have found that it can be a firewall problem so i had open the 1883 port
for the incoming traffic but nevertheless nothing changed the connection between the broker and other interfaces like node red or mqtt explorer is working fine
i have made some test to see if any of the ports is opened to other clients on the network but i get that all the ports is closed
also from my search on the net i remarked that the mosquitto folder does not have the proper config file where i can for example change the port or modify the authentication settings
can anyone help me to solve this problem i already tried some suggestions from #185 but nothing worked
her is my esp32 code :

#include <WiFi.h>
#include <PubSubClient.h>

int i;
WiFiClient espClient;
PubSubClient client(espClient);
void reconnectmqttserver() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}

char msgmqtt[50];
void callback(char* topic, byte* payload, unsigned int length) {
String MQTT_DATA = "";
for (int i=0;i<length;i++) {
MQTT_DATA += (char)payload[i];}

}

void setup()
{
i = 0;
Serial.begin(9600);

WiFi.disconnect();
delay(3000);
Serial.println("START");
WiFi.begin("tp_link","01020304050");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
Serial.print("..");
}
Serial.println("Connected");
Serial.println("Your IP is");
Serial.println((WiFi.localIP()));
}

void loop()
{

if (!client.connected()) {
reconnectmqttserver();
}
client.loop();
client.setServer("192.168.137.1", 1883);
client.setCallback(callback);
snprintf (msgmqtt, 50, "%d ",i);
client.publish("state", msgmqtt);
i = i + 1;
delay(1000);

}

@waxore
Copy link
Author

waxore commented May 19, 2021

i have just run a test using Zenmap to scan for the default mqtt port 1883 the result was that the port was filtered , and from my search i understand that this state means there is a network obstacle blocking the port , any clue what should i search for ?

@tuan-karma
Copy link

The problem is new version of mosquitto broker, it is dafault not allowing to connect from outside (other than the localhost loopback). The solution is you need to edit the mosquitto.conf file to allowing listeners from other machine. See details: https://stackoverflow.com/questions/65278648/mosquitto-starting-in-local-only-mode/65278769#65278769

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants