BASIC CODE for MQTT connection

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

Hello Jose,

Thanks for your interest in MQTT and HiveMQ MQTT Broker!

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

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 towards your HiveMQ Cloud broker cluster:

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

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

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