1.20 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).

TCP/IP WolfSSL MQTT MCC Configuration

The following Project Graph diagram shows the Harmony components included in this WolfSSL MQTT demonstration application.

  • MCC is launched by selecting Tools > Embedded > MPLAB® Code Configurator from the MPLAB X IDE and after opening the project, TCP/IP demo project is ready to be configured and regenerated.

  • TCP/IP Root Layer Project Graph

    The root layer project shows that USART1 peripheral is selected to do read and write operation for TCP/IP commands.

    This is the basic configuration with SYS_CONSOLE, SYS_DEBUG and SYS_COMMAND modules. These modules are required for TCP/IP command execution.

    tcpip_same70_v71_project

    NOTE:-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:

      tcpip_same70_v71_project
    • 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

    1. SAM E70 Xplained Ultra

      tcpip_same70_v71_project

  • TCP/IP Required Application

    TCP/IP demo use these application module components for this demo.

    Announce module to discover the Microchip devices within a local network.

    DHCP Client module to discover the IPv4 address from the nearest DHCP Server.

    DNS Client provides DNS resolution capabilities to the stack. During these components selection, the required transport and network modules are also selected.

    SNTP Simple Network Time Protocol updates its internal time periodically using a pool of public global time servers.

  • TCPIP Data Link Layer

    Internal ethernet driver(gmac) is enabled with the external LAN8740 PHY driver library for SAME70 demonstartion.

    The MIIM Driver supports asynchronous read/write and scan operations for accessing the external PHY registers and notification when MIIM operations have completed.

TCP/IP WolfSSL MQTT Hardware Configuration

This section describes the hardware configuration for ATSAM E70 Xplained Ultra Evaluation Kit and one can be used for the respective application demonstration.

  1. This section describes the required default hardware configuration use SAM E70 Xplained Ultra Evaluation Kit

    • Ensure ERASE jumper is Open.

    • Open the J805 Jumper

    • Refer to the SAM E70 Xplained Ultra User Guide

      required_hardware
    • Insert the LAN8740 PHY daughter board on the ETHERNET PHY MODULE header.

    • Connect the micro USB cable from the computer to the DEBUG USB connector on the SAM E70 Xplained Ultra Evaluation Kit

    • Establish a connection between the router/switch with the SAM E70 Xplained Ultra Evaluation Kit through the RJ45 connector

      required_hardware

TCP/IP WolfSSL MQTT Running Application

This table list the name and location of the MPLAB X IDE project folder for the demonstration.

Project NameTarget DeviceTarget Development BoardDescription
sam_e70_xult.XATSAME70Q21BSAME70 Xplained Ultra + LAN8740 PHY Daughter boardDemonstrates the wolfMQTT on development board with ATSAME70Q21B device and LAN8740 PHY daughter board. This implementation is based on Bare Metal ( non-RTOS).
sam_e70_xult_freertos.XATSAME70Q21BSAME70 Xplained Ultra + LAN8740 PHY Daughter boardDemonstrates the wolfMQTT on development board with ATSAME70Q21B device and LAN8740 PHY daughter board. This implementation is based on Freertos.

Running Demonstration Steps

  1. Build and download the demonstration project on the target board.

  2. If the board has a UART connection:

    1. A virtual COM port will be detected on the computer, when the USB cable is connected to USB-UART connector.

    2. Open a standard terminal application on the computer (like Hyper-terminal or Tera Term) and configure the virtual COM port.

    3. Set the serial baud rate to 115200 baud in the terminal application.

    4. See that the initialization prints on the serial port terminal.

    5. When the 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 serial port terminal.

      • Alternatively: Use the Announce service or ping to get the IP address of the board.

      • Run tcpip_discoverer.jar to discover the IPv4 and IPv6 address for the board.

  3. Execution : 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:

  4. Most of the commands are self explanatory.

    All settings come with default values.

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

    tcpip_wolfmqtt_server_project
  • "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.

  1. 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.

    tcpip_wolfmqtt_server_project