Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot connect to MQTT broker running on the same ESP32 (PicoMQTT) #1068

Open
fredlcore opened this issue Nov 23, 2024 · 3 comments
Open

Cannot connect to MQTT broker running on the same ESP32 (PicoMQTT) #1068

fredlcore opened this issue Nov 23, 2024 · 3 comments

Comments

@fredlcore
Copy link

fredlcore commented Nov 23, 2024

I have PubSubClient running fine on an ESP32 which connects without any problems to an MQTT broker in my local network.
For my project, I now want to include an MQTT broker in my code on the same ESP32 that PubSubClient is running on, so that users have the choice of using a dedicated MQTT broker or - if my project is the only one they need MQTT for - use a small MQTT broker on the same ESP32. For that I have included PicoMQTT. I can connect to the PicoMQTT broker on that same ESP32 that PubSubClient is running on from MQTT Explorer and publishing and subscribing the topics from outside the ESP32 works fine. PubSubClient is also still running fine when subscribing and publishing to an external MQTT broker, but what is not working is for PubSubClient to connect to the PicoMQTT broker.

I have used the actual local IP, as well as 127.0.0.1 as well as "localhost" to tell PubSubClient that it should connect to the broker on the same host, but the connection fails all the time. state() returns -2 which means that the connection has failed.

Does anyone have an idea why this is the case? I don't want to switch to PicoMQTT for the client functions because it only runs on ESPs, not on the Arduino Due which is also used by a number of my users. Adding an optional feature such as the broker would be fine to offer for just the ESP users.

Any help is highly appreciated!

@joenuK
Copy link

joenuK commented Feb 14, 2025

Did you find a solution to this? I'm running into the exact same issue.

@fredlcore
Copy link
Author

No, I decided not to pursue this approach anymore because the ESP32 has too little memory to keep/retain several hundred values. But I read somewhere else that the issue is connected to the fact that the network library of the ESP32 doesn't resolve localhost (or has a concept of it talking to itself), so that's what could be the reason here as well...

@joenuK
Copy link

joenuK commented Feb 15, 2025

Thank you for your response.
I think I will also follow an other approach so I get rid of the local broker.

But I will list what I found out. Maybe it's interesting for someone:
I could run PubSubClient with EmbeddedMqttBroker on the same ESP32. There PubSubClient connects to 127.0.0.1. Because I found memory leaks on this broker I will not use it. But it shows that it is possible to connect to localhost with PubSubClient.

I could run PubSubClient with PicoMqtt on the same ESP32. The PubSubClient connected to an other ESP32 with a PicoMqtt broker what worked without problems and I could send and subscribe from a raspberry pi and mosquito to the first PicoMqtt broker. So running PubSubClient and PicoMqtt on the same ESP32 is working.
The problems start when I try to connect PubSubClient to PicoMqtt running on the same host. I tried the actual IP of the device, localhost and 127.0.0.1 but none of this got a connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants