# MqttClient folder with Mosquitto?

**URL:** <https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944>\
**Category:** Uncategorized\
**Created:** [April 8, 2021, 10:41am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944 "2021-04-08T10:41:51Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [April 8, 2021, 10:41am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/1 "2021-04-08T10:41:51Z")

</div>

Hi guys!  
I want to enstabilish a MQTT communication between:

- Mosquitto MQTT for the broker side
- MqttClient APP for the client side  
I read that alternatively I can use paho for the client side, but I didn’t understand how.

I compiled the MqttClient App that is in the mangOH project:

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

I included it in the main green.sdef:

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

So I updated it on my mangOH and the app is wrightly started:  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/8/81e38846dfe6f07a70835b59883374569d20e21d.png)

I obtain the following log, I can see an error:

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

Can you say me if I have to do other things. Can you give me same exmamples to implement on my mangOH green in order to understand if the mqtt commununication works well?

I installed yesterday mosquitto MQTT:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/0/0d1338845b97f8831e9a3eee41d725bd8fc4a2a3.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:** [April 8, 2021, 10:58am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/2 "2021-04-08T10:58:25Z")

</div>

Successful log should be like this

> **[How to use component 3rdParty paho.mqtt.c](https://forum.legato.io/t/how-to-use-component-3rdparty-paho-mqtt-c/5612/19)**
>
> it worked very well with the local broker (192.168.2.3) but when trying to use a remote broker on the internet, I get the following error: URL: tcp://mqtt.dioty.co:1883 ========================================================= Feb 19 10:45:31 fx30s...

You can see here on how to run mqtt

> **[legatoproject/legato-af](https://github.com/legatoproject/legato-af/tree/master/apps/sample/mqtt)**
>
> master/apps/sample/mqtt

---

<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:** [April 8, 2021, 11:07am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/3 "2021-04-08T11:07:17Z")

</div>

Thank you for the quickly answer, but I don’t have paho.mqtt.c in 3rdParty folder. Have I to download it? I thought I had to use the application in your mangOH layer …

---

<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:** [April 8, 2021, 11:19am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/4 "2021-04-08T11:19:09Z")

</div>

Seems to be here

> **[legatoproject/legato-af](https://github.com/legatoproject/legato-af/tree/master/components/3rdParty/paho.mqtt.embedded-c)**
>
> master/components/3rdParty/paho.mqtt.embedded-c

---

<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:** [April 8, 2021, 11:24am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/5 "2021-04-08T11:24:33Z")

</div>

okay, I found it, it was in a different path. I can to run it, thank you

---

<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:** [April 8, 2021, 1:18pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/6 "2021-04-08T13:18:03Z")

</div>

When I start **app start start mqttPublisher** and **app start mqttSubscriber** of your example I obtain this log…

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/b/b55d329f07be4529f323f86965ec50f3403b2e25.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:** [April 8, 2021, 1:34pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/7 "2021-04-08T13:34:29Z")

</div>

you need to debug why there is socket error…  
e.g. capturing wireshark log, adding debug message , etc

---

<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:** [April 8, 2021, 2:04pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/8 "2021-04-08T14:04:27Z")

</div>

what are these commands and where have I to write them?  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/c/ce08f98237b64dc949c69fee30fabec192eea4c6.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:** [April 13, 2021, 10:41am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/9 "2021-04-13T10:41:26Z")

</div>

okay I compiled this example, I launched  
 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/b/b5e7f651c0714163c42e8e6be8aa0c0550b19f2b.png)

The log:

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

But:

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

What am I wrong?

---

<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:** [April 13, 2021, 10:50am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/10 "2021-04-13T10:50:22Z")

</div>

it asked you to type -help.  
Actually you have the source code, you should able to add debug message for debugging.  
Also mosquitto is 3rd application, I have no knowledge on it.

---

<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:** [April 13, 2021, 10:56am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/11 "2021-04-13T10:56:46Z")

</div>

I have a connection error…

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/c/c52383e9630823e8e95404859612be062ee1c892.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:** [April 13, 2021, 11:16am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/12 "2021-04-13T11:16:58Z")

</div>

I think it is a good chance for you to debug and adding debug message on testsubscriber.c.

Enjoy!

---

<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:** [April 13, 2021, 11:36am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/13 "2021-04-13T11:36:03Z")

</div>

okay  
you say in the Component init part? But then is the same thing of helloWorld example?  
#include “interfaces.h”

//--------------------------------------------------------------------------------------------------  
/\*\*

- Whether connection to MQTT server is secured  
\*/  
//--------------------------------------------------------------------------------------------------  
#define MQTT\_SERVER\_USE\_SECURED\_CONNECTION 0

//--------------------------------------------------------------------------------------------------  
/\*\*

- Device IMEI, used as a unique device identifier  
\*/  
//--------------------------------------------------------------------------------------------------  
char DeviceIMEI[LE\_INFO\_IMEI\_MAX\_BYTES];

//--------------------------------------------------------------------------------------------------  
/\*\*

- MQTT session reference  
\*/  
//--------------------------------------------------------------------------------------------------  
mqtt\_SessionRef\_t MQTTSession;

//--------------------------------------------------------------------------------------------------  
/\*\*

- Call-back function called on lost connection  
_/  
//--------------------------------------------------------------------------------------------------  
void OnConnectionLost  
(  
void_ context  
)  
{  
LE\_ERROR(“Connection lost!”);  
}

//--------------------------------------------------------------------------------------------------  
/\*\*

- Call-back function called on arrived message  
_/  
//--------------------------------------------------------------------------------------------------  
void OnMessageArrived  
(  
const char_ topic,  
const uint8\_t\* payload,  
size\_t payloadLen,  
void\* context  
)  
{  
char payloadStr[payloadLen + 1];  
memcpy(payloadStr, payload, payloadLen);  
payloadStr[payloadLen] = ‘\0’;  
LE\_INFO(“Received message! topic: “%s”, payload: “%s””, topic, payloadStr);  
}

COMPONENT\_INIT  
{  
// server is running on the Linux workstation connected to the target  
#if MQTT\_SERVER\_USE\_SECURED\_CONNECTION  
const char mqttBrokerURI = “ssl://192.168.2.3:8883”;  
const uint8\_t mqttPassword = {‘S’, ‘W’, ‘I’};  
#else  
const char mqttBrokerURI = “tcp://192.168.2.3:1883”;  
#endif  
LE\_ASSERT\_OK(le\_info\_GetImei(DeviceIMEI, NUM\_ARRAY\_MEMBERS(DeviceIMEI)));  
char clientId[32];  
snprintf(clientId, sizeof(clientId), “%s-sub”, DeviceIMEI);  
LE\_ASSERT\_OK(mqtt\_CreateSession(mqttBrokerURI, clientId, &MQTTSession));

```
const uint16_t keepAliveInSeconds = 60;
const bool cleanSession = true;
const char* username = DeviceIMEI;
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);

int rc = mqtt_Connect(MQTTSession);
if (rc != LE_OK)
{
    LE_ERROR("Connection failed! error %d", rc);
}
else
{
    char subscribeTopic[64];
    snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/messages/json", DeviceIMEI);
    LE_FATAL_IF(
        mqtt_Subscribe(MQTTSession, subscribeTopic, MQTT_QOS0_TRANSMIT_ONCE) != LE_OK,
        "failed to subscribe to %s",
        subscribeTopic);
    LE_INFO("Subscribed to topic (%s)", subscribeTopic);

    snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/errors", DeviceIMEI);
    LE_FATAL_IF(
        mqtt_Subscribe(MQTTSession, subscribeTopic, MQTT_QOS0_TRANSMIT_ONCE) != LE_OK,
        "failed to subscribe to %s",
        subscribeTopic);
    LE_INFO("Subscribed to topic (%s)", subscribeTopic);
}

```

}

---

<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:** [April 13, 2021, 11:40am UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/14 "2021-04-13T11:40:11Z")

</div>

seems mqtt\_Connect() is returning the error, probably you need to debug \3rdParty\paho.mqtt.c\src\MQTTClient.c

Good luck!

---

<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:** [April 13, 2021, 12:55pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/15 "2021-04-13T12:55:16Z")

</div>

I don’t know how to modify this file…

---

<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:** [April 13, 2021, 1:16pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/16 "2021-04-13T13:16:54Z")

</div>

You can use gedit or vi tool to add debug message

---

<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:** [April 13, 2021, 1:46pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/17 "2021-04-13T13:46:59Z")

</div>

I have a connection error -6 but don’t understand why

---

<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:** [April 13, 2021, 2:31pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/18 "2021-04-13T14:31:52Z")

</div>

No one can help me? I have to make this example start… 3rd party does not work

---

<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:** [April 13, 2021, 2:40pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/19 "2021-04-13T14:40:51Z")

</div>

@jyijyi I run mqttClient folder in your mangOH green 2-7-19 and it seems to commect:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/2X/4/454f898298d847b5888fbf76d78ecc46eb97b9ec.png)  
What does it mean? Can I use it to send/receive messages?

---

<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:** [April 13, 2021, 3:57pm UTC](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944/20 "2021-04-13T15:57:41Z")

</div>

I never use that application.  
BTW, you have all the source code, you should be able to debug by yourself.

[Next page](https://mangoh.discourse.group/t/mqttclient-folder-with-mosquitto/7944.md?page=2)
