RA Flexible Software Package Documentation  Release v5.8.0

 
All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
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

Examples

Basic Example

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

#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
/* 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 */
const struct mqtt_connect_client_info_t lwip_mqtt_example_client_info =
{
"lwip_mqtt_example",
NULL, /* user */
NULL, /* pass */
100, /* keep alive */
NULL, /* will_topic */
NULL, /* will_msg */
0, /* will_qos */
0 /* will_retain */
#if LWIP_ALTCP && LWIP_ALTCP_TLS
, NULL
#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;
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;
}
}