Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] libmosquitto issues

Hello,

For your information, i would like to share with you that i am using raspberry pi as a platform to use "libmosquitto" in my code.


Thank you,

Sansheel kokne


On Saturday 10 December 2016 01:00 PM, Sansheel wrote:
Hello,

I am using "libmosquitto" for my MQTT client code and i am having an issue with mosquitto_loop_forever() API getting exited with an error. The error says "INVALID FUNCTION ARGUMENT PROVIDED". Please find my code snippet below.

-> Below is the function(fnMqttInit) which initializes mosquitto and connects to the server. -> After initialization, a thread is created where mosquitto_loop_forever() ran. -> Please find the thread function MqttLoopFun() below fnMqttInit() function. -> After initialization and thread creation, some subscription and publishing is done periodically. -> After few hours, the mosquitto_loop_forever() function exits with error "INVALID FUNCTION ARGUMENT PROVIDED" and thread gets exited. -> Also in the fnMqttInit(), mosquitto_connect() throws error "INTERRUPTED SYSTEM CALL" and says "Unable to connect To Broker" as per the implementation below but it gets connected to the server as the Acknowledge are received from the server through call backs and the
        publishing to topics is done successfully.

I would appreciate your help in finding issue as soon as possible. Thanks a lot in advance.


int fnMqttInit(void)
{
    printf("\nFunction  MQTT Initlization\n");

    int keepalive = 60;
    char clean_session = TRUE;
    int Return = 0;

    mosquitto_lib_init();
    mosq = mosquitto_new(NULL, clean_session, NULL);
    if(!mosq)
    {
        fprintf(stderr, "Error: Out of memory.\n");
        return 0;
    }
    mosquitto_log_callback_set(mosq, my_log_callback);
    mosquitto_connect_callback_set(mosq, my_connect_callback);
    mosquitto_message_callback_set(mosq, my_message_callback);
    mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
    mosquitto_publish_callback_set(mosq,my_publish_callback);
mosquitto_disconnect_callback_set(mosq,my_disconnect_callback);

    mosquitto_username_pw_set(mosq,Mqtt.UserName,Mqtt.Password);
printf("\n Mqtt.UserName:%s,Mqtt.Password:%s\n",Mqtt.UserName,Mqtt.Password); printf("\n Mqtt.HostName:%s,Mqtt.PortNo:%d\n",Mqtt.HostName,Mqtt.PortNo);
    usleep(5000 * 1000);
if(Return = mosquitto_connect(mosq,Mqtt.HostName,Mqtt.PortNo, keepalive))
    {
fprintf(stderr, "fnMqttInit : (mosquitto_connect) Error: %s\n", mosquitto_strerror(Return));
        syslog(LOG_INFO, "%s",mosquitto_strerror(Return));
        fprintf(stderr, "\nUnable to connect To Broker\n");
        g_bServerStatus = 0;
        return 0;
    }
    else
    {
        g_bServerStatus = 1;
        syslog(LOG_INFO, "%s",mosquitto_strerror(Return));
        printf("\n CONNECTED TO MQTT BROKER");
    }
    return 1;
}


void *MqttLoopFun(void *vargp)
{
    int rc=0;
    FILE *FileDesc=NULL;
    char MQTTErrFile[200]="/home/pi/MQTTErrFile.txt";
    //sleep(1);
    printf("\n In MQtt Loop Thread \n ");
    if(NULL == (FileDesc = fopen(MQTTErrFile,"a"))) {
        perror("Error opening MQTTErrFile File");
    }
    rc = mosquitto_loop_forever(mosq,0,1);
    printf("\nTES\n");
    if(rc == MOSQ_ERR_SUCCESS){
        printf("mosquitto_loop_forever() exited successfully\n");
    }else {
        printf("Error code : %s %d\n",mosquitto_strerror(rc),rc);
        fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
        fprintf(FileDesc,"Error: %s\n",mosquitto_strerror(rc));
syslog(LOG_INFO, "Mqtt thread - mosquitto_loop_forever error: %s",mosquitto_strerror(rc));
    }
    MqttLoopFlag = 1;
    fclose(FileDesc);
    return NULL;
}


Thank you,
Sansheel Kokne



Back to the top