# How to connect to HiveMQ Cloud from Arduino

**URL:** <https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762>\
**Category:** HiveMQ Cloud\
**Created:** [October 6, 2021, 8:36pm UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762 "2021-10-06T20:36:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@frank](https://community.hivemq.com/u/frank)\
**Post date:** [October 6, 2021, 8:36pm UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/1 "2021-10-06T20:36:20Z")

</div>

I’m trying to make connection to HiveMQ cloud from Arduino, but get a connection failed with state “-2”. In the cluster details only ports 8883 (TLS) and 8884 (Websocket + TLS) are specified, but all the Arduino examples seem to use port 1883, e.g. [Arduino PubSubClient - MQTT Client Library Encyclopedia](https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-arduino-pubsubclient/).

The network I use is a WiFi defined as

```auto
#include <WiFiNINA.h>

// WiFi settings
char ssid[] = "***";          
char ssid_password[] = "***";  
int wifi_status = WL_IDLE_STATUS;

WiFiClient network;

void setup() {
  while (wifi_status != WL_CONNECTED) {
    Serial.print(".");
    wifi_status = WiFi.begin(ssid, ssid_password);
    delay(2500);
  }
}

```

I use this wifi network to connect to HiveMQ Cloud with:

```auto
#include <PubSubClient.h>

PubSubClient mqttClient(network);

const char* server = "xxx.s1.eu.hivemq.cloud";

void init_mqtt() {
  mqttClient.setServer(server, 1883);

  if (mqttClient.connect("Arduino", "xxx", "xxx")) {
    Serial.println("Connected");
  } else {
    Serial.print("Connection failed: ");
    Serial.println(mqttClient.state());
  }
}

```

Serial.println(mqttClient.state()); shows the error code -2.

How can I further investigate what is going wrong? Or do I need to do additional steps to be able to connect to TLS?

---

<div class="post-metadata">

**Author:** ![YannickWeber](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/yannickweber/32/103_2.png) [@YannickWeber](https://community.hivemq.com/u/YannickWeber)\
**Post date:** [October 7, 2021, 11:20am UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/2 "2021-10-07T11:20:57Z")

</div>

Hello Frank,

You are trying to connect to HiveMQ Cloud with the default MQTT port (1883). HiveMQ Cloud only allows the ports 8883 (TLS) and 8884 (Websocket + TLS). This is why your connection fails.

> Or do I need to do additional steps to be able to connect to TLS?

Yes, you do need to take further steps. Good news is that a full fledge guide on how to do this will be available very soon.  
In the meantime maybe you can find some help in this thread: [MQTT with SSL/TLS · Issue #462 · knolleary/pubsubclient · GitHub](https://github.com/knolleary/pubsubclient/issues/462)

- Yannick

---

<div class="post-metadata">

**Author:** ![frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@frank](https://community.hivemq.com/u/frank)\
**Post date:** [October 7, 2021, 11:33am UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/3 "2021-10-07T11:33:51Z")

</div>

Thanks for the feedback! How soon is very soon? 😉

I guess the most important part is the certificate to be added into

```auto
// Set x509 CA root (must match server cert)
const char *x509CA PROGMEM = R"EOF("
-----BEGIN CERTIFICATE-----
[...]
-----END CERTIFICATE-----
")EOF";

```

---

<div class="post-metadata">

**Author:** ![smartskills](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@smartskills](https://community.hivemq.com/u/smartskills)\
**Post date:** [October 8, 2021, 8:06pm UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/4 "2021-10-08T20:06:27Z")

</div>

Hi Frank,

In case it help, I’m able to connect on the hiveMQ Cloud server with an arduino MKR Wifi 1010 via the library ArduinoMqttClient ( [GitHub - arduino-libraries/ArduinoMqttClient: ArduinoMqttClient Library for Arduino](https://github.com/arduino-libraries/ArduinoMqttClient) available on the Library Manager.

---

<div class="post-metadata">

**Author:** ![YannickWeber](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/yannickweber/32/103_2.png) [@YannickWeber](https://community.hivemq.com/u/YannickWeber)\
**Post date:** [October 29, 2021, 1:16pm UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/5 "2021-10-29T13:16:50Z")

</div>

Hello Frank 😉

> How soon is very soon?

HiveMQ Cloud now has a guide on how to connect with Arduino ESP8266. You can find it under `Cluster Details > Getting Started > Arduino ESP8266`. I hope this helps you 🙂

- Yannick

---

<div class="post-metadata">

**Author:** ![frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@frank](https://community.hivemq.com/u/frank)\
**Post date:** [November 9, 2021, 1:26pm UTC](https://community.hivemq.com/t/how-to-connect-to-hivemq-cloud-from-arduino/762/6 "2021-11-09T13:26:13Z")

</div>

For others looking for the correct link, you need a HiveMQ Cloud account and then can find the info on `https://console.hivemq.cloud/clients/arduino-esp8266?uuid={YOUR_CLOUD_ID}`
