RA Flexible Software Package Documentation  Release v6.2.0

 
lwIP MQTT

This module provides the lwIP MQTT integration documentation.

Overview

The lwIP MQTT library can connect to a third party MQTT broker such as Mosquitto. The documentation for the library can be found at the following link: lwIPMQTT.

Features

Usage Notes

TLS connection

The stack and heap sizes must be properly configured when using the TLS layer with MbedTLS. Depending on the specific configuration and use case, these values may need to be increased from their defaults. For more information on memory requirements, refer to the Memory Usage section in Mbed Crypto H/W Acceleration (rm_psa_crypto).

Note
When using hardware acceleration for the MbedTLS implementation, verify that the target device supports all required mbedCrypto features for the TLS layer of lwIP. Refer to the HW Overview and Features sections in Mbed Crypto H/W Acceleration (rm_psa_crypto) to check hardware support. If any required feature is not supported, configure the corresponding features to use the software crypto implementation instead.

Examples

Basic Example

This is a basic example of minimal use of the lwIP MQTT APIs in an application.

#if LWIP_ALTCP && LWIP_ALTCP_TLS
#include "lwip/altcp_tls.h"
#include "mbedtls/platform.h"
#endif
#include "lwip/tcp.h"
#include "lwip/timeouts.h"
#include "lwip/init.h"
#include "lwip/ip4.h"
#include "lwip/apps/mqtt.h"
#define LWIP_MQTT_EXAMPLE_PORT_NUMBER 8883
#define LWIP_MQTT_EXAMPLE_TOPIC "/lwip/example"
#define LWIP_MQTT_EXAMPLE_TOPIC_LENGTH 13
#define LWIP_MQTT_EXAMPLE_BUF_SIZE 50
#define LWIP_MQTT_EXAMPLE_KEEP_ALIVE 100
/* LwIP MQTT callback functions. */
static void lwip_mqtt_example_connection_callback(mqtt_client_t * client, void * arg, mqtt_connection_status_t status);
static void lwip_mqtt_example_pub_sub_request_callback(void * arg, err_t result);
static void lwip_mqtt_example_incoming_publish_callback(void * arg, const char * topic, u32_t tot_len);
static void lwip_mqtt_example_incoming_data_callback(void * arg, const u8_t * data, u16_t len, u8_t flags);
volatile uint8_t g_lwip_mqtt_example_connected_flag;
volatile uint8_t g_lwip_mqtt_example_receive_incomming_msg_flag;
volatile uint8_t g_lwip_mqtt_example_pub_sub_flag;
static unsigned char g_mqtt_lwip_example_incoming_msg[LWIP_MQTT_EXAMPLE_BUF_SIZE];
static char g_mqtt_lwip_example_incoming_topic[LWIP_MQTT_EXAMPLE_BUF_SIZE];
/* LwIP MQTT connection information */
struct mqtt_connect_client_info_t lwip_mqtt_example_client_info =
{
"lwip_mqtt_example",
NULL, /* user */
NULL, /* pass */
LWIP_MQTT_EXAMPLE_KEEP_ALIVE, /* keep alive */
NULL, /* will_topic */
NULL, /* will_msg */
0, /* will_qos */
0 /* will_retain */
#if LWIP_ALTCP && LWIP_ALTCP_TLS
, NULL
#endif
};
#if LWIP_ALTCP && LWIP_ALTCP_TLS
/* Example ca certificate, needs to be updated. */
static uint8_t gs_ca_crt[] =
"-----BEGIN CERTIFICATE-----\n" \
"MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n" \
"ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n" \
"b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n" \
"MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n" \
"b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n" \
"ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n" \
"9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n" \
"IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n" \
"VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n" \
"93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n" \
"jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n" \
"AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n" \
"A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n" \
"U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n" \
"N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n" \
"o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n" \
"5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n" \
"rqXRfboQnoZsG4q5WTP468SQvvG5\n" \
"-----END CERTIFICATE-----";
#endif
void lwip_mqtt_example () {
struct netif netif;
mqtt_client_t * g_lwip_mqtt_example_client;
ip_addr_t ipaddr;
ip_addr_t netmask;
ip_addr_t gw;
ip_addr_t server_ip_address;
const char data[] = "lwIP MQTT Hello world.";
/* Initializing lwIP core. */
lwip_init();
/* Set ip address of the board. */
IP4_ADDR(&ipaddr, 192, 168, 10, 4);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 10, 1);
/* Initialize netif. Please pass rm_lwip_ether_init() function and the instance of rm_lwip_ether to netif_add(). */
netif_add(&netif, &ipaddr, &netmask, &gw, &g_lwip_ether0_instance, rm_lwip_ether_init, netif_input);
/* Set netif as default and enable */
netif_set_default(&netif);
netif_set_up(&netif);
netif_set_link_up(&netif);
IP4_ADDR(&server_ip_address, 192, 168, 10, 1);
/* Initialize lwIP MQTT Client */
g_lwip_mqtt_example_client = mqtt_client_new();
/* Establish MQTT connection to server */
g_lwip_mqtt_example_connected_flag = false;
#if LWIP_ALTCP && LWIP_ALTCP_TLS
/* Create a TLS client configuration */
struct altcp_tls_config * configuration =
altcp_tls_create_config_client((const u8_t *) gs_ca_crt, sizeof(gs_ca_crt));
/* Initializing TLS configuration */
lwip_mqtt_example_client_info.tls_config = configuration;
#endif
assert(ERR_OK ==
mqtt_client_connect(g_lwip_mqtt_example_client, &server_ip_address, LWIP_MQTT_EXAMPLE_PORT_NUMBER,
lwip_mqtt_example_connection_callback, NULL, &lwip_mqtt_example_client_info));
/* Waiting for connection to be established. */
while (false == g_lwip_mqtt_example_connected_flag)
{
sys_check_timeouts();
}
/* Set callback for receiving incomming messsage from subscribed MQTT topic */
g_lwip_mqtt_example_receive_incomming_msg_flag = false;
mqtt_set_inpub_callback(g_lwip_mqtt_example_client,
lwip_mqtt_example_incoming_publish_callback,
lwip_mqtt_example_incoming_data_callback,
NULL);
/* Subcribe to MQTT topic with Qos 0 */
g_lwip_mqtt_example_pub_sub_flag = false;
assert(ERR_OK ==
mqtt_subscribe(g_lwip_mqtt_example_client, LWIP_MQTT_EXAMPLE_TOPIC, 0,
lwip_mqtt_example_pub_sub_request_callback, NULL));
/* Waiting for subcribe process completed. */
while (false == g_lwip_mqtt_example_pub_sub_flag)
{
sys_check_timeouts();
}
/* Publish message to MQTT topic with Qos 0 and retain flag disabled */
g_lwip_mqtt_example_pub_sub_flag = false;
assert(ERR_OK ==
mqtt_publish(g_lwip_mqtt_example_client, LWIP_MQTT_EXAMPLE_TOPIC, data, strlen(data), 0, 0,
lwip_mqtt_example_pub_sub_request_callback, NULL));
/* Waiting for publish process completed. */
while (false == g_lwip_mqtt_example_pub_sub_flag)
{
sys_check_timeouts();
}
/* Waiting for receive incomming process completed. */
while (false == g_lwip_mqtt_example_receive_incomming_msg_flag)
{
sys_check_timeouts();
}
/* Make sure receiving correct topic and message */
assert(0 == memcmp(g_mqtt_lwip_example_incoming_topic, LWIP_MQTT_EXAMPLE_TOPIC, sizeof(LWIP_MQTT_EXAMPLE_TOPIC)));
assert(0 == memcmp(g_mqtt_lwip_example_incoming_msg, data, sizeof(data)));
/* Unsubcribe to MQTT topic */
assert(ERR_OK ==
mqtt_unsubscribe(g_lwip_mqtt_example_client, LWIP_MQTT_EXAMPLE_TOPIC,
lwip_mqtt_example_pub_sub_request_callback, NULL));
/* Close MQTT connection and netif. */
mqtt_disconnect(g_lwip_mqtt_example_client);
netif_remove(&netif);
mqtt_client_free(g_lwip_mqtt_example_client);
}
/* LwIP MQTT callback functions */
void lwip_mqtt_example_incoming_publish_callback (void * arg, const char * topic, u32_t tot_len)
{
memcpy(g_mqtt_lwip_example_incoming_topic, topic, tot_len);
}
void lwip_mqtt_example_incoming_data_callback (void * arg, const u8_t * data, u16_t len, u8_t flags)
{
if (flags & MQTT_DATA_FLAG_LAST)
{
memcpy(g_mqtt_lwip_example_incoming_msg, data, len);
g_lwip_mqtt_example_receive_incomming_msg_flag = true;
}
}
void lwip_mqtt_example_connection_callback (mqtt_client_t * client, void * arg, mqtt_connection_status_t status)
{
if (MQTT_CONNECT_ACCEPTED == status)
{
g_lwip_mqtt_example_connected_flag = true;
}
}
void lwip_mqtt_example_pub_sub_request_callback (void * arg, err_t result)
{
if (ERR_OK == result)
{
g_lwip_mqtt_example_pub_sub_flag = true;
}
}