Query regarding MQTT Paho Library used into ESP32

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Query regarding MQTT Paho Library used into ESP32

Postby Ritesh » Sat Feb 03, 2018 4:37 pm

Hi,

We are working on ESP32 boards and are using ESP32 IDF 2.1 SDK so far for development purpose. So, We have one requirement to use MQTT for message communication through our cloud server.

We had use MQTT Paho Library and developed our wrapper on top of it for connection and communication purpose. So, We had tested it with some basic test-cases and it is working fine.

But, We are using ESP32 board in STA mode to connect with Router and our Cloud Server. We had one test-case in which We had just disconnected device from cloud but internally it is connected with Router at that time we are not getting any callback or event for disconnection from MQTT Paho Library.

If i disconnect completely ESP32 Device from Router then we are getting disconnect event. So, We had checked MQTT Paho library with its parameters and found that there is one parameter called it as keep alive interval which have set it as 10 seconds.

So, MQTT Paho Library internally going to ping on every keep alive interval time but after that we are getting disconnect event from MQTT Paho library which should not be happened.

Does anyone has faced this type of issue and how anyone has handled disconnect event in which device is connected with router internally but not externally?
Regards,
Ritesh Prajapati

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Query regarding MQTT Paho Library used into ESP32

Postby WiFive » Sat Feb 03, 2018 7:57 pm

What do you mean by "disconnected device from cloud" ? If the server closes the tcp connection then device should know but if server just "disappears" then you have to wait for timeout.

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Query regarding MQTT Paho Library used into ESP32

Postby Ritesh » Sun Feb 04, 2018 10:47 am

WiFive wrote:What do you mean by "disconnected device from cloud" ? If the server closes the tcp connection then device should know but if server just "disappears" then you have to wait for timeout.
Hi,

Let me explain it in details with examples so that you can understand it what I want to explain regarding this MQTT Paho Library issue.

We had started to create our ESP32 based Gateway and used MQTT Paho Library for Cloud communication with ESP32 based Devices.

Code: Select all

void mqtt_yalgaar_task(void *ignore) {
	DEBUG_LOGI(UART,TAG, "Starting ...");
	
	sendBuf = (unsigned char*)malloc(MQTT_BUFFER_SIZE);
	if(sendBuf == NULL)
	{
		DEBUG_LOGE(UART,TAG,"sendBuf malloc failed");
		vTaskSuspend(NULL); //vTaskDelete(NULL); 
	}
	readBuf = (unsigned char*)malloc(MQTT_BUFFER_SIZE);
	if(readBuf == NULL)
	{
		DEBUG_LOGE(UART,TAG,"readBuf malloc failed");
		free(sendBuf);
		vTaskSuspend(NULL); //vTaskDelete(NULL); 
	}

	while(1)
	{
		
		
		Init_network(&network);
		// NetworkInit(&network);
		int ret = 0;
		MQTTString clientId = MQTTString_initializer;
		MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	
		DEBUG_LOGI(UART,TAG,"connecting to MQTT server ... ");
		
		DEBUG_LOGD(UART,TAG, "NetworkConnect  ...");
		
		DEBUG_LOGI(UART,TAG,"conf.serveraddr :: %s", conf.serveraddr);
		
		ret = connect_network(&network,conf.serveraddr);
		// ret = NetworkConnect(&network,"api.yalgaar.io",1883);
		
		if(!ret)
		{
			DEBUG_LOGI(UART,TAG,"connected to mqtt server");
	
			DEBUG_LOGD(UART,TAG, "MQTTClientInit  ...");
			//~ MQTTClientInit(&client, &network,
				//~ 1000,            // command_timeout_ms
				//~ sendBuf,         //sendbuf,
				//~ MQTT_BUFFER_SIZE, //sendbuf_size,
				//~ readBuf,         //readbuf,
				//~ MQTT_BUFFER_SIZE  //readbuf_size
			//~ );
			
			MQTTClientInit(&client, &network,
				30000,            // command_timeout_ms
				sendBuf,         //sendbuf,
				MQTT_BUFFER_SIZE, //sendbuf_size,
				readBuf,         //readbuf,
				MQTT_BUFFER_SIZE  //readbuf_size
			);

			client.defaultMessageHandler = messageHandler_func_yalgaar;
			clientId.cstring = cli_id;

			data.clientID          = clientId;
			data.willFlag          = 0;
			data.MQTTVersion       = 3;
			//data.keepAliveInterval = 0;
			data.keepAliveInterval = 60;
			//data.cleansession      = 0;
			data.cleansession      = 1;

			DEBUG_LOGI(UART,TAG,"clientID : %s\r\n",data.clientID.cstring );
			ret = MQTTConnect(&client, &data);
			if (ret != SUCCESS) {
				DEBUG_LOGE(UART,TAG, "MQTTConnect: %d", ret);
				if(connect_call)
				{
					if(ret>100)
					connect_call(yalgaar_error_string[ret-100]);
				} 
			}
			connect_flag = true;
			
			DEBUG_LOGI(UART,TAG, "MQTTConnect : ret %d",ret);
			
			 if(!ret)
			 {
				if(connect_call)
				{
					connect_call(yalgaar_error_string[0]);
				} 
				while (1)
				{
					MQTTYield(&client, 1000);
					if (!MQTTIsConnected(&client))
					{
						DEBUG_LOGE(UART,TAG,"Disconncted.......");
						break;
					}
					//~ else
					//~ {
						//~ DEBUG_LOGI(UART,TAG,"Connected.......");
					//~ }
				}
			 }
			 else
			 {
                DEBUG_LOGE(UART,TAG,"failed to connect mqtt");
             }
			if(!connect_flag)
			{
				connect_flag = false;
				break;
			}
			
			MQTTDisconnect(&client);
			disconnect_network(&network);
		}
		else 
		{
			DEBUG_LOGE(UART,TAG,"failed to connect to mqtt server");
			disconnect_network(&network);
		}
	vTaskDelay(2000);	
	
	DEBUG_LOGI(UART,TAG,"AFter 2000 msec delay.......");
	}	
	if(sendBuf != NULL)
	{
		free(sendBuf);
		sendBuf = NULL;
	}
	
	if(readBuf != NULL)
	{
		free(readBuf);
		readBuf = NULL;
	}
    //vTaskSuspend(NULL); //vTaskDelete(NULL);
    
    DEBUG_LOGI(UART,TAG,"Before vTaskDelete call.......");
    vTaskDelete(NULL);
}
So, Above is sample code which we have developed using MQTT Paho Library for overall cloud connect, data communication and disconnection features.

In above configuration list, if I set data.keepAliveInterval = 60; then every 60 seconds it is going to ping to configured server and wait for next 60 seconds ideally but in our case we are getting disconnect event suddenly after that again going to connect it which should not be happened ideally if external connectivity is there.

So, We had checked over cloud server side and found that we are getting pin request but some how not receiving response of ping request which is being sent every 60 seconds as per configurable time.

Also, we had turned of external network in between but internal network is still exist at that time also we are getting any disconnect event and it seems like cloud is connected which is not in real case.

I had also checked this type of issue into issue list of MQTT Paho Library but not found this type of issue into that list. So, Might be we are missing something into MQTT configuration or in handling mechanism.

So, Do you have any idea or any clue to handle this type of situation or scenario? Please let me know if you are still unclear about out issue or query for same.
Regards,
Ritesh Prajapati

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Query regarding MQTT Paho Library used into ESP32

Postby Ritesh » Sun Feb 04, 2018 5:31 pm

Hi,

Few more informations like we had used MQTT Paho Library Link.

https://github.com/eclipse/paho.mqtt.em ... /README.md
Regards,
Ritesh Prajapati

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Query regarding MQTT Paho Library used into ESP32

Postby Ritesh » Tue Feb 06, 2018 5:17 am

Hi WiFive,

Do you need anything else means need some more informations regarding this issue?

Let me know if need any more informations regarding same.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: No registered users and 136 guests