Lab 5 - How to Integrate MQTT to an MPLAB® X IDE Project to Publish and Subscribe Data To/From a Cloud Broker

Last modified by Microchip on 2025/07/07 15:05

Introduction

This guide outlines the process of adding Message Queuing Telemetry Transport (MQTT) using an WIxCS02 M.2 Wi-Fi® Module Card into a MPLAB® X IDE project using the SAM9X75 Early Access Curiosity Wireless Kit. This enables embedded systems to connect to wireless networks and facilitates cloud communication through the use of an MQTT broker. This project showcases how embedded systems can interact with IoT platforms for real-time data monitoring.

This project was developed as a bare metal MPLAB Harmony application using MPLAB X IDE.

This application will use the MQTT broker, broker.emqx.io, to subscribe or publish messages. You can use the MyMQTT mobile application to connect to the MQTT broker. This example demonstrates how the SAM9X75 Curiosity Development Board publishes real-time temperature data, read using the Temp&Hum13 Click, in response to a data request sent from the MyMQTT mobile application.

Back to top

Prerequisites

Follow these steps:

Add Project Graph and Develop a MPLAB Harmony Application

Follow the steps in Lab 1 and ensure you have cloned all the necessary libraries to create this thermostat MPLAB Harmony application.

To publish or subscribe the messages via the MQTT broker, it is necessary to establish Wi-Fi communication using the WIxCS02 M.2 Wireless Module Card on the SAM9X75 Curiosity wireless board by following Lab 4.

In this training module, you can learn how to connect to home or mobile Wi-Fi, read the current date from SNTP server and print it in the serial console.

Before moving to this lab, we recommend learning how to transmit the temperature value read from Temp&Hum13 Click by following to a mobile phone using Microchip Bluetooth Data (MBD) mobile application by following Lab 3.

Back to Top


The next step is to add RNWF WINCS MQTT services to the project graph. To do this, from wireless, launch the project graph of the project created by following Lab 4. Then, launch MPLAB Code Configurator (MCC) and from Device Resource, under Wireless > System Services, add RNWF WINCS Mqtt Service as shown in the accompanying image.

Project graph

Back to Top


Now enable the RNWF WINCS MQTT Service in the RNWF WINCS Wi-Fi Service configuration option.
Add Wi-Fi service to project graph

 

Back to Top


The next step is to do MQTT configurations on the SAM9X75 Curiosity Thermostat board. Configure RNWF WINCS02 MQTT configurations with the following information:

  • MQTT Protocol version: v3.1.1
  • Cloud Port: 1,883
  • Cloud URL: broker.emqx.io
  • Client Id: $MCHP/Wireless/device01
  • Publish Topic Name: $MCHP/Wireless/device01
  • Publish Message: Hi. It's MCHP Thermostat Device
  • Sub Topic 0: $MCHP/Wireless/device02

MQTT Config

 

Back to Top


Save and then click the Generate button. This will generate code for all the device resources that have been added in the project graph.
Generated Code

Back to Top


Up to this point, you used MCC to configure and generate code to initialize the device (SAMA9X75). All that is left is for you to do is to write the application code in the main.c and app.c file.

Documentation for each of the peripheral libraries or driver libraries can be accessed as follows:

  • BSP libraries APIs can be found in bsp.h.
  • For the debug peripheral libraries' APIs description and usage, see the "2.27 Debug Unit (DBGU)" page.
  • MPLAB Harmony Wireless Wi-Fi- MQTT API documentation can be found ​​​​on the "1.1.18 Client - MQTT APIs" page.

Back to Top


In this example, you can learn how to publish/subscribe to topics using MQTT.

When the status is requested, the temperature value read using the Temp&Hum 13 Click boardTM will be broadcast from the SAM9X75 Curiosity kit.

See the example application source files, app_wincs02.c and app_wincs02.h, to connect to MQTT using M.2 WINCS02 module used in a thermostat application for reference.

APIs used for this application are as follows:

  • SYS_WINCS_MQTT_SrvCtrl(SYS_WINCS_MQTT_SET_CALLBACK,...)

  • SYS_WINCS_MQTT_SrvCtrl(SYS_WINCS_MQTT_CONFIG,...)

  • SYS_WINCS_MQTT_SrvCtrl(SYS_WINCS_MQTT_CONNECT,...)

Back to Top


The source file can be downloaded and added to the project as shown:Add source files to the project

Similarly, the header file can be added as shown:

Add header files to the project

Back to Top


The  app_wincs02_task should be called periodically to connect to Wi-Fi and to publish/subscribe topics via MQTT. (See the sample app.c file.)

Back to Top


Now, clean and build the code. (See the sample application project.)​​​​

Back to Top


To debug this application, follow Steps 1 through 13 of Lab 7.

Run the application from the MPLAB X IDE. Turn on the mobile hotspot/home Wi-Fi configured with:

  • Username: DEMO_AP
  • Password: password

The WINCS02 module will auto-detect the Wi-Fi and read the time from the SNTP server and print the debug log:

Debug Output

The MQTT subscribe and publish messages can also be noted in the debug log. 

Back to Top


To connect to MQTT, download the ​​​​MyMQTT mobile application.

On your Apple® device, locate the App Store® icon and download MYMQTT.

Launch the mobile application and set the parameters as shown below:

  • Host: broker.emqx.io
  • Port: 1883
  • MQTT Protocol: MQTT V3  

The following details are needed when running the application:
The client ID of SAM9X75 Curiosity based Thermostat application device is: $MCHP/Wireless/device01, which is subscribed to the topic $MCHP/Wireless/device02.

From the MyMQTT mobile application, use the following:

  • Publish
    • Topic: $MCHP/Wireless/device02 
  • Subscribe
    • Topic: $MCHP/Wireless/device01 

Using the MQTT broker MyMQTT mobile application, publish the message "status" to the topic $MCHP/Wireless/device02. The SAM9X75 Curiosity, which is subscribed to this topic, receives the message, processes it and responds by publishing the real-time temperature value read from the Temp&Hum13 Click. This temperature data is then received and displayed in the dashboard of the MyMQTT mobile application, as shown:
MQTT output

Back to Top


To boot the application from a microSD card, follow these steps:

Create a harmony.bin file by following Step 14 from Lab 7.

Now, copy the generated harmony.bin file from your project directory and boot.bin file to the microSD card.

Insert the microSD card into the SD card J14 slot. Power the board on. Press the reset button (sw3) on the board.

Observe the output as mentioned in the previous step.

Summary

In this training, you developed, configured and built an MPLAB Harmony v3 application that connects to a mobile hotspot/home Wi-Fi, reads the current date from the SNTP server for the SAM9X75 Curiosity and prints it on the serial debug console. This project also demonstrates a simple yet effective implementation of MQTT communication between the MyMQTT mobile application and the SAM9X75 Curiosity Development Kit. Upon receiving a data request via MQTT, the board responds by publishing real-time temperature readings from the Temp&Hum13 Click sensor.

Back to Top

Learn More

Back to Top