MQTT Setup using Giant Gecko & Medusa AMW007
This example demonstrates connecting a Giant 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
<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> dfu_update -f
The DMS Claim and DFU (Device Firmware Update) process is discussed 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:
- Execute the command
setup_webfrom the console. This runs the Web App, on the AMW007 Soft AP HTTP Server.
- From your PC, scan for Wi-Fi networks. Connect to the network with SSID
Gecko OS Web Setup ###, where
###is replaced by the last 3 digits of the module MAC address (to verify your device MAC address, issue the command
- In your PC browser, open the URL:
http://10.10.10.1/. This opens the Web App.
- In the Web App, select
Filesin the menu to open the Files tab.
- Download the CA cert, client cert and client key files to the module. See Web App, Files 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>
<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.
In the AWS IoT Services section, from the menu choose
Create a certificate screen, choose
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.
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>
- Connect EXP Header USART1_TX (pin 4) to Medusa H3 URX (pin 3)
- Connect EXP Header USART1_RX (pin 6) to Medusa H3 UTX (pin 4)
- Connect EXP Header GND (pin 1) to Medusa H3 GND (pin 9)
Simplicity Studio Code
Download the GiantMqtt.zip code package. Extract the contents.
main.c, change the variables to match your MQTT 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.
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
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
- Gecko OS-1.2+