Connecting to HiveMQ Cloud Basic with Mongoose-OS

HiveMQ Cloud is “the live version” of HiveMQ, an excellent MQTT broker from the company of the same name. You can have HiveMQ running in your servers, or you can take advantage of the HiveMQ Cloud infrastructure and pay a plan according to your business needs.

HiveMQ Cloud Basic is free for up to 100 concurrent connections (at the time I wrote this…), which is great for us developers for quick and stable proof-of-concept stuff. You can even start with this and move to a paid service once your business starts gathering momentum.

The broker is a full fledged MQTT 5 broker with everything you expect, including QoS2, last will and retained messages. Connections use TLS in server authentication mode, there is no user certificate, valid credentials are a username and password pair created at the web console.

For more info, read their Quick Start Guide.

Configuration

In mos.yml, we need to add the broker name and port, the CA certificate for TLS, and a user/password set of credentials. The broker hostname we get it from our cluster details at the HiveMQ console. The set of credentials we will configure there in the Access Management section.

config_schema:
  - ["mqtt.enable", true]
  - ["mqtt.server", "yarayarayara.hivemq.cloud:8883"]
  - ["mqtt.user", "username"]                        
  - ["mqtt.pass", "password"]                       
  - ["mqtt.ssl_ca_cert", "trustid-x3-root.pem"]      

If you need more information on using MQTT over TLS, read this article.

Example

You can find a working example at Github. It is a simple mJS script that publishes a small message once a connection is established.

The CA certificate has been included, in case of trouble check the HiveMQ Cloud docs for the cloud console (for registered users), there you’ll find how to get a fresh copy. Mongoose-OS requires the certificate to recognize it has to initiate the TLS client handshake and to be able to validate the server certificate.

If everything goes fine, after we mos build and mos flash we’ll see this in mos console:

[May 21 14:57:38.806] mgos_mqtt_conn.c:468    MQTT0 connecting to somelonghexnumber.s1.eu.hivemq.cloud:8883
[May 21 14:57:38.841] mgos_mongoose.c:66      New heap free LWM: 210876
[May 21 14:57:39.350] mg_ssl_if_mbedtls.c:30  0x3ffc8818 ciphersuite: TLS-ECDHE-RSA-WITH-AES-128-GCM-SHA256
[May 21 14:57:40.741] SW ECDH curve 3
[May 21 14:57:41.170] mgos_mongoose.c:66      New heap free LWM: 191588
[May 21 14:57:41.467] mgos_mqtt_conn.c:227    MQTT0 TCP connect ok (0)
[May 21 14:57:41.733] mgos_mqtt_conn.c:271    MQTT0 CONNACK 0
[May 21 14:57:41.742] init.js:34              MQTT connected
[May 21 14:57:41.756] init.js:26              Published:OK topic:/this/test/esp32_807A98 msg:CONNECTED!

If we have a mosquitto_sub client, or some other we like, we can connect to the broker to see everything is working. In my case, I user CentOS 7 and the mosquitto client requires also specifying the CA certificate:

$ mosquitto_sub -h somelonghexnumber.s1.eu.hivemq.cloud -p 8883 -t "#" -u yourusername -P yourpassword --cafile pathto/isrgrootx1.pem -v
	/this/test/esp32_807A98 CONNECTED!

Want to share this ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image