Page 1 of 1

Can not connect to ESP32 Broker using DNS from phone

Posted: Mon Nov 04, 2024 3:45 pm
by ntdkhoa0410
as in title, I can not connect to ESP32 Broker using DNS from phone
Background
I am creating a MQTT broker on ESP32 using https://github.com/mlesniew/PicoMQTTlibrary, and use https://github.com/espressif/arduino-es ... /ESPmDNS.hlib so I can access it via hostname instead of IP address. Here is my code:

Code: Select all

#include <PicoMQTT.h>
#include <ESPmDNS.h>

#define WIFI_SSID "myWifi"
#define WIFI_PASSWORD "myPassword"


class MQTT: public PicoMQTT::Server {
    protected:
        PicoMQTT::ConnectReturnCode auth(const char * client_id, const char * username, const char * password) override {
            // only accept client IDs which are 3 chars or longer
            if (String(client_id).length() < 3) {    // client_id is never NULL
                return PicoMQTT::CRC_IDENTIFIER_REJECTED;
            }


            // only accept connections if username and password are provided
            if (!username || !password) {  // username and password can be NULL
                // no username or password supplied
                return PicoMQTT::CRC_NOT_AUTHORIZED;
            }


            // accept two user/password combinations
            if (
                ((String(username) == "alice") && (String(password) == "1"))
                || ((String(username) == "bob") && (String(password) == "password"))) {
                return PicoMQTT::CRC_ACCEPTED;
            }


            // reject all other credentials
            return PicoMQTT::CRC_BAD_USERNAME_OR_PASSWORD;
        }
} mqtt;


void setup() {
  // Setup serial
  Serial.begin(115200);

  // Connect to WiFi
  Serial.printf("Connecting to WiFi %s\n", WIFI_SSID);
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) { delay(1000); }
  Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());

  if (!MDNS.begin("esp32")) {
    Serial.println("Error setting up MDNS responder!");
    while(1){
        delay(1000);
    }
  }
  // Subscribe to a topic pattern and attach a callback
  mqtt.subscribe("#", [](const char* topic, const char* payload) {
    Serial.printf("Received message in topic '%s': %s\n", topic, payload);
  });
  mqtt.begin();
}

void loop() {
  mqtt.loop();
  if (random(1000) == 0){
    mqtt.publish("picomqtt/welcome", "Hello from PicoMQTT!");
  }
}
Current results
On my computer, using Mosquitto:

mosquitto_pub -h esp32 -t "test/topic" -m "Hello from mosquitto_pub!" -u "alice" -P "1"

mosquitto_pub -h <The ESP32 IP address> -t "test/topic" -m "Hello from mosquitto_pub!" -u "alice" -P "1"

Both of the above works, esp32 prints out the message.
Problem statement
So I tried to move to my phone, using the IOT MQTT panel app https://play.google.com/store/apps/deta ... prod&hl=en When i connect using the Ip address, fine, it works and can pulish message

But when I changed to "esp32" or "esp32.local" , it says can not connect, how?...

Re: Can not connect to ESP32 Broker using DNS from phone

Posted: Tue Nov 05, 2024 3:30 pm
by lbernstone
mDNS name resolution is not available by default on (most) Android, so unless your mqtt client (or phone) specifically supports it, this is not going to be possible.