# Ameba MQTT connect to Hive MQ Cloud

**URL:** <https://forum.amebaiot.com/t/ameba-mqtt-connect-to-hive-mq-cloud/1106>\
**Category:** SDK\
**Created:** [February 18, 2022, 4:46am UTC](https://forum.amebaiot.com/t/ameba-mqtt-connect-to-hive-mq-cloud/1106 "2022-02-18T04:46:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruanjanc](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@ruanjanc](https://forum.amebaiot.com/u/ruanjanc)\
**Post date:** [February 18, 2022, 4:46am UTC](https://forum.amebaiot.com/t/ameba-mqtt-connect-to-hive-mq-cloud/1106/1 "2022-02-18T04:46:55Z")

</div>

Hi,

Has anyone success to connected Hive MQ Cloud via Ameba standard SDK MQTT lib ?  
[http://console.hivemq.cloud/](http://console.hivemq.cloud/)  
[https://www.hivemq.com/docs/hivemq-cloud/introduction.html](https://www.hivemq.com/docs/hivemq-cloud/introduction.html)  
My device always received CONNACK with return code is 5 (The Client is not authorized to connect) on MQTT package level. SSL hand shake was success.

Regards,

---

<div class="post-metadata">

**Author:** ![wyy](https://avatars.discourse-cdn.com/v4/letter/w/bc8723/32.png) [@wyy](https://forum.amebaiot.com/u/wyy)\
**Post date:** [February 18, 2022, 9:40am UTC](https://forum.amebaiot.com/t/ameba-mqtt-connect-to-hive-mq-cloud/1106/2 "2022-02-18T09:40:36Z")

</div>

Hi @ruanjanc

are you using the MQTT example found in `example_mqtt.c`, activated by the macro `#define CONFIG_EXAMPLE_MQTT` ?

---

<div class="post-metadata">

**Author:** ![ruanjanc](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@ruanjanc](https://forum.amebaiot.com/u/ruanjanc)\
**Post date:** [February 18, 2022, 3:19pm UTC](https://forum.amebaiot.com/t/ameba-mqtt-connect-to-hive-mq-cloud/1106/3 "2022-02-18T15:19:00Z")

</div>

@wyy  
Yes, correct I use `example_mqtt.c` , activated by the macro `#define CONFIG_EXAMPLE_MQTT`

What I change from default SDK are.

1. Change Heap Size `FreeRTOS.h`  
`#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 110 * 1024 ) )	// use HEAP5`

2. Switch to use mbed SSL and enable MQTT example `platform_opts.h`  
`#define CONFIG_USE_POLARSSL 0`  
`#define CONFIG_USE_MBEDTLS 1`  
`#define CONFIG_EXAMPLE_MQTT 1`

3. Change (mbed) SSL Max content length `config_rsa.h`  
`#define MBEDTLS_SSL_MAX_CONTENT_LEN 16384`

4. To run prvMQTTEchoTask `MQTTClient`.h  
`//#define MQTT_TASK`

5. `example_mqtt.c``prvMQTTEchoTask`  
Change address  
`char* address = "xxxxxxxxxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud";`

Add user name & password  
`char * user_name = "Usernamexxxxx";`  
`char * password = "Passwordxxxx";`

Config network to use SSL & add Root CA of HiveMQ Cloud  
`network.rootCA = (char*)rootCABuff;`  
`network.clientCA = NULL;`  
`network.private_key = NULL;`  
`network.use_ssl = 1;`

1. Change port to `8883` follow as HiveMQ Cloud spec.

2. Set connection data  
`connectData.username.cstring = user_name;`  
`connectData.password.cstring = password;`  
`connectData.MQTTVersion = 3;`  
`connectData.clientID.cstring = "FT1_018";`  
`connectData.cleansession = 1;`

And below is log, as you can see SSL handshake is success. But return code from MQTT is 5

> [Fri Feb 18 21:52:32.717 2022] [5087]mqtt:Connect Network xxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud"  
> [Fri Feb 18 21:52:32.955 2022]  
> [Fri Feb 18 21:52:32.971 2022] [5331]mqtt:addr = 54.73.92.158  
> [Fri Feb 18 21:52:33.196 2022]  
> [Fri Feb 18 21:52:33.196 2022] [5565]mqtt:root\_crt parse done  
> [Fri Feb 18 21:52:33.499 2022]  
> [Fri Feb 18 21:52:33.499 2022] [5868]mqtt:  
> [Fri Feb 18 21:52:33.499 2022] Verify requested for (Depth 1):  
> [Fri Feb 18 21:52:33.515 2022]  
> [Fri Feb 18 21:52:33.515 2022]  
> [Fri Feb 18 21:52:33.516 2022] [5882]mqtt:cert. version : 3  
> [Fri Feb 18 21:52:33.554 2022] serial number : 91:2B:08:4A:CF:0C:18:A7:53:F6:D6:2E:25:A7:5F:5A  
> [Fri Feb 18 21:52:33.567 2022] issuer name : C=US, O=Internet Security Research Group, CN=ISRG Root X1  
> [Fri Feb 18 21:52:33.577 2022] subject name : C=US, O=Let’s Encrypt, CN=R3  
> [Fri Feb 18 21:52:33.588 2022] issued on : 2020-09-04 00:00:00  
> [Fri Feb 18 21:52:33.599 2022] expires on : 2025-09-15 16:00:00  
> [Fri Feb 18 21:52:33.600 2022] signed using : RSA with SHA-256  
> [Fri Feb 18 21:52:33.609 2022] RSA key size : 2048 bits  
> [Fri Feb 18 21:52:33.609 2022] basic constraints : CA=true, max\_pathlen=0  
> [Fri Feb 18 21:52:33.624 2022] key usage : Digital Signature, Key Cert Sign, CRL Sign  
> [Fri Feb 18 21:52:33.641 2022] ext key usage : TLS Web Client Authentication, TLS Web Server Authentication  
> [Fri Feb 18 21:52:33.672 2022]  
> [Fri Feb 18 21:52:33.672 2022]  
> [Fri Feb 18 21:52:33.672 2022] [6033]mqtt: This certificate has no flags  
> [Fri Feb 18 21:52:33.673 2022]  
> [Fri Feb 18 21:52:33.673 2022]  
> [Fri Feb 18 21:52:33.673 2022] [6046]mqtt:  
> [Fri Feb 18 21:52:33.688 2022] Verify requested for (Depth 0):  
> [Fri Feb 18 21:52:33.688 2022]  
> [Fri Feb 18 21:52:33.688 2022]  
> [Fri Feb 18 21:52:33.688 2022] [6061]mqtt:cert. version : 3  
> [Fri Feb 18 21:52:33.704 2022] serial number : 04:33:4E:59:4E:0A:6B:F4:EE:70:04:55:E0:8D:E0:89:BB:A9  
> [Fri Feb 18 21:52:33.747 2022] issuer name : C=US, O=Let’s Encrypt, CN=R3  
> [Fri Feb 18 21:52:33.760 2022] subject name : CN=\*.s1.eu.hivemq.cloud  
> [Fri Feb 18 21:52:33.772 2022] issued on : 2022-01-09 15:25:32  
> [Fri Feb 18 21:52:33.773 2022] expires on : 2022-04-09 15:25:31  
> [Fri Feb 18 21:52:33.784 2022] signed using : RSA with SHA-256  
> [Fri Feb 18 21:52:33.792 2022] RSA key size : 2048 bits  
> [Fri Feb 18 21:52:33.792 2022] basic constraints : CA=false  
> [Fri Feb 18 21:52:33.800 2022] subject alt name : \*.s1.eu.hivemq.cloud, s1.eu.hivemq.cloud  
> [Fri Feb 18 21:52:33.815 2022] key usage : Digital Signature, Key Encipherment  
> [Fri Feb 18 21:52:33.831 2022] ext key usage : TLS Web Server Authentication, TLS Web Client Authentication  
> [Fri Feb 18 21:52:33.849 2022]  
> [Fri Feb 18 21:52:33.849 2022]  
> [Fri Feb 18 21:52:33.849 2022] [6215]mqtt: This certificate has no flags  
> [Fri Feb 18 21:52:33.862 2022]  
> [Fri Feb 18 21:52:34.309 2022]  
> [Fri Feb 18 21:52:34.309 2022] [6672]mqtt:ssl handshake success  
> [Fri Feb 18 21:52:34.309 2022]  
> [Fri Feb 18 21:52:34.324 2022] [6684]mqtt:“xxxxxxxxxxxxxxxxxxxxx.s1.eu.hivemq.cloud” Connected  
> [Fri Feb 18 21:52:34.340 2022]  
> [Fri Feb 18 21:52:34.340 2022] [6705]mqtt:Start MQTT connection  
> [Fri Feb 18 21:52:34.547 2022]  
> [Fri Feb 18 21:52:34.547 2022] [6911]mqtt:Read packet type: 2  
> [Fri Feb 18 21:52:34.548 2022]  
> [Fri Feb 18 21:52:34.548 2022] [6920]mqtt:Return code from MQTT connect is 5

Regards,
