# mqttClient Service Connect error?

**URL:** <https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093>\
**Category:** Uncategorized\
**Created:** [April 28, 2021, 10:01am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093 "2021-04-28T10:01:12Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 3, 2021, 12:26pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/68 "2021-05-03T12:26:54Z")

</div>

No idea then…

You might try other mqtt server

[https://cumulocity.com/guides/device-sdk/mqtt-examples/](https://cumulocity.com/guides/device-sdk/mqtt-examples/)

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 3, 2021, 12:49pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/69 "2021-05-03T12:49:29Z")

</div>

On the other hand the socket error is just a warning (seeing the code). Instead, the error is the following:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/2/22a8f1ca08741400f4b3daa4a9231ccc707968d4.png)

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 3, 2021, 1:03pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/70 "2021-05-03T13:03:09Z")

</div>

Didn’t LE\_FAULT is -6

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 3, 2021, 1:16pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/71 "2021-05-03T13:16:36Z")

</div>

I’m trying to analyze this error…thanks for the advice

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 3, 2021, 1:18pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/72 "2021-05-03T13:18:50Z")

</div>

Then that means it is not MQTTCLIENT\_NULL\_PARAMETER

```
 switch (connectResult)
    {
        case SOCKET_ERROR:
            LE_WARN("Socket error");
            result = LE_FAULT;
            break;

        case MQTTCLIENT_NULL_PARAMETER:
        case MQTTCLIENT_BAD_STRUCTURE:
        case MQTTCLIENT_BAD_UTF8_STRING:
            result = LE_BAD_PARAMETER;
            break;
```

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 3, 2021, 1:28pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/73 "2021-05-03T13:28:59Z")

</div>

I’m so sorry for bothering you. In this post:

![](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/1X/252837692301bb53b7d4b8041901ad6486ced526.png) [Legato Forum – 19 Feb 21](https://forum.legato.io/t/how-to-use-component-3rdparty-paho-mqtt-c/5612/21)

 ![](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/1X/0769edef158f9b8e088d3b63e9fc4ef0d66bf277.jpeg)

### [How to use component 3rdParty paho.mqtt.c](https://forum.legato.io/t/how-to-use-component-3rdparty-paho-mqtt-c/5612/21)

using the real ip I got the error return -6 Feb 19 14:56:41 fx30s user.info Legato: INFO | testPublisher[4655]/testPublisher T=main | testPublisher.c \_testPublisher\_COMPONENT\_INIT() 111 | mqttBrokerURI tcp://104.198.201.218:1883 Feb 19 14:56:41…

the guy has my same problem:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/2/2525bb41367b3cf52fce69e671b1eebfd862a43d.png)

and the solution seems to be:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/7/7cc2516c2f3881119ee35ea26f9357b88aca0e0e.png)

I’m wondering if I could be in the same case  
[/quote]

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 4, 2021, 3:06am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/74 "2021-05-04T03:06:03Z")

</div>

you might give a try on that.

You might also try other mqtt server

[https://cumulocity.com/guides/device-sdk/mqtt-examples/](https://cumulocity.com/guides/device-sdk/mqtt-examples/)

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 4, 2021, 11:01am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/75 "2021-05-04T11:01:43Z")

</div>

Hi @jyijyi as always I’m so sorry for bothering you. Unfortunately the supervisors necessarily want me to use mosquitto. I am desperate. In your opinion, could a COMPONENT\_INIT calling functions like this be correct? I do not know what to do. I can’t find any errors, debugging the MQTT\_Connect function.

COMPONENT\_INIT  
{  
#if MQTT\_SERVER\_USE\_SECURED\_CONNECTION  
const char mqttBrokerURI = “ssl://192.168.2.3:8883”;  
//const char mqttBrokerURI = “ssl://eu.airvantage.net:8883”;  
const uint8\_t mqttPassword = {‘S’, ‘W’, ‘I’};  
#else  
const char mqttBrokerURI = “tcp://192.168.2.3:1883”;  
//const char mqttBrokerURI = “tcp://eu.airvantage.net:1883”;  
//const char mqttBrokerURI = “tcp://127.0.0.1:1883”;  
#endif  
//const char mqttBrokerURI = “tcp://192.168.2.3:1883”;  
//const char mqttBrokerURI = “tcp://eu.airvantage.net:1883”;  
//const uint8\_t mqttPassword = {‘S’, ‘W’, ‘I’};  
//char clientId[32] = “kineton”;  
LE\_ASSERT\_OK(le\_info\_GetImei(DeviceIMEI, NUM\_ARRAY\_MEMBERS(DeviceIMEI)));  
char clientId[32];  
snprintf(clientId, sizeof(clientId), “%s-pub”, DeviceIMEI);  
//Create memory pools\*/  
MQTTSessionPoolRef = le\_mem\_CreatePool(“MQTT session pool”, sizeof(mqtt\_Session));  
le\_mem\_ExpandPool(MQTTSessionPoolRef, MQTT\_SESSION\_MAX);  
le\_mem\_SetDestructor(MQTTSessionPoolRef, mqttSessionDestructor);

```
UsernamePoolRef = le_mem_CreatePool("MQTT username pool", MQTT_MAX_USERNAME_LENGTH);
PasswordPoolRef = le_mem_CreatePool("MQTT password pool", MQTT_MAX_PASSWORD_LENGTH);
MessagePoolRef = le_mem_CreatePool("MQTT message pool", sizeof(mqtt_Message));
TopicPoolRef = le_mem_CreatePool("MQTT topic pool", MQTT_MAX_TOPIC_LENGTH);
PayloadPoolRef = le_mem_CreatePool("MQTT payload pool", MQTT_MAX_PAYLOAD_LENGTH);

// MessageHandlerRefMap is created with size (MQTT_SESSION_MAX * 2) since each MQTT session
// may have 2 handlers, i.e. 1 message arrived handler and 1 connection lost handler
SessionRefMap = le_ref_CreateMap("MQTT sessions", MQTT_SESSION_MAX);
MessageHandlerRefMap = le_ref_CreateMap("MQTT message handlers", MQTT_SESSION_MAX * 2);

ReceiveThreadEventId = le_event_CreateId(
    "MqttClient receive notification", sizeof(mqtt_Message*));
le_event_AddHandler(
    "MqttClient receive notification", ReceiveThreadEventId, MessageReceivedEventHandler);

ConnectionLostThreadEventId = le_event_CreateId(
    "MqttClient connection lost notification", sizeof(mqtt_Message*));
le_event_AddHandler(
    "MqttClient connection lost notification",
    ConnectionLostThreadEventId,
    ConnectionLostEventHandler);

res = mqtt_CreateSession(mqttBrokerURI, clientId, &MQTTSession);
  if (res != LE_OK){
      LE_INFO ("tutto male :( ");
  }
  else {
      LE_INFO("tutto ok");
  }

//inizia il mio codice

```

const uint16\_t keepAliveInSeconds = 30;  
const bool cleanSession = true;  
const char\* username = DeviceIMEI;  
//const char\* username = “kienton”;  
const uint16\_t connectTimeout = 20;  
const uint16\_t retryInterval = 10;  
mqtt\_SetConnectOptions(  
MQTTSession,  
keepAliveInSeconds,  
cleanSession,  
username,  
#if MQTT\_SERVER\_USE\_SECURED\_CONNECTION  
mqttPassword,  
NUM\_ARRAY\_MEMBERS(mqttPassword),  
#else  
NULL,  
0,  
#endif  
connectTimeout,  
retryInterval);

```
mqtt_AddConnectionLostHandler(MQTTSession, &OnConnectionLost, NULL);
mqtt_AddMessageArrivedHandler(MQTTSession, &OnMessageArrived, NULL);

sleep (5);
sleep (5);
sleep(5);

/*res = mqtt_Connect(MQTTSession);
if (res == LE_OK)
{
    LE_ERROR("Connection failed! error %d", res);
}
else
{
    LE_INFO("Connected to server '%s'", mqttBrokerURI);
}
//finisce il mio codice

```

\*/

LE\_FATAL\_IF(mqtt\_Connect(MQTTSession) != LE\_OK, “Connection failed”);

```
le_timer_Ref_t timer = le_timer_Create("MQTT Publish");
LE_ASSERT_OK(le_timer_SetHandler(timer, &publishTimerHandler));
LE_ASSERT_OK(le_timer_SetMsInterval(timer, 5000));
LE_ASSERT_OK(le_timer_SetRepeat(timer, 0));
LE_ASSERT_OK(le_timer_Start(timer));
LE_INFO("Publish timer started");
le_msg_AddServiceCloseHandler(mqtt_GetServiceRef(), DestroyAllOwnedSessions, NULL);

MQTTClient_init_options initOptions = MQTTClient_init_options_initializer;
initOptions.do_openssl_init = 1;
MQTTClient_global_init(&initOptions);

```

}

I used a timer in order to recall the function until there isn’t a connection to the server.  
I configured the broker in this way:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/f/f846a6d46d7aebf865969c108c87cfc6d22304cc.png)

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 4, 2021, 11:10am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/76 "2021-05-04T11:10:42Z")

</div>

As you can see, i’m trying also with AirVantage adress, but the error is the same

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 4, 2021, 11:27am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/77 "2021-05-04T11:27:45Z")

</div>

You can try other mqtt application if yours does not work

> [@Mangoh-to-gcloud](https://mangoh.discourse.group/t/mangoh-to-gcloud/897/24):
>
> Hey friend, These projects are not Legato Application. Can you give me more detail? Thanks

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 4, 2021, 11:32am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/78 "2021-05-04T11:32:11Z")

</div>

okay thank you. But is GCould free? In my company they don’t want to use it. Can I use it for free with a personal account?

[image]

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 4, 2021, 11:49am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/79 "2021-05-04T11:49:39Z")

</div>

No idea, you need to ask google

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 4, 2021, 12:36pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/80 "2021-05-04T12:36:10Z")

</div>

I can’t use it, I should pay. I can only use mosquitto 😥

---

<div class="post-metadata">

**Author:** ![jameswollaeger](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@jameswollaeger](https://mangoh.discourse.group/u/jameswollaeger)\
**Post date:** [May 4, 2021, 9:04pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/81 "2021-05-04T21:04:22Z")

</div>

I would advise not trying to use mosquitto on the VM. We have this same setup functional on a desktop running linux(ubuntu 18.04) natively. There is some routing issue where traffic won’t traverse to a VM. Its too complicated to troubleshoot. You can also run mosquitto server on a cheap raspberry pi.

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 5, 2021, 10:39am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/82 "2021-05-05T10:39:39Z")

</div>

I think that another parameter that comes me to have error is the url tcp or ssl.  
If I put tcp://eu.airvantage.net:1883 or tcp://mqtt.eclipse.org:1883 I obtain

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/d/dcf1cf1f1f27a78f32f5935587efe9cd3fee2e86.png)

If I put local adress such as 0.0.0.0:1883 I obtain only socket error.  
Can someone explain to me how to connect to a broker URL (I will be happy also with a local URL)

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 5, 2021, 10:54am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/83 "2021-05-05T10:54:06Z")

</div>

That is another issue that the program cannot resolver the ip by dns server

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 5, 2021, 10:55am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/84 "2021-05-05T10:55:55Z")

</div>

The root of my problems could be the fact that I’m using Ubuntu VM on virtual box and so a local adress is not simple to give?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/jyijyi/32/997_2.png) [@jyijyi](https://mangoh.discourse.group/u/jyijyi)\
**Post date:** [May 5, 2021, 10:59am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/85 "2021-05-05T10:59:21Z")

</div>

You might try with rea pc then

---

<div class="post-metadata">

**Author:** ![jameswollaeger](https://avatars.discourse-cdn.com/v4/letter/j/e495f1/32.png) [@jameswollaeger](https://mangoh.discourse.group/u/jameswollaeger)\
**Post date:** [May 5, 2021, 3:25pm UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/86 "2021-05-05T15:25:59Z")

</div>

In our case, we got an error when trying to connect to mqtt server URL when it was set to non-secured (TCP). When connecting to server over SSL (over port 8883), the issue resolved itself. For some reason, DNS resolution doesn’t work and we are forced to use the ip address of the server when SSL isn’t enabled.

Following this page(and all the associated linked pages) on the internet was enough to get us functional setup: [Using The Mosquitto\_pub and Mosquitto\_sub MQTT Client Tools- Examples](http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/)

---

<div class="post-metadata">

**Author:** ![gavinoimparato1](https://avatars.discourse-cdn.com/v4/letter/g/f475e1/32.png) [@gavinoimparato1](https://mangoh.discourse.group/u/gavinoimparato1)\
**Post date:** [May 6, 2021, 8:21am UTC](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093/87 "2021-05-06T08:21:27Z")

</div>

I tried, but unfortunately this is not the solution either. However, I noticed another thing. I have Mosquitto server installed on windows of another PC and from my Ubuntu I can ping (seeing the connection attempt on the other side):  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/1/1ba87fe317b49e252b200abd68936da012a0a041.png)

But if I repeat the same operation from my mangOH nothing happens:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/f/f7e51a8f193490cbf87550b498cb9c290eae426a.png)

![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/6/641865634627300c6c97a9ed14b085694dd04987.png)

It’s a normal thing? Should my mangOH be configured on a door? Because at this point it’s really not clear to me what I’m wrong  
I want to clarify that my mangOH is on Internet (also with WiFi and Data) so that I can ping google 8.8.8.8.

[Previous page](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093.md?page=3)

[Next page](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093.md?page=5)
