MQTT is not working in an specific wifi network

I'm working with an IOT project where i have to connect an ESP32 in a MQTT server, when i was testing the module in my networking wifi home it was working very well but when i change the wifi connection to another network that is not this of my home, the MQTT not connect with thw device anymore, just the new wifi netwoking is connecting. What can i do to solve this??

Where is the MQTT server? Perhaps the firewall at your second location won't let you talk to it.

Here is the part of the code corresponding to wifi and mqtt:

const char* ssid = "as";
const char
password = "v
const char
username = "";
const char
mqttServer = "t
unsigned int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";

WiFiClient espClient;
PubSubClient client(espClient);

DFRobotHCHOSensor hcho(SensorAnalogPin,VREF);

void setup() {

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi..");

Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

if (client.connect("***", mqttUser, mqttPassword )) {


} else {

  Serial.print("failed with state ");



And this is the result:

state -2

If you post your code as described in How to get the best out of this forum, more forum members will read it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.