# ESP32 does not connect

**URL:** <https://community.hivemq.com/t/esp32-does-not-connect/2765>\
**Category:** Uncategorized\
**Created:** [March 11, 2024, 9:48am UTC](https://community.hivemq.com/t/esp32-does-not-connect/2765 "2024-03-11T09:48:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosetxoSB](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@JosetxoSB](https://community.hivemq.com/u/JosetxoSB)\
**Post date:** [March 11, 2024, 9:48am UTC](https://community.hivemq.com/t/esp32-does-not-connect/2765/1 "2024-03-11T09:48:36Z")

</div>

Hello

I’m new to this and can’t communicate with HiveMQ from an ESP32-CAM.Hola

I have defined a Cluster in HiveMQ.

The WiFi connection does it right.  
The MQTT connection gives me error 5.

In the Arduino program

const char\* mqtt\_server = “4aa46c5b67484842bf1e2a69f446a433.s1.eu.hivemq.cloud”; // Cluster URL  
const char\* mqtt\_username = “JosetxoSB”; // Cluster username  
const char\* mqtt\_password = “xxxxxxxxxx”; // Cluster password  
const int mqtt\_port =8883;

clientID, I’ve tried these two options  
// String clientId = “ESP8266Client-”; // Create a random client ID  
// clientId += String(random(0xffff), HEX);  
String clientId = “hivemq.webclient.1707816231762”; // userename in Cluster ACCESS MANAGEMENT

This is the Arduino program extracted from the internet.

#include \<WiFi.h\>  
#include \<PubSubClient.h\>  
#include \<WiFiClientSecure.h\>

#include \<Arduino.h\>  
#include \<time.h\>

// Update these with values suitable for your network.  
const char\* ssid = “xxxxxxxxxxxx”;  
const char\* password = “xxxxxxxx”;

/\*\*\*\*\*\*\* MQTT Broker Connection Details \*\*\*\*_/  
const char mqtt\_server = “4aa46c5b67484842bf1e2a69f446a433.s1.eu.hivemq.cloud”;  
const char mqtt\_username = “JosetxoSB”;  
const char_ mqtt\_password = “xxxxxxxxxx”;  
const int mqtt\_port =8883;

/\*\*\*\* Secure WiFi Connectivity Initialisation \*\*\*\*\*/  
WiFiClientSecure espClient;

/\*\*\*\* MQTT Client Initialisation Using WiFi Connection \*\*\*\*\*/  
PubSubClient client(espClient);

unsigned long lastMsg = 0;  
#define MSG\_BUFFER\_SIZE (50)  
char msg[MSG\_BUFFER\_SIZE];

const char\* sensor1\_topic = “sensor1”;

/\*\*\*\*\*\* root certificate \*\*\*\*\*\*\*\*\*/  
static const char \*root\_ca PROGMEM = R"EOF(  
-----BEGIN CERTIFICATE-----  
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw  
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh  
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4  
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu  
ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY  
MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc  
h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+  
0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U  
A5/TR5d8mUgjU+g4rk8Kb4Mu0UlXjIB0ttov0DiNewNwIRt18jA8+o+u3dpjq+sW  
T8KOEUt+zwvo/7V3LvSye0rgTBIlDHCNAymg4VMk7BPZ7hm/ELNKjD+Jo2FR3qyH  
B5T0Y3HsLuJvW5iB4YlcNHlsdu87kGJ55tukmi8mxdAQ4Q7e2RCOFvu396j3x+UC  
B5iPNgiV5+I3lg02dZ77DnKxHZu8A/lJBdiB3QW0KtZB6awBdpUKD9jf1b0SHzUv  
KBds0pjBqAlkd25HN7rOrFleaJ1/ctaJxQZBKT5ZPt0m9STJEadao0xAH0ahmbWn  
O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ATE-----  
)EOF";

/\*\*\*\*\*\*\*\*\*\*\*\*\* Connect to WiFi \*\*\*\*\*\*\*\*\*\*\*/  
void setup\_wifi() {  
delay(10);  
Serial.print("\nConnecting to ");  
Serial.println(ssid);

WiFi.mode(WIFI\_STA);  
WiFi.begin(ssid, password);

while (WiFi.status() != WL\_CONNECTED) {  
delay(500);  
Serial.print(“.”);  
}  
randomSeed(micros());  
Serial.println("\nWiFi connected\nIP address: ");  
Serial.println(WiFi.localIP());  
}

/\*\*\*\*\*\*\*\*\*\*\*\*\* Connect to MQTT Broker \*\*\*\*\*\*\*\*\*\*\*/  
void reconnect() {  
// Loop until we’re reconnected  
while (!client.connected()) {  
Serial.print(“Attempting MQTT connection…”);  
// String clientId = “ESP8266Client-”; // Create a random client ID  
// clientId += String(random(0xffff), HEX);  
String clientId = “hivemq.webclient.1707816231762”;  
// Attempt to connect  
if (client.connect(clientId.c\_str(), mqtt\_username, mqtt\_password)) {  
Serial.println(“connected”);  
client.subscribe(“led\_state”); // subscribe the topics here  
} else {  
Serial.print(“failed, rc=”);  
Serial.print(client.state());  
Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying  
delay(5000);  
}  
}  
}

/\*\*\*\*\* Call back Method for Receiving MQTT messages and Switching LED \*\*_/  
void callback(char topic, byte_ payload, unsigned int length) {  
String incommingMessage = “”;  
for (int i = 0; i \< length; i++) incommingMessage+=(char)payload[i];  
Serial.println(“Message arrived [”+String(topic)+“]”+incommingMessage);  
// //— check the incomming message  
// if( strcmp(topic,“led\_state”) == 0){  
// if (incommingMessage.equals(“1”)) digitalWrite(led, HIGH); // Turn the LED on  
// else digitalWrite(led, LOW); // Turn the LED off  
// }  
}

/\*\*\*\* Method for Publishing MQTT Messages \*\*\*\*\*\*\*\*\*_/  
void publishMessage(const char_ topic, String payload , boolean retained){  
if (client.publish(topic, payload.c\_str(), true))  
Serial.println(“Message publised [”+String(topic)+"]: "+payload);  
}

/\*\*\*\* Application Initialisation Function\*\*\*\*\*\*/  
void setup() {  
Serial.begin(115200);  
while (!Serial) delay(1);  
setup\_wifi();

espClient.setCACert(root\_ca); // enable this line and the the “certificate” code for secure connection  
client.setServer(mqtt\_server, mqtt\_port);  
client.setCallback(callback);  
}

/\*\*\*\*\*\*\*\* Main Function \*\*\*\*\*\*\*\*\*\*\*\*\*/  
void loop() {  
if (!client.connected()) reconnect(); // check if client is connected  
client.loop();

publishMessage(sensor1\_topic, “Hola”, false);

delay(100);  
}

---

<div class="post-metadata">

**Author:** ![SShet](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@SShet](https://community.hivemq.com/u/SShet)\
**Post date:** [March 13, 2024, 12:02pm UTC](https://community.hivemq.com/t/esp32-does-not-connect/2765/2 "2024-03-13T12:02:45Z")

</div>

Hello @JosetxoSB

Greetings and welcome to the HiveMQ Community Forum! We’re delighted to see your interest in MQTT and HiveMQ.

In response to your inquiry, may we kindly direct you to this link: [Connecting ESP-32 CAM with HiveMQ Cloud](https://community.hivemq.com/t/connecting-esp-32-cam-with-hive-mq-cloud/2098/2), where my colleague has generously shared a working example.

Should you require additional assistance, please don’t hesitate to reach out to us, we are happy to assist.

Regards,  
Sheetal from the HiveMQ team

---

<div class="post-metadata">

**Author:** ![JosetxoSB](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@JosetxoSB](https://community.hivemq.com/u/JosetxoSB)\
**Post date:** [March 14, 2024, 7:24am UTC](https://community.hivemq.com/t/esp32-does-not-connect/2765/3 "2024-03-14T07:24:34Z")

</div>

Thank you very much for your help.

Now everything is working properly.

Best regards

Josetxo Sabalza

En miércoles, 13 de marzo de 2024, 13:12:51 CET, Sheetal via HiveMQ Community Forum [notifications@hivemq1.discoursemail.com](mailto:notifications@hivemq1.discoursemail.com) escribió:

> ![](https://canada1.discourse-cdn.com/flex035/uploads/hivemq1/original/1X/f79c8d2978f3644a7eadd1c503cad6ed97a72316.png "SShet") | [SShet](https://community.hivemq.com/u/sshet)  
> March 13 |
> 
> - | - |

Hello [@JosetxoSB](https://community.hivemq.com/u/josetxosb)

Greetings and welcome to the HiveMQ Community Forum! We’re delighted to see your interest in MQTT and HiveMQ.

In response to your inquiry, may we kindly direct you to this link: [Connecting ESP-32 CAM with HiveMQ Cloud](https://community.hivemq.com/t/connecting-esp-32-cam-with-hive-mq-cloud/2098/2), where my colleague has generously shared a working example.

Should you require additional assistance, please don’t hesitate to reach out to us, we are happy to assist.

Regards,  
Sheetal from the HiveMQ team

---

<div class="post-metadata">

**Author:** ![JosetxoSB](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@JosetxoSB](https://community.hivemq.com/u/JosetxoSB)\
**Post date:** [March 27, 2024, 5:49am UTC](https://community.hivemq.com/t/esp32-does-not-connect/2765/4 "2024-03-27T05:49:09Z")

</div>

Hello SShet

Sorry for the delay in replying, it has been impossible for me to dedicate time to this problem.

Now everything works properly.

Thanks a lot

Best regards
