This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

mqtt_connect error -12 when I run simple_mqtt in continuous loop

Hello Community,

I successfully build and run mqtt_simple it works fine.

I edit the code  

//////////code///////////

void main(void)
{
u64_t start_time_mqtt;
while(1)
{
int err;

printk("The MQTT simple sample started\n");

modem_configure();

client_init(&client);

err = mqtt_connect(&client);
if (err != 0) {
printk("ERROR: mqtt_connect %d\n", err);
return;
}

err = fds_init(&client);
if (err != 0) {
printk("ERROR: fds_init %d\n", err);
return;
}
start_time_mqtt = k_uptime_get();
while (1) {
err = poll(&fds, 1, K_SECONDS(CONFIG_MQTT_KEEPALIVE));
if (err < 0) {
printk("ERROR: poll %d\n", errno);
break;
}

err = mqtt_live(&client);
if (err != 0) {
printk("ERROR: mqtt_live %d\n", err);
break;
}

if ((fds.revents & POLLIN) == POLLIN) {
err = mqtt_input(&client);
if (err != 0) {
printk("ERROR: mqtt_input %d\n", err);
break;
}
}

if ((fds.revents & POLLERR) == POLLERR) {
printk("POLLERR\n");
break;
}

if ((fds.revents & POLLNVAL) == POLLNVAL) {
printk("POLLNVAL\n");
break;
}
if(((k_uptime_get() - start_time_mqtt) >= 30000))
{
printk("start_time_mqtt TIMEOUT HSJJ...\n");
break;
}
}

printk("Disconnecting MQTT client...\n");

err = mqtt_disconnect(&client);
if (err) {
printk("Could not disconnect MQTT client. Error: %d\n", err);
}

printk("Disconnected ....\n");
for(int i=0;i<20;i++);
k_sleep(K_MSEC(500));
printk("delay over...\n");
}
}

1. I am running code in while loop

2. Once it is connected to cloud I added timeout 

3.  Once it is timed out I close socket

4. again it will try to connect MQTT

Now this code run 5 times fluently but after 5th try code stuck and throw error

mqtt_connect error -12

What changes are necessary to beat this error?

Thanks and Regards,

Nikunj  

Related