knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.84k stars 1.47k forks source link

esp32 Mqtt connection by hostname #845

Open josefgull opened 3 years ago

josefgull commented 3 years ago

My setup: i have an wifi router to wich an ubuntopc is connected running IOT-Stack and ihave my esp32 wich should send temp-data to it.

everthing works fine when i use the ip it doesnt anymore if i switch to hostname

the code is the same as from this projekt https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/

errors i get :[E][WiFiGeneric.cpp:654] hostByName(): DNS Failed for

i also tryed pinging like this

include

IPAddress ip (192, 168, 0, 102); // case1The remote ip to ping bool ret1 = Ping.ping(ip); bool ret = Ping.ping("hostname");// case2 bool ret = Ping.ping("hostname.local");// case3

it returns on in case 1 true

slavino commented 3 years ago

You may need to dive bit deeper into networking basics.

https://forum.arduino.cc/index.php?topic=328931.0

SaKiEQ commented 3 years ago
on ESP32 is 'faulty', do not include "host.local' - just "host" instead. The ESP32 version of mDNS adds an additional ".local" for what ever reason to the address. It works fine on ESP8266 compilations, but nor for ESP32...
josefgu commented 2 years ago

the problem is that i have an nuc pc where i installed mosqitto and graphana. and i gave this pc a name(my-pc). I can reach my-pc via browser or ping. but it is not possible to use my-pc as mqtt_server in puzbsub script. i am not shure if theres any possiblity without giving my pc a static ip.

josefgu commented 2 years ago

i tryed this code to look if i get an ip: WiFi.dnsIP(dnServer); IPAddress result; int err = WiFi.hostByName(mqtt_server, result) ; if(err == 1){ Serial.print("Ip address: "); Serial.println(result); } else { Serial.print("Error code: "); Serial.println(err);

for google.com this gave back an ip for my-pc none

BrentonPoke commented 2 years ago

I am also having issues with this. This library is a dependency in a project I work with, but I'm not able to connect by just using the hostname on the same network.