This module provides the lwIP MQTT integration documentation.
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
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];
const struct mqtt_connect_client_info_t lwip_mqtt_example_client_info =
{
"lwip_mqtt_example",
NULL,
NULL,
100,
NULL,
NULL,
0,
0
#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.";
lwip_init();
IP4_ADDR(&ipaddr, 192, 168, 10, 4);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 10, 1);
netif_add(&netif, &ipaddr, &netmask, &gw, &g_lwip_ether0_instance,
rm_lwip_ether_init, netif_input);
netif_set_default(&netif);
netif_set_up(&netif);
netif_set_link_up(&netif);
IP4_ADDR(&server_ip_address, 192, 168, 10, 1);
g_lwip_mqtt_example_client = mqtt_client_new();
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));
while (false == g_lwip_mqtt_example_connected_flag)
{
sys_check_timeouts();
}
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);
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));
while (false == g_lwip_mqtt_example_pub_sub_flag)
{
sys_check_timeouts();
}
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));
while (false == g_lwip_mqtt_example_pub_sub_flag)
{
sys_check_timeouts();
}
while (false == g_lwip_mqtt_example_receive_incomming_msg_flag)
{
sys_check_timeouts();
}
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)));
assert(ERR_OK ==
mqtt_unsubscribe(g_lwip_mqtt_example_client, LWIP_MQTT_EXAMPLE_TOPIC,
lwip_mqtt_example_pub_sub_request_callback, NULL));
mqtt_disconnect(g_lwip_mqtt_example_client);
netif_remove(&netif);
mqtt_client_free(g_lwip_mqtt_example_client);
}
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;
}
}