# Sending file over mqtt using mqttClient-for-Legato

**URL:** <https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773>\
**Category:** Uncategorized\
**Created:** [December 3, 2020, 6:16pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773 "2020-12-03T18:16:21Z")\
**Posts on this page:** 17\
**Page:** 3

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 17, 2023, 12:59pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/41 "2023-10-17T12:59:35Z")

</div>

sir the procedure is i have to git clone this to myworkspace and i will enable the leaf shell and run the mktool command wrigtht

---

<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:** [October 17, 2023, 1:01pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/42 "2023-10-17T13:01:54Z")

</div>

Then why does it still ask for datahub?

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 17, 2023, 1:05pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/43 "2023-10-17T13:05:15Z")

</div>

i changed only publish and subscribe.c files but don,t know why it is happening like this

---

<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:** [October 17, 2023, 1:23pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/44 "2023-10-17T13:23:41Z")

</div>

You can see there is no datahub in the adef file

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 17, 2023, 1:25pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/45 "2023-10-17T13:25:24Z")

</div>

yes it is not there,i have checked it

---

<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:** [October 17, 2023, 1:43pm UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/46 "2023-10-17T13:43:36Z")

</div>

Clear the project or create a new one and retry

BTW, you should just compile the adef instead of sdef, the sdef is pointing to other location:

> <https://github.com/legatoproject/legato-af/blob/19.02-release/apps/sample/mqtt/mqttSample.sdef>

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 7:33am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/47 "2023-10-18T07:33:06Z")

</div>

sir i have copiled .adef and successfull .update file generated and application is running successfully in board also, but it is not connecting to mqtt , it is not showing error also , what may be the reason could u help !  
this is my mqttPublish .c fle  
/\*\*

- @file testPublisher.c
- 
- MQTT Publisher sample application
- 
- 
* * *
- 
- Copyright (C) Sierra Wireless Inc.
- 

\*/

#include “legato.h”  
#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  
_/  
//--------------------------------------------------------------------------------------------------  
static void OnConnectionLost  
(  
void_ context  
)  
{  
LE\_ERROR(“Connection lost!”);  
}

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

- Call-back function called on arrived message  
_/  
//--------------------------------------------------------------------------------------------------  
static void OnMessageArrived  
(  
const char_ topic,  
const uint8\_t\* payload,  
size\_t payloadLen,  
void\* context  
)  
{  
LE\_ERROR(“The publisher received a message!”);  
}  
//--------------------------------------------------------------------------------------------------  
/\*\*
- Timer handler for periodically publishing data  
_/  
//--------------------------------------------------------------------------------------------------  
static void PublishTimerHandler  
(  
le\_timer\_Ref\_t timer  
)  
{  
static int messageData = 0;  
uint8\_t payload[64];  
snprintf((char_)payload, sizeof(payload), “{"value":%d}”, messageData);  
size\_t payloadLen = strlen((char\*)payload);  
const bool retain = false;  
char publishTopic = “v1/devices/me/telemetry”;  
snprintf(publishTopic, sizeof(publishTopic), “%s/messages/json”, DeviceIMEI);  
const le\_result\_t publishResult = mqtt\_Publish(  
MQTTSession,  
publishTopic,  
payload,  
payloadLen,  
MQTT\_QOS0\_TRANSMIT\_ONCE,  
retain);  
LE\_INFO(“Published Topic %s data %s result %s”, publishTopic, payload,  
LE\_RESULT\_TXT(publishResult));  
messageData++;  
}

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 = “[phycluds.com:1884](http://phycluds.com:1884)”;  
#endif

```
LE_ASSERT_OK(le_info_GetImei(DeviceIMEI, NUM_ARRAY_MEMBERS(DeviceIMEI)));
char clientId[32];
snprintf(clientId, sizeof(clientId), "%s-pub", DeviceIMEI);
LE_ASSERT_OK(mqtt_CreateSession(mqttBrokerURI, clientId, &MQTTSession));

const uint16_t keepAliveInSeconds = 60;
const bool cleanSession = true;
const char* username = "GCbrAhbMVyCR2DFwWht0";
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
{
    LE_INFO("Connected to server '%s'", mqttBrokerURI);
    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, 10000));
    LE_ASSERT_OK(le_timer_SetRepeat(timer, 0));
    LE_ASSERT_OK(le_timer_Start(timer));
    LE_INFO("Publish timer started");
}

```

}

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 7:37am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/48 "2023-10-18T07:37:31Z")

</div>

[mango-log](https://mangoh.discourse.group/uploads/short-url/dvMZGuSOynIM5xEODiM640DCc23) (6.6 KB)

sir this is the log file uploaded on that publisher.adef

---

<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:** [October 18, 2023, 7:44am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/49 "2023-10-18T07:44:39Z")

</div>

can you print a helloworld out first?  
You need to debug by yourself!

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 7:50am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/50 "2023-10-18T07:50:09Z")

</div>

yeah i have writen hellowold application and it is running successfully

---

<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:** [October 18, 2023, 7:56am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/51 "2023-10-18T07:56:01Z")

</div>

the put the code in your application’s first function

Other user can make it work:

> [@mqttClient Service Connect error?](https://mangoh.discourse.group/t/mqttclient-service-connect-error/9093):
>
> Hi! I’m developing a SW to connect with mosquitto broker. I started from the source code in apps/platformservices of Legato environment. I can right build the project, create the session, but I have problem when I have to connect to the broker: [image] I’m trying to connect without user and password (I’m beginning!) and this is the result: Obviously I connected my mangOH green to internet thought both WiFi and Sim. Which could be the problem?

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 8:36am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/52 "2023-10-18T08:36:53Z")

</div>

sir , i did’t get what you are saying

---

<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:** [October 18, 2023, 8:38am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/53 "2023-10-18T08:38:16Z")

</div>

did your program start the component\_init()?

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 8:43am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/54 "2023-10-18T08:43:00Z")

</div>

in log it is not showing anything related to component initialization, above i have attached the log

---

<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:** [October 18, 2023, 8:44am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/55 "2023-10-18T08:44:04Z")

</div>

that is why i ask you to print a helloworld message in component\_init()

This is some basic debug skill …

---

<div class="post-metadata">

**Author:** ![nayanhn](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nayanhn](https://mangoh.discourse.group/u/nayanhn)\
**Post date:** [October 18, 2023, 8:59am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/56 "2023-10-18T08:59:47Z")

</div>

i added this line in component init()  
LE\_INFO(“Hello, World!”);

but in log it is not showing anything

---

<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:** [October 18, 2023, 9:43am UTC](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773/57 "2023-10-18T09:43:14Z")

</div>

your hello world app is working fine, right?  
Then you can port the mqtt app to your helloworld app line by line

[Previous page](https://mangoh.discourse.group/t/sending-file-over-mqtt-using-mqttclient-for-legato/5773.md?page=2)
