1.20 TCP/IP WolfMQTT 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 WolfMQTT 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 WolfMQTT Application

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

Figure 1-60. Project Graph

wolfSSL component is added for secure connection which supports TLS.

wolfCrypt module enabled with MD5 and SHA authentication

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 WolfMQTT Application

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-61. TCP/IP Configuration - PIC32CZ CA70 Curiosity Ultra

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-21. Demo Configurations
Project NameTarget DeviceDevelopment BoardDescription
pic32cz_ca70_curiosity_ultra.XPIC32CZ2051CA70144 PIC32CZ CA70 Curiosity Ultra + LAN8720 PHY Daughter BoardTCP/IP WolfMQTT Application - Bare Metal
pic32cz_ca70_curiosity_ultra_freertos.XPIC32CZ2051CA70144 PIC32CZ CA70 Curiosity Ultra + LAN8720 PHY Daughter BoardTCP/IP WolfMQTT Application - FreeRTOS

Hardware Setup - PIC32CZ CA70 Curiosity Ultra

The target board for running the application is PIC32CZ CA70 Curiosity Ultra.

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

Figure 1-62. PIC32CZ CA70 Curiosity Ultra

Board Setup

To set up the hardware, refer to the PIC32CZ CA70 Curiosity Ultra User's Guide.

  • Default jumper setting of the board is shown above.
  • Connect the micro USB cable between DEBUG USB port on the board and host PC.
  • Establish a connection between the router/switch with the PIC32CZ CA70 Curiosity Ultra board through the RJ45 connector, using the Ethernet cable.

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 virtual COM port.
  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.

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.

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.