knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.83k stars 1.47k forks source link

mqtt client fail to connect (mosquitto broker) #864

Open waxore opened 3 years ago

waxore commented 3 years ago

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

include

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 commented 3 years ago

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 commented 1 year ago

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