lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] Callbacks in MQTT


From: Bas Prins
Subject: Re: [lwip-users] Callbacks in MQTT
Date: Tue, 9 Aug 2022 13:22:16 +0200

Hi,
Not claiming I am an expert, but we do have a similar setup (Mosquitto, mcu as lwip mqtt client).
I am also using freertos as OS, maybe that confuses things a bit. After establishing a connection (we use ppp over serial, with an LTE modem), it is indeed simply calling connect, and await the callback (sent from mqtt.c, line 702)

Here a copy/paste from what we use

LOCK_TCPIP_CORE();
_client = mqtt_client_new();
UNLOCK_TCPIP_CORE();

struct mqtt_connect_client_info_t clientInfo = { };
clientInfo.client_id = id.c_str();

etl_short_string will_topic;
fmt::format_to(std::back_inserter(will_topic), "t/{}/{}", id.c_str(), iobox_status_topic);

clientInfo.will_topic = will_topic.c_str();
clientInfo.will_msg = "lost connection";
clientInfo.will_qos = 2;
clientInfo.keep_alive = 60;

LOCK_TCPIP_CORE();
err_t error = mqtt_client_connect(
_client,
&_ip_address,
item.get_mqtt_port(),
connect_callback,
this,
&clientInfo);
UNLOCK_TCPIP_CORE();

if (error == ERR_OK)
{
// If connect returned OK, we expect a callback (with accept)
_free_rtos.semaphore_take(_callback_semaphore, portMAX_DELAY);

// Set the callbacks for subscriptions once after successful connect
LOCK_TCPIP_CORE();
mqtt_set_inpub_callback(
_client,
subscription_topic_received_callback,
subsription_payload_received_callback, this);
UNLOCK_TCPIP_CORE();

return _connected;
}

in the callback I check for the connection state:

void mqtt_client::connect_callback(
mqtt_client_t *client,
void *arg,
mqtt_connection_status_t status)
{
auto me = reinterpret_cast<mqtt_client*>(arg);

me->_logger.write_debug_to<50>(logging_category_mqtt, "connect callback: {}", status);
switch (status)
{
case MQTT_CONNECT_ACCEPTED:
if (me->_connected)
{
// If already connected and we receive an ACCEPT, the server restarted
// We want to reconnect and resubscribe
mqtt_disconnect(client);
mqtt_client_free(client);
// Wait a bit to be sure we no longer receive
// data for delete client
vTaskDelay(500);
}
else
{
me->_connected = true;
me->_free_rtos.semaphore_give(me->_callback_semaphore);
}
break;
case MQTT_CONNECT_DISCONNECTED:
case MQTT_CONNECT_TIMEOUT:
mqtt_disconnect(client);
mqtt_client_free(client);
me->_connected = false;
// Wait a bit to be sure we no longer receive
// data for delete client
vTaskDelay(500);
me->_free_rtos.semaphore_give(me->_reconnect_semaphore);
me->_free_rtos.semaphore_give(me->_callback_semaphore);
break;
default:
// Ignore for now
project_assert(false, error_code_mqtt_unknown_error_connection_lost);
break;
}


I never receive the state 'connecting'. Are you sure you have a working ppp connection?

Op di 9 aug. 2022 om 07:17 schreef Ganesh Thambhahalli Satyen <gthambha@mathworks.com>:

Hi,

A gentle reminder about these queries.

Regards,

Ganesh

 

 

From: Ganesh Thambhahalli Satyen
Sent: 01 August 2022 13:10
To: 'lwip-users@nongnu.org' <lwip-users@nongnu.org>; erian747@gmail.com
Cc: Dhanashree Mohite <dmohite@mathworks.com>
Subject: RE: Callbacks in MQTT

 

Hi,

As mentioned in the previous mail, we are trying to use the MQTT application present in LWIP along with a local Mosquitto broker. The callbacks  provided in the detailed description here - lwIP: MQTT client (nongnu.org) are being used.

 

In mqtt_connection_cb, it can be seen that the connection state needs to be MQTT_CONNECT_ACCEPTED for the the mqtt_set_inpub_callback to be called which in turn will call the incoming_publish and incoming_data callbacks which are needed for the subscribe data.

 

As per our observation, in the example_do_connect function, the connection state post mqtt_client_connect call is TCP_CONNECTING and not MQTT_CONNECT_ACCEPTED. Further, mqtt_connection_cb is not entered. This leads us to ask the following questions:

  1. When is the mqtt_connection_cb called?
  2. In mqtt_client_connect it can be observed that the connection state is manually set to TCP_CONNECTING. Thus, does it imply that the connection state needs to be manually set to another state?
  3. What is the flow for the connection state to reach MQTT_CONNECT_ACCEPTED?

Eagerly awaiting your response

Regards,

Ganesh

 

From: Ganesh Thambhahalli Satyen
Sent: 28 July 2022 13:47
To: lwip-users@nongnu.org
Cc: Dhanashree Mohite <dmohite@mathworks.com>
Subject: Callbacks in MQTT

 

Hi,

We are trying to use the MQTT application present in LWIP along with a local Mosquitto broker. As per the documentation, the connect callback is supposed to be called after the connection goes through. We are not able to replicate the same when we utilise the detailed description provided here -  lwIP: MQTT client (nongnu.org). The client gets connected, but the callback is not entered. The conclusion that the callback is not entered is based on the flags we have added at the start and end of the callback. Is there any parameter change/ change in the order of calling which needs to be done?

Eagerly awaiting your response

Regards,

Ganesh

 

_______________________________________________
lwip-users mailing list
lwip-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/lwip-users

reply via email to

[Prev in Thread] Current Thread [Next in Thread]