SendDataOverMQTT does not reconnect
Answer
6/13/18 8:53 AM

Hello,

I'm not a developer so far, I'm only able to put parts together and sometimes my FrankenCodes work ;)
 

I'm using the SendDataOverMQTT demo code straight from the workbench, I've added the username and password login to the broker.

Now my Issue, what can I do to make the XDK reconnect if W-Lan is lost?

This is the error message I get as soon as W-Lan is lost:

 INFO | XDK DEVICE 1: Unhandled MQTT Event Number: 4
 INFO | XDK DEVICE 1: Mqtt_publish is failed:Stack erro code :7429 
 INFO | XDK DEVICE 1: System Error:, Severity:2 Error code: 65 Package Id:153 and module ID is :8 
 INFO | XDK DEVICE 1:  Reset the system

Is it possible to implement a wait loop to check if the connection comes back?
If so how?

I've published my edited code on GitLab here:

https://gitlab.com/antonionardella/boschxdkmqttpoc

Thanks,
Antonio

0 (0 Votes)
RE: SendDataOverMQTT does not reconnect
Answer
6/14/18 8:38 AM as a reply to Antonio Nardella.
Hello Antonio,

a WiFi connect routine is fairly common, and quite easy to implement.

Since you want to use this reconnect routine in an MQTT client implementation and given the fact that MQTT is event-based, I would recommend to listen to the MQTT event MQTT_CONNECTION_CLOSED in the event handler.

There, I would recommend to first stop all timers that try to send data via MQTT (since the MQTT connection is closed). Then, check the current WiFi status using WlanConnect_GetCurrentNwStatus() . If this network status is anything other than the enum value CONNECTED_AND_IPV4_ACQUIRED , then it most likely means there is a connection loss.

If the WiFi connection is lost, you can simply call WlanConnect_WPA(...) , with the SSID and PW you configured as inputs. Once the connection is reestablished, call Mqtt_connect(SessionPtr) to reconnect.

Finally, I would suggest that you also change the way the publish-timer is created and started. In the original code, this is done in the event handler by reacting to the event MQTT_SUBSCRIPTION_ACKNOWLEDGED using the function CreateAndStartPublishingTimer() .

In an use case that implements a reconnect routine, I would actually suggest that the timer is created elsewhere (preferably within InitializeExample() ), and the timer is started in the event handler by reacting to the connection event.

This will prevent creating a new timer for every reconnect.

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

Kind regards,
Alex
0 (0 Votes)