# BASIC CODE for MQTT connection

**URL:** <https://community.hivemq.com/t/basic-code-for-mqtt-connection/829>\
**Category:** HiveMQ Cloud\
**Created:** [November 19, 2021, 9:22am UTC](https://community.hivemq.com/t/basic-code-for-mqtt-connection/829 "2021-11-19T09:22:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jigutierrez](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jigutierrez](https://community.hivemq.com/u/jigutierrez)\
**Post date:** [November 19, 2021, 9:22am UTC](https://community.hivemq.com/t/basic-code-for-mqtt-connection/829/1 "2021-11-19T09:22:06Z")

</div>

Hello,

I have a Ewon Flexy205 and I sending MQTT data. It needs BASIC (or java) code for do it.

I have problems with the code in the MQTT connection with my HIVEMQ cloud.

I wanna put a code thats is working actually for connection with IoTHub Azure. I need to know what I have to addpat for HIVEMQ cloud.

**THE CODE IS:**

CONNECTMQTT:  
MQTT “OPEN”, SerNum$ , MQTTBrokerURL$  
MQTT “SETPARAM”, “PORT”, MQTTPort$  
MQTT “SETPARAM”, “LOG”, “1”  
MQTT “SETPARAM”, “KEEPALIVE”, “20”  
MQTT “SETPARAM”, “TLSVERSION”, “tlsv1.2”  
MQTT “SETPARAM”, “PROTOCOLVERSION”, “3.1.1”  
MQTT “SETPARAM”, “cafile”,"/usr/BaltimoreCyberTrustRoot.pem"  
MQTT “SETPARAM”, “CertFile”,"/usr/"+DeviceId$+".crt"  
MQTT “SETPARAM”, “KeyFile”,"/usr/"+DeviceId$+".key"

//_ **AZURE** _//  
MQTT “SETPARAM”,“Username”,IotHubName$+ “.azure-devices.net/”+DeviceId$+"/api-version=2016-11-14"  
MQTT “SETPARAM”,“Password”,“HostName=”+IotHubName$+";DeviceID="+DeviceId$+";x509=true"

MQTT “SETPARAM”, “WILLTOPIC”, “/willtopic”  
MQTT “SETPARAM”, “WILLPAYLOAD”, “Flexy " + SerNum$ + " is disconnected”

MQTT “SUBSCRIBE”,TopicToSubscribe$,1

//Launch the MQTT process in the background

SETSYS PRG,“RESUMENEXT”,1 //Continue in case of error at MQTT “CONNECT”  
MQTT “CONNECT”

**END CODE**

Can anyone help me to adapt this code to HIVE connetion

Thanks so much  
Jose

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [November 19, 2021, 12:49pm UTC](https://community.hivemq.com/t/basic-code-for-mqtt-connection/829/2 "2021-11-19T12:49:47Z")

</div>

Hello Jose,

Thanks for your interest in MQTT and HiveMQ MQTT Broker!

Looking at your code, it seems that the issue is the “cafile”:

```auto
MQTT “SETPARAM”, “cafile”,"/usr/BaltimoreCyberTrustRoot.pem"

```

You would need to use HiveMQ Broker “server.pem”.  
You can create the Server Certificate file using the [openssl s\_client](https://community.hivemq.com/t/frequently-asked-questions/514) towards your HiveMQ Cloud broker cluster:

```auto
openssl s_client -connect XXXXXXXXXXXXXXXXXXXXX.s2.eu.hivemq.cloud:8883 -showcerts < /dev/null 2> /dev/null | sed -n '/BEGIN/,/END/p' > server.pem

```

This will create a file called “server.pem”, which you can use as “Server Certiciate”.

Please let me know if this helps.

Kind regards,  
Dasha from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![jigutierrez](https://avatars.discourse-cdn.com/v4/letter/j/6bbea6/32.png) [@jigutierrez](https://community.hivemq.com/u/jigutierrez)\
**Post date:** [November 19, 2021, 1:34pm UTC](https://community.hivemq.com/t/basic-code-for-mqtt-connection/829/3 "2021-11-19T13:34:45Z")

</div>

Hi Dasha,

Yes you rigth. It was just and example with the \*.pem. I will generate the server.pem and upload that file into the Flexy.

But I have doubts with the next lines:

**LINE1**

MQTT “SETPARAM”,“Username”,IotHubName$+ “.azure-devices.net/”+DeviceId$+"/api-version=2016-11-14"

I understund that IotHubName don’t need it. I have to eliminate it and put just “broker address”…

And what about: /api-version=2016-11-14" ??? thats for Azure… but in HIVE how is that??

**LINE 2**

MQTT “SETPARAM”,“Password”,“HostName=”+IotHubName$+";DeviceID="+DeviceId$+";x509=true"

I understund I have to eliminate IotHubName again… but the other is OK… in’s it?

Thanks so much  
Jose

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [November 19, 2021, 2:29pm UTC](https://community.hivemq.com/t/basic-code-for-mqtt-connection/829/4 "2021-11-19T14:29:55Z")

</div>

Hi Jose,

What happens if you use Username and Password credentials that you set in HiveMQ Cloud → Cluster → Access Management → MQTT Credentials?

Thanks,  
Dasha from HiveMQ Team
