# Automatic data connection

**URL:** <https://mangoh.discourse.group/t/automatic-data-connection/909>\
**Category:** mangOH Red\
**Created:** [November 16, 2017, 6:25am UTC](https://mangoh.discourse.group/t/automatic-data-connection/909 "2017-11-16T06:25:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://mangoh.discourse.group/u/basti)\
**Post date:** [November 16, 2017, 6:25am UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/1 "2017-11-16T06:25:34Z")

</div>

Hi,

I am trying to create an application that publishes data to a MQTT broker, and after publishing going into ULPM. I am using MQTT client from [https://github.com/mangOH/MqttClient/tree/use\_paho\_linux\_version](https://github.com/mangOH/MqttClient/tree/use_paho_linux_version). My issue is that the application will crash if there is no data connection and my question is: is there a way to verify board has data connection before trying to publish data?

Thanks!

---

<div class="post-metadata">

**Author:** ![dfrey](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/dfrey/32/303_2.png) [@dfrey](https://mangoh.discourse.group/u/dfrey)\
**Post date:** [November 17, 2017, 7:39pm UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/2 "2017-11-17T19:39:37Z")

</div>

Hi basti,

I think a better approach would be to try to make it so that attempting to publish when there’s not data connection is a detectable error, but doesn’t cause anything to crash.

---

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://mangoh.discourse.group/u/basti)\
**Post date:** [November 20, 2017, 5:51am UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/3 "2017-11-20T05:51:15Z")

</div>

Hi,

and thanks for the answer! I will try that approach. Would you use le\_data API to achieve this, or is there another API that would be more suitable?

And is there a demo/example of an application that would use this?

---

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://mangoh.discourse.group/u/basti)\
**Post date:** [November 20, 2017, 11:06am UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/4 "2017-11-20T11:06:44Z")

</div>

I found an example I could follow (dcsTestComp), but I cannot publish to broker using it for some reason. If I manually input cm data connect before running application, there are no issues. And using the dcsTestComp example I can see there is a connection established. le\_data\_TryConnectService returns LE\_OK and cm data gives:  
Index: 1  
APN: [internet.sierrawireless.com](http://internet.sierrawireless.com)  
PDP Type: IPV4  
Connected: yes  
Interface: rmnet0  
Family: inet  
IP: 100.71.213.157  
Gateway: 100.71.213.158  
Dns1: 8.8.8.8  
Dns2: 4.2.2.2

But /etc/resolv.conf is empty. If you use cm data connect it will have “8.8.8.8 4.2.2.2” in it.

Any ideas what could be going on?

---

<div class="post-metadata">

**Author:** ![dfrey](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/dfrey/32/303_2.png) [@dfrey](https://mangoh.discourse.group/u/dfrey)\
**Post date:** [November 20, 2017, 3:15pm UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/5 "2017-11-20T15:15:07Z")

</div>

You might also want to look through this code example which uses the data connection service and ULPM. It uses le\_avdata rather than MQTT though.  
[https://github.com/mangOH/mangOH/tree/master/samples/tutorials/developerDay2016](https://github.com/mangOH/mangOH/tree/master/samples/tutorials/developerDay2016)

---

<div class="post-metadata">

**Author:** ![basti](https://avatars.discourse-cdn.com/v4/letter/b/fbc32d/32.png) [@basti](https://mangoh.discourse.group/u/basti)\
**Post date:** [November 23, 2017, 11:11am UTC](https://mangoh.discourse.group/t/automatic-data-connection/909/6 "2017-11-23T11:11:29Z")

</div>

Hi @dfrey,

Thanks for the answer, I had pretty much the same solution but could not get it to work. I noticed that wifiService was also crashing because i had no SSID. After removing that app I was able to find a “workaround”, but maybe you could clarify or give a solution.  
When I am launching my MQTT app I need to put in a delay (sleep) for dcsDaemon to run:  
dcsDaemon[791]/le\_pa\_dcs T=main | pa\_dcs\_general.c AddNameserversToResolvConf() 305 | Set DNS ‘8.8.8.8’ '4.2.2.2’  
When this happens, the application has connection and works fine.

So right now my workaround is havin a small delay before checking “LE\_FATAL\_IF(mqtt\_Connect(mqttSession) != LE\_OK, “Connection failed”);”. Thank you!
