MQTT connection invalide socket
Answer
9/5/18 8:36 AM

Hi everyone,

I have the following problem. I have a MQTT connection from the XDK over WLAN to an lokal MQTT Broker. The connection is set up and I can send data over the connection.

The problem is, if I lose the connection to the broker or to the WLAN access point the xdk triggers an assertion. The code stops at the file "SDK/xdk110/Libraries/ServalStack/3rd-party/ServalStack/src/Msg/TcpMsg.c" in Line 91.

The assertion is triggerd 8-10 seconds after the connection abort, every time. So I think there is an timer which checks the connection.

Do you have an idea to handle this problem?

0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/5/18 2:17 PM as a reply to Lukas Dörr.
Hello Lukas,

the issue you are describing is the standard procedure of the Mqtt_connect function. If the connection is lost, an assert is triggered to stop future connection attempts.

Furthermore, if there is no Wi-Fi connection, there is no way to transmit data. In conclusion, the transmit attempt gets asserted.

May I ask, why this is actually an issue for you?

You mentioned that the connection is working. This means all that is necessary for your working project is to ensure, that the Wi-Fi router is and the MQTT broker are online and reachable.

Additionally, to bypass this error case, you could add functionality to monitor the Wi-Fi and MQTT broker connection and if no active connection is present, reconnect. This would prevent the assert from getting triggered.

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

Kind regards,
Franjo
+1 (1 Vote)
RE: MQTT connection invalide socket
Answer
9/6/18 6:08 AM as a reply to Franjo Stjepandic.

Hello Franjo,

thank you for your reply. The problem I have is exactly that what you are understand! If the connection is available there is no problem and my application works fine. My problem is, the XDK device will be installed in a portable device to measure the acceleration. I'm not sure if the device has a continuous connection to the broker the whole time.

So my plan is to send data if there is a connection available. If the connection is lost, so the XDK should wait for a new connection and if there is an new connection available to the broker the device should start sending data again.

At the moment, if the connection is lost, the device gets the assertion. So if I want to start sending data again I have to reboot the device.

Is there an opportunity to start an auto reconnect if the connection gets lost?

0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/11/18 7:18 AM as a reply to Lukas Dörr.
Hello Lukas,

Thank you for providing further information on your use case.

That is exactly what I thought about. You can of course, add the functionality to monitor the Wi-Fi and the MQTT broker connection. You could use the approach to monitor the Wi-Fi connection first. I assume it is the connection that will be lost more often.

For that, I recommend taking a look at this thread here . There, I explained in detail the steps to implement the monitoring function and reconnect routine for personal WPA Wi-Fi networks as well as Enterprise WPA2 Wi-Fi networks.

In regards, if you want to monitor the MQTT broker connection too, you would need to use the function Mqtt_isConnected() from the Serval_Mqtt.h interface. This function would be used to monitor the MQTT broker connection. If the connection is lost, a reconnect attempt through the function MQTT_ConnectToBroker() would start.

You can find the interface Serval_Mqtt.h by navigating to it over the following folderpath:

SDK > xdk110 > Libraries > ServalStack > 3rd-party > ServalSTack > api

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

Kind regards,
Franjo
0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/11/18 5:53 AM as a reply to Franjo Stjepandic.

Hey Franjo,

thank you. Your link to the WLAN thread is not working. Could you update the link?

Regards

Lukas

0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/11/18 6:56 AM as a reply to Lukas Dörr.
I checked the MQTT Connection problem but I am not sure how to handle the problem. If I lost the connection to the broker the assert is triggered, immediately (see picture). There is no chance to react with the mqtthandler because the program crashs before.
0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/11/18 2:59 PM as a reply to Lukas Dörr.
Hello Lukas,

Thank you for reaching out about the corrupted link. It should work now.

Furthermore, I was able to reproduce your documented issue.

The reason behind your issue is, they get fired during two certain events:
  • The first one gets triggered, if the keep alive time runs out and the MQTT broker terminates the connection.
  • The other, more crucial one shows up when the Wi-Fi connection gets lost.

In both cases, due to an invalid used socket, an assert from the inside of the function TcpMsg_receiveFromTcp() gets triggered.

To prevent this from happening, I recommend a slight modification to the code, by changing the line below:
 
assert(Tcp_isValidSocket(conn));

into an if-condition:
 
if(Tcp_isValidSocket(conn)) {
// initial content of the function TcpMsg_receiveFromTcp()
}
else {
return TCP_RECEIVE_ERROR;
}


If the condition is true, the initial content of the function TcpMsg_receiveFromTcp() will be processed. Otherwise, an TCP_RECEIVE_ERROR is returned, instead of an assert.

By the use of this modification, you should be able to monitor the Wi-Fi and also the MQTT connection, without the risk to deal with an assert if the Wi-Fi connection gets lost or the MQTT broker terminates the connection.

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

Kind regards,
Franjo

0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/12/18 12:14 PM as a reply to Franjo Stjepandic.

Hello Franjo,

thank you for your support. That fix solved my problem.

Today I let the sensor run the whole day. Sometimes I get an error and I am nor sure why.

 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: asserted at Filename 3rd-party/ServalStack/src/Mqtt/Mqtt.c , line no  131 
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15
 INFO | XDK DEVICE 1: MqttEventHandler : Event - 15

 

After this message, the device continues to send data but I can't reconnect anymore. If the error occure the red and yellow led turn on.

0 (0 Votes)
RE: MQTT connection invalide socket
Answer
9/13/18 3:12 PM as a reply to Lukas Dörr.
Hello Lukas,

The error which shows in the console as asserted at Filename 3rd-party/ServalStack/src/Mqtt/Mqtt.c , line no 131 is probably caused by an assert, which triggers, if a previous message has not been deallocated correctly.

Often, this relates to a inconvenient timed interval, in which the messages get sent out.
You could find the solution by trying out different sending intervals.

For the case this does not help, may I ask, which period you are currently using to send out MQTT messages?

Kind regards,
Franjo
0 (0 Votes)