# Help with mqtt (maybe something else)

**URL:** <https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988>\
**Category:** Programming\
**Created:** [April 6, 2023, 8:41pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988 "2023-04-06T20:41:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 8:41pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/1 "2023-04-06T20:41:33Z")

</div>

Hi All,  
Need your help once more.  
I had a perfect working sketch, everything working.  
Made a bad connection and fried my ESP ☹  
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 🙂

Here's the code:

```cpp
////////////////////////////
//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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 6, 2023, 8:42pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/2 "2023-04-06T20:42:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 8:44pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/3 "2023-04-06T20:44:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 6, 2023, 8:50pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/4 "2023-04-06T20:50:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 8:53pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/5 "2023-04-06T20:53:47Z")

</div>

Done. Thank you.

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [April 6, 2023, 9:07pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/6 "2023-04-06T21:07:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 9:12pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/7 "2023-04-06T21:12:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [April 6, 2023, 9:13pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/8 "2023-04-06T21:13:54Z")

</div>

can the esp32 ping?

> **[GitHub - marian-craciunescu/ESP32Ping: Ping library for ESP32 Arduino core](https://github.com/marian-craciunescu/ESP32Ping)**
>
> Ping library for ESP32 Arduino core. Contribute to marian-craciunescu/ESP32Ping development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 9:23pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/9 "2023-04-06T21:23:41Z")

</div>

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

This is mosquitto IP

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

```

---

<div class="post-metadata">

**Author:** ![iduarte](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/iduarte/32/1101310_2.png) [@iduarte](https://forum.arduino.cc/u/iduarte)\
**Post date:** [April 6, 2023, 9:33pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/10 "2023-04-06T21:33:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 3, 2023, 9:34pm UTC](https://forum.arduino.cc/t/help-with-mqtt-maybe-something-else/1111988/11 "2023-10-03T21:34:12Z")

</div>

Closed on October 3, 2023, 9:34pm UTC

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