Hi everyone,

I am using MQTT now. But there is a problem that xdk can connect to mqtt server which even it doesn't start and sometimes mqtt server was running and xdk can connect to the server but no response.

static void StartExample(void)
{
    retcode_t rc = RC_INVALID_STATUS,r;
    rc = Mqtt_connect(SessionPtr);
   // r = Mqtt_ping(SessionPtr);
    if (rc != RC_OK)
    {
        printf("Could not Connect, error 0x%04x\n", rc);
      //  Retcode_RaiseError(RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_CONNECT_FAIL));
    }

    else
    {
        printf(" XDK connected to MQTT Server : %s and is ready to publish sensor data\n\r",MqttBroker);
    }
}

static void ConfigureSession(void)
{
    // set target
    int8_t server_ip_buffer[13];
    Ip_Address_T ip;

    Retcode_T rc;

    rc = NetworkConfig_GetIpAddress((uint8_t *) MQTT_BROKER_HOST, &ip);
    if(RETCODE_OK != rc)
    {
        printf("Getting Broker address failure\n\r");
        Retcode_RaiseError(RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_IPCONIG_FAIL));
    }
    else
    {
        int32_t retval = Ip_convertAddrToString(&ip, (char *)server_ip_buffer);
        if(0 == retval)
        {
            printf("Ip_convertAddrToString return value is zero\n\r");
            Retcode_RaiseError(RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_IPCONIG_FAIL));
        }
        else
        {
            sprintf(MqttBroker, MqttBrokerAddressFormat, server_ip_buffer,
            MQTT_BROKER_PORT);

            rc = SupportedUrl_fromString((const char *)MqttBroker, (uint16_t) strlen((const char *)MqttBroker),
                    &SessionPtr->target);
            if(RC_OK != rc)
            {
                printf("SupportedUrl_fromString failure\n\r");
                Retcode_RaiseError(RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_MQTT_IPCONIG_FAIL));
            }
            else
            {
                SessionPtr->target.scheme = SERVAL_SCHEME_MQTT;

                printf("Broker address: %s\n\r", MqttBroker);

                // set event handler
                SessionPtr->onMqttEvent = EventHandler;

                // set Connect information
                SessionPtr->MQTTVersion = 3;
                SessionPtr->keepAliveInterval = 100;
                SessionPtr->cleanSession = true;
                SessionPtr->will.haveWill = false;

                StringDescr_T device_name_descr;
                StringDescr_wrap(&device_name_descr, DeviceName);
                SessionPtr->clientID = device_name_descr;


                // set publish and subscribe Topics as StringDescr
			    StringDescr_wrap(&PublishTopicDescription_LIGHT, (const char *)PublishTopic_LIGHT);

                Qos[0] = MQTT_QOS_AT_MOST_ONE;
            }
        }
    }
}


For battery status, I see there is five status :

  • Critical
  • Low
  • Normal
  • High
  • On charge

Now I can read battery but can I know whether XDK is charged or not.

Thank you at first.

Rachel

0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
7/27/18 12:57 PM as a reply to Rachel Wu.
Hello Rachel,

Could you please get more into detail about what is happening, when you try to connect with the SendDataOverMQTT example?

As far as I understand, you have the issue that you are able to connect to an MQTT broker, which is currently not running, but a connection is still established? Otherwise, if the broker is running you have the issue that a connection can be established, but you do not receive any notification that the connection was established?

Am I right so far?

In regards to the battery level, only the battery voltage is measured, not the actual battery charge. As such, you can only reflect over the battery voltage if the XDK is charged or not. The five states you mentioned are describing the conditions a certain battery voltage can have.

While every condition value is describing a specific voltage for a specific battery charge of Li-Ion batteries.

For example: The state Normal , would be equal to about 80% battery charge, while the state High would be equal to 100% battery charge.

In that regard, I recommend investing some time to get familiar about the behavior of Li-Ion batteries. Then you will have it easier to analyse the functionality of the battery monitoring interface. A good starting point for that would be the Battery University .

Please let me know if that was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
7/30/18 7:23 AM as a reply to Franjo Stjepandic.

Hello Franjo,

I will check Battery University.
What you described MQTT is exactly right. Do you have any suggestions?

Thank you at first.

Kind regards

Rachel

0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
7/30/18 2:25 PM as a reply to Rachel Wu.
Hello Rachel,

I assume you are using the SendDataOverMQTT example from the Welcome Screen of the XDK-Workbench?

There, the function MQTT_connect() is used to establish a connection between an MQTT client and an MQTT broker.

If you take a look at the example, you will find the EventHandler function.

In this function, you can define the functionality to be executed on the different MQTT events. Especially the handling of two cases are relevant here. These are MQTT_CONNECTION_ESTABLISHED and MQTT_CONNECTION_ERROR .

In your case it would be important to understand, if the MQTT_connect() returns RC_OK , but receives due to an offline (or not reachable) broker no acknowledgement as and therefore no MQTT_CONNECTION_ESTABLISHED or MQTT_CONNECTION_ERROR event.

That would currently result in, that the function MQTT_connect() is still trying to connect and no timeout is triggered.

If that is the case, it would be necessary to check if the MQTT broker is in general reachable with another MQTT client.

Additionally, it would be necessary to check with printf() function calls, if any events are returned by the MQTT API in case of the MQTT connection.

Otherwise, could you please go more into detail about how you determine that your XDK is successfully connecting to the MQTT broker? Are you logging the network traffic via Wireshark , for example?

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/1/18 2:28 PM as a reply to Franjo Stjepandic.

Hi Franjo,

I am using mosquito and set up MQTT Broker in my laptop. So I can see information from the terminal.

And I notice that if no response I just can see there is" new connection from "XDK IP address"" and if it can connect successfully I can see "New client connected from "XDK IP address" as "client name"" in mosquito.

So dose it means xdk didn't get response from MQTT Broker?

Now my solution is to restart xdk. But after restarting several times I got ths arror :asserted at Filename 3rd-party/FreeRTOS/Source/queue.c , line no  1372 
 INFO | XDK DEVICE 2: System Error:, Severity:2 Error code: 1 Package Id:30 and module ID is :8 

For me, the best solution is to reconnect to MQTT Broker, And then it will enter to MQTT_CONNECTION_ERROR or other "wrong" event.

Do you have any ideas about it?

Thank you at first.

Rachel

0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/2/18 2:55 PM as a reply to Rachel Wu.
Hello Rachel,

Thank you for providing further information.

Since you are getting at least in your local MQTT broker a response that an XDK is connecting, it might indicate correct behavior. Obscure is that the XDK is not behaving as expected.

Furthermore, could you please provide the outline of how you implemented the event handling for MQTT? It might give me a clue what might goes wrong.

Furthermore, the error regarding :asserted at Filename 3rd-party/FreeRTOS/Source/queue.c , line no 1372 is pointing to an empty queue during initialization. Are you implementing something with queues and the command processor in your XDK application, which could trigger that?

The other error is coming from the ServalPAL package and is indicating that a hidden called function is returning RETCODE_FAILURE . As far as I understand that, it could be related to the XDK restart.

However, here it would be nice if you could provide the outline of how you implemented the ServalPAL setup. That might also give me a clue what might goes wrong.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/7/18 8:24 AM as a reply to Franjo Stjepandic.

Hi Franjo,

I find the reason for connection falling is that internet delay. According to a document of MQTT, it will lose connection after the time of keep alive. So after keepalive, the function MQTT_connect()seems to be trying to connect actually xdk already lose the connection with MQTT Broker. Is it right? How can I know it already lose connection and reconnect again?

Thank you at first.

Rachel

0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/7/18 7:30 PM as a reply to Rachel Wu.
Hello Rachel,

thank for the hint with the internet delay.

To check if the connection is active or lost you could try the following approach.

The Serval_MQTT.h library provides all necessary functions. The first one is MQTT_isConnected() , which returns either true (if MQTT session is connected) or false (if MQTT session is not connected).

Expecially the return code false is usable for an if-instruction. There you can call the MQTT_connect() function to reconnect, if the the first request returns a false.

The last step is for you to decide regarding the delay between actual disconnect and the call of the MQTT-isConnected() function.
This delay defines the offline duration of the MQTT connection. I assume that the best approach is to define a timer for the offline monitoring.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/8/18 8:58 AM as a reply to Franjo Stjepandic.

Hi Franjo,

Thank you for your answer. I tried to create a timer to monitor the time.

static void StartExample(void)
{
	retcode_t rc = RC_INVALID_STATUS;
	TickType_t lastxTicks=0 ,mills=0;
	static char *currenttime;
	int tries=0;
	while (1)
	{
		if(mills -lastxTicks > 120000||tries==0)
		{
		 	tries++;
		 	printf("tries:%i\n\r",tries);
		 	mills = xTaskGetTickCount();
		 	lastxTicks = mills;
		 	currenttime = timestamp();
		 	rc = Mqtt_connect(SessionPtr);
		 	if (rc != RC_OK)
		 	 	{
		 	 		printf("  Could not Connect, error 0x%04x\n", rc);
		 	 	}
		 	 		else
		 	 	{
		 	 		printf("  Waiting for connection to MQTT Server : %s\n\r",MqttBroker);
		 	 	}
		 }
		 else
		 {
		 	mills = xTaskGetTickCount();
		 }
	}
}

But It can enter in event 0 but then it will enter in event 4.

Is the function I defined right?

Thank you at first

Rachel

0 (0 Votes)
RE: MQTT connection Problem and battery status
Answer
8/8/18 5:30 PM as a reply to Rachel Wu.
Hello Rachel,

Unfortunately, this kind of implementation will only to monitor the time a connection attempt requires.

As I assume, you are trying to build a reconnect routine to reconnect to the MQTT broker, after the XDK loses its connection to it.

In regards to that you need to monitor the events, which are thrown by the MQTT API. For example, MQTT_CONNECTION_CLOSED , which indicates that the connection was closed. Additionally, it is also possible via the function MQTT_isConnected() to monitor the current connection status.

For the routine, the easiest approach would be to call the function MQTT_connect() after the event MQTT_CONNECTION_CLOSED was thrown.

Additionally, it would also be possible to insert the connection check with MQTT_isConnected() in the function you are using to publish data.

Please let me know if this was helpful and feel free to ask if you have any further questions.

Kind regards,
Franjo
0 (0 Votes)