Mqtt+mqtt explorer

I use mqtt explorer . this I publish the topic nazneen/bin and subscribe the topic by esp32 . but problem is that I don't get any message to esp32 after subscribe the topic ..there is a code below

#ifdef ESP8266
#include <ESP8266WiFi.h> /* WiFi library for ESP8266 */
#else
#include <WiFi.h> /* WiFi library for ESP32 */
#endif
#include <Wire.h>
#include <PubSubClient.h>


#define wifi_ssid "yourSSID"
#define wifi_password "yourPASSWORD"
#define mqtt_server "test.mosquitto.org"

#define naz_topic "nazneen/bin"

WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length) {
  Serial.print("Message arrived in topic: ");
  Serial.println(topic);
  Serial.print("Message:");
  for (int i = 0; i < length; i++){
    Serial.print((char)payload[i]);
  }
  Serial.println();
  Serial.println("-----------------------");
}

void setup() {
  Serial.begin(115200);
  // dht.begin();
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");

    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}


void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //  client.publish(naz_topic, "welcome", true);
client.subscribe(naz_topic);
}

any one can help?

What were you expecting? You've subscribed, so the ESP should pickup anything published, but there's no code to display anything. Similarly, you don't publish.

Try a simple example that just prints what is published. Subscribing over & over again every time loop is called should not be necessary.

MQTT requires a unique id and someone else is already using "ESP8266Client" so change the string in
client.connect("ESP8266Client")
to some random characters
client.connect("jgfkhjgjhmoihfpijpj")

That will give you a connection that doesn't keep getting disconnected and then reconnecting.

For test purposes try using the topic "/merakimv/Q2FV-VZ4B-BH3V/0" as that currently is receiving a steady stream of messages.

It would also make sense to move the
client.subscribe(naz_topic);
from the loop into the reconnect function.

What about the code in the callback function ?

Oops :pleading_face:

WiFi connected

IP address:

192.168.218.246

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Message arrived in topic: packplast/nazneen/file

Message: hello nazneen how are you?


Message arrived in topic: packplast/nazneen/file

Message: i am good


thank you now I get the message
is that possible to publish binary data from Mqtt explorer and subscribe it by esp32 and update the firmware by this binary data

after some i try again but don't get the message
output
Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...connected

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Attempting MQTT connection...connected

Attempting MQTT connection...

I don't know the answer, however a Google search for "esp32 mqtt ota" gives lots of results including links to libraries.

ok thanks ...

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