ESP32 code giving error Attempting MQTT connection...failed, rc=-4

Hello, i’m trying to use ESP32 to send sensors data to an HTML dashboard , when i run the code on esp32 it gives me the error " Attempting MQTT connection…failed, rc=-4" i used port 8884
i will send part of the code below, i need help to solve this problem

Hello @muhmmad ,

Thank you for the outreach, and first off, welcome to the HiveMQ Community! We are always happy to see new faces working with MQTT.

With that in mind, there are a few things that jump out at me based on the details you’ve provided.

To start with, RC = 4 here indicates that a timeout occurred when attempting to connect to the provided broker. It looks like you may be using a HiveMQ Cloud instance as your host broker - if that is the case, then port 8884 is reserved for Websocket connections. With this library, I believe standard TCP connections are supported, and therefore would utilize port 8883.

It is also worth mentioning that, when using HiveMQ Cloud, a TLS connection with TLS-SNI enabled is a requirement, and it does not look like a TLS configuration has been provided within the code here. As an example, ESP-MQTT does look to support TLS, and has some provided sample configurations as well.

Let us know if this answers your question, or if there is any additional assistance we can provide!

Best,
Aaron from the HiveMQ Team

1 Like

@AaronTLFranz thanks for your concern, i used 8883 port without any problem and it worked, but I need to use port 8884 to display the data on an HTML dashboard, from your words, I understood that I need to put the TLS certificate in the esp32 code right? if yes please tell me how to generate it because I tried but I can’t find an easy way