Jump to main content

##

# 1.17 TCP/IP WolfSSL MQTT Application

The wolfMQTT application demonstrates a simple MQTT application using the 3rd party wolfMQTT library.

The application uses the console to implement a simple set of commands to start a MQTT connection to a selected broker.

The connection parameters can be adjusted using various MQTT related commands.

This demonstration uses the NET_PRES layer which supports encrypted communication through an external service provider for TLS support (usually wolfSSL).

## Description

The TCP/IP WolfSSL MQTT application is based on MPLAB® Harmony 3 TCP/IP Stack.

The Harmony TCP/IP stack is a collection of different network protocols.

The source files of Harmony 3 TCP/IP stack is available here.

The demo is created with MPLAB X IDE and MCC plugin.

MCC Project Graph - TCP/IP WolfSSL MQTT

The following Project Graph shows the Harmony components included in the TCP/IP WolfSSL MQTT demo application.

Figure 1-51.Project Graph

TCP sockets calculate the ISN using the wolfSSL crypto library.

FreeRTOS component is required for RTOS application. For bare-metal (non-RTOS) FreeRTOS component should not be selected.

TCP/IP WolfMQTT Configurations

- The configuration options for the wolfMQTT are the standard ones supported by wolfMQTT (see wolfMQTT site). Note that the project has already selected the 3rd party wolfMQTT and wolfSSL libraries (for supporting encrypted connections).
- The NET_PRES layer is also needed because the Harmony wolfMQTT NET glue layer uses the NET_PRES to handle the connections to the broker.
- Use the github repositories for wolfMQTT and wolfSSL.
- To configure the wolfMQTT and the wolfMWQTT NET glue layer, select the "wolfMQTT Library" module:
- The configuration options for the wolfMQTT NET glue allow the application to:
  - force TLS mode
  - enable IPv6 operation
  - set the maximum length of the MQTT broker
  - enable the NET glue debug and debug strings
  - set the allocation functions
  - set the MQTT TX and RX buffer sizes
  - generate the custom MQTT application template
  - the files app_mqtt_task.c, app_mqtt_task.h and app_mqtt_commands.c will be added to the project

TCP/IP Configuration - TCP/IP WolfSSL MQTT

The TCP/IP modules enabled for the demo is shown in the TCP/IP Configuration Overview.

More details of TCP/IP Configuration plugin is available here.

Figure 1-52.SAM9X60-Curiosity

The Application Layer modules enabled in the demo are as follows:

- Application Layer Modules
  - Announce to discover the Microchip devices within a local network.
  - DHCP Client to discover the IPv4 address from the nearest DHCP Server.
  - DNS Client provides DNS resolution capabilities to the stack.
  - SNTP Simple Network Time Protocol updates its internal time periodically using a pool of public global time servers.

## Downloading and Building the application

To clone or download this application from Github, go to the main page of this repository and then click Clone button to clone this repository or download as zip file.

This content can also be downloaded using content manager by following these instructions.

Path of the application within the repository is apps\wolfmqtt_demo\firmware.

To build the application, refer to the following list of demo configurations and open the project using MPLAB X IDE.

Table 1-17.Demo Configurations
Project Name | Target Device | Development Board | Description
--- | --- | --- | ---
sam_9x60_curiosity.X | SAM9X60D1G | SAM9X60-Curiosity | TCP/IP WolfSSL MQTT with an on-board KSZ8081 PHY - Bare Metal
sam_9x60_curiosity_freertos.X | SAM9X60D1G | SAM9X60-Curiosity | TCP/IP WolfSSL MQTT with an on-board KSZ8081 PHY - FreeRTOS

## Hardware Setup - SAM9X60-Curiosity

The target board for running the application is SAM9X60-Curiosity.

This section provides essential hardware configuration of this target board to run TCP/IP applications.

Figure 1-53.SAM9X60-Curiosity Development Board

Board Setup

To set up the hardware, refer to the section Board Setup in SAM9X60-Curiosity User's Guide.

- Connect the micro-USB cable from the computer to connector J1 (USB_A port) on the SAM9X60-Curiosity Development Board.
- Connect a USB-UART converter from the computer to J11 (UART DEBUG port) on the SAM9X60-Curiosity Development Board.
- Connect a debugger/programmer(like MPLAB® ICD) from the computer to the J12 (JTAG connector) on the SAM9X60-Curiosity Development Board.
- Establish a connection between the router/switch with the SAM9X60-Curiosity Development Board through the RJ45 connector.

## Setting up at91bootstrap loader

To load the application binary onto the target device, we need to use at91bootstrap loader.

Refer to the at91bootstrap loader documentation for details on how to configure and run/debug the application using MPLABX and booting application using an SD Card.

## Running the Application

1. Open a terminal application on the host computer (like Hyper-terminal or Tera Term).
2. Configure the terminal application for serial port connected to USB-UART converter.
3. Set baud rate as 115200 in the terminal application.
4. Build and download the application project on the target board.
5. Verify the TCP/IP Stack initialization console messages.

Console Start up

If DHCP client is enabled in the demonstration, wait for the DHCP server to assign an IP address for the development board.

This will be printed on the console. Otherwise, the default static IP address will be assigned to the board.

By entering the command netinfo, the response is obtained as shown above.

WolfSSL MQTT Test

- The wolfMQTT demo is interactive, using a set of special MQTT commands that have been added to this demo.
- Pressing "mqtt help" at the command prompt displays the list of the available commands/options:

Most of the commands are self explanatory.

All settings come with default values.

A brief description of the most important settings/commands follows:

- "mqtt broker": sets the MQTT broker to connect to. The default value is "iot.eclipse.org".
- "mqtt port": sets the MQTT port to use. Default is 1883.
- "mqtt qos": setd the QoS. Default is MQTT_QOS_0.
- "mqtt topi": sets the MQTT topic. Default is "MQTT_NET_Client topic".
- "mqtt tls": sets the force TLS flag. Default is 0.
- "mqtt user": sets the user name. Default is "MQTT_NET_User".
- "mqtt txbuf": sets the size of the TX buffer for the connection. Default is 1024.
- "mqtt rxbuf": sets the size of the RX buffer for the connection. Default is 1024.

Note: The connection to the broker is normally done using the port number: 1883 for plain connections, 8883 for encrypted ones.

Wolf MQTT Connection:

Using the "force TLS" will force an encrypted connection regardless of the port number.

The wolfMQTT demo is based on the examples provided by wolfMQTT.

To allow a simple usage of the wolfMQTT library, a wolfMQTT NET Glue layer has been added to the Harmony port.

The NET glue layer takes care of all the connections to the broker, supporting both encrypted and plain connections.

This way the application does not have to be concerned with the communication mechanisms and can use directly the wolfMQTT API on a Harmony platform without the need to add other supporting code.

The configuration parameters for the wolfmqtt demonstration are defined in the app_mqtt_task.h file that's part of the project.

The configuration parameters for the NET glue layer can be configured with MCC (see MCC Configuration).

The demo starts with all parameters having default values (see above).

If needed, parameters can be adjusted as the application requires.

Then a connection to the broker can be started using the command: "mqtt start".

A successful connection should render the following output:

- The "mqtt stop" command can be used to request the ongoing MQTT transaction to be aborted.
- The "mqtt stat" can be used to display the current state of the MQTT connection cycle.