MQTT Setup using Pearl Gecko & Medusa AMW007

This example demonstrates connecting a Pearl Gecko eval board to a Medusa (AMW007) eval board to establish a connection with an MQTT broker and exchange data in both directions.

Settings for AMW007

Connect your Medusa board to your computer, and apply the settings:

set wlan.ssid <YOUR_NETWORK>
set wlan.passkey <YOUR_PASSWORD>
set system.cmd.mode machine
save
reboot

where <YOUR_NETWORK> is the SSID and <YOUR_PASSWORD> is the password for your local WLAN.

Update your module to the latest Gecko OS release that supports SSL/TLS (WL-1.2+).

To claim and update your Gecko OS device, from the AMW007 Gecko OS terminal, run the commands:

dms_claim <DMS_USER_NAME> <DMS_PASSWORD>
ota -f

The DMS Claim and OTA update process is dicussed in detail in Update and Recovery.

Obtain the required CA cert, client cert and client key files from your MQTT broker (Amazon AWS, Azure, etc.) and copy them to the AMW007 file system. The simplest way to do this is by dragging and dropping the files onto the Web App File tab:

Set the certificate variables to the names of the downloaded files. At the Gecko OS terminal, issue the command:

set network.tls.ca_cert <CA_CERT>
set network.tls.client_cert <CLIENT_CERT>
set network.tls.client_key <CLIENT_KEY>

where <CA_CERT>, <CLIENT_CERT> and <CLIENT_KEY> are the Gecko OS file system filenames of the CA cert, client cert and client key files.

Note that setting the correct network.tls variable to the correct file is essential. The following example demonstrates how to obtain and allocate the files for AWS.

AWS Example

In the AWS IoT Services section, from the menu choose Secure/Certificates.

In the Create a certificate screen, choose Create certificate.

From the Certificate created screen, download the certificate (<abc123...>.cert.pem), the private key (<abc123...>.private.key), and the root CA for AWS IoT. The public key is not needed for this application.

Before leaving this screen, activate this certificate by pressing the Activate button, and attach it to your previously created policy by pressing the Attach a policy button. See the Amazon Developer Guide, IoT policies documentation, for details of these procedures.

Set the network.tls variables to the files as follows:

set network.tls.client_cert <abc123...>.cert.pem
set network.tls.client_key <abc123...>.private.key
set network.tls.ca_cert <Root CA for AWS IoT>

Hardware Connection

Simplicity Studio Code

Download the PearlMqtt.zip code package. Extract the contents.

In main.c, change the variables to match your broker settings:

#define MQTT_PUB_TOPIC              "<TOPIC_NAME>"
#define MQTT_SUB_TOPIC              "<TOPIC_NAME>"
#define MQTT_CLIENT_ID              "<CLIENT_STRING>"
#define MQTT_USER                   "<USER_NAME>"
#define MQTT_PASSWORD               "<PASSWORD>"
#define MQTT_HOST                   "<YOUR_IOT_HOST>"
#define MQTT_PORT                   MQTT_SECURE_PORT or MQTT_CLEAR_PORT

Now, import the GeckoMQTT project into Simplicity Studio, build and download it to the target.

The main.c code demonstrates how to publish a message to the topic defined by MQTT_PUB_TOPIC, and how to subscribe to the topic defined by MQTT_SUB_TOPIC.

In main(), the call to MQTTSubscribe() supplies the receive_handler callback, which checks commands received.

Periodic publishing starts when the command, defined by COMMAND_START_PERIODIC, is received on the subscribed topic. Periodic publishing stops when the command, defined by COMMAND_STOP_PERIODIC, is received on the subscribed topic.

Supporting Gecko OS Editions and Versions

Change Log

ModifiedChanges
2017-11-14Created