Help with mqtt (maybe something else)

Hi All,
Need your help once more.
I had a perfect working sketch, everything working.
Made a bad connection and fried my ESP :frowning:
I bought a new one, is a esp32-s2-saola-1.

I think this problem is something else than mqtt, but can't figure what!!
The sketch below is working fine, but only because the server is local. Any server I try outside my network doesn't work, just keep trying.

It's driving me crazy ... again :slight_smile:

Here's the code:

////////////////////////////
//INCLUDE
#include "libs.h"

///////////////////////////
// Add your MQTT Broker IP address, example:
//=======================================================================
//                   START MQTT SERVER
//=======================================================================
const char* mqtt_server = "192.168.121.22";
const char* mqtt_name = "WeatherStation";
const char* mqtt_weather = "weather/#";
const char* mqtt_tasmota = "stat/tasmota_xxxxx/POWER";
const char* mqttuser = "xxxxxxxx";
const char* mqttpass = "xxxxxxxxxxx";
const int mqttPort = 1883;
WiFiClient wifiClient;
PubSubClient client(mqtt_server, mqttPort, wifiClient);
long timeToReconnect = 0;
String rxString = "";
String mqttComando;
//long timerRequest;
String mqttReqst;
//Mqtt
const long intervalleitura = 4000;  //4 Seg entre leituras e envio ao mqtt e LCD
unsigned long previousMillisleitura = 0;
String mqttmess = "";

//=======================================================================
//                    MQTT CALLBACK
//=======================================================================
void callback(char* mqtt_topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(mqtt_topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) rxString += (char)payload[i];
  Serial.print("(");
  Serial.print(rxString);
  Serial.println(")");
  rxString = "";
  mqttComando = mqtt_topic;
}

void connectMosquitto() {
  client.setServer(mqtt_server, mqttPort);
  client.setCallback(callback);
  Serial.println("");
  Serial.println("Conecting MOSQUITTO Broker...");
  if (client.connect(mqtt_name, mqttuser, mqttpass)) {
    Serial.println("Connected to MOSQUITTO Broker!");
    Serial.println("");
    Serial.print("I have subscribe ");
    Serial.print(mqtt_weather); Serial.print(" e "); Serial.print(mqtt_tasmota);
    client.subscribe(mqtt_weather);
    client.subscribe(mqtt_tasmota);
    client.publish("weather", "ola do esp32");
  } else {
    Serial.println("Connection to MOSQUITTO  Broker failed. , i will try again...");
    timeToReconnect = millis() + 5000;
  }
}
//////////////////// SETUP ////////////////
void setup() {

  //Serial
  Serial.begin(115200);
  ///////////////////////////
  //Wifi
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) Serial.println("STA Failed to configure");
  WiFi.begin(ssid, password);
  delay(1000);

  //BROKER
  connectMosquitto();

//OTA
#include "ota.h"
  ///////////////////////////

}
//////////////////// LOOP ////////////////
void loop() {

  ///////////////////////////////////////
  //MOSQUITTO
  client.loop();
  if (client.state() != 0 && timeToReconnect < millis() && WiFi.status() == WL_CONNECTED) {
    connectMosquitto();
  }
  client.loop();

  //OTA
  ArduinoOTA.handle();
}

More info: I can ping the ESP, OTA is working fine.

I'll apreciate any help.

Please post your sketch here, using < CODE/ > tags when you do

It's a link to Pastebin, is not Ok ?!

I know what it is. Better to post it here to make it easier to access and copy to examine

Done. Thank you.

One possibility is that the MAC address is not being allowed through on the router.

Look at the router logs to see why/if a connection is being refused.

Thank you. But I have wifi. I can ping ESP IP address, I can flash with OTA.

can the esp32 ping?

Wierd,
Can ping local IP.
My broker IP
Mosquitto IP

This is mosquitto IP

Connecting to WiFi
.
WiFi connected with ip 192.168.121.110
Pinging ip 85.119.83.194
Success!!

You've lead me to the solution.
I've binded MAC address to IP with previous ESP. So I can't use the same IP. I was using fixed IP.
Now it's easy to fix.
Thank you.

1 Like

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