# Publishing data to MQTT broker

**URL:** <https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854>\
**Category:** mangOH Red\
**Created:** [October 30, 2017, 1:48pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854 "2017-10-30T13:48:58Z")\
**Posts on this page:** 18\
**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:** [October 30, 2017, 1:49pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/1 "2017-10-30T13:49:00Z")

</div>

Hi,

I am pretty new to working with MQTT and just started working with the mangOH red board. I was able to install the MQTT client from mangOH git (paho eclipse branch) and I am wondering how does one go about to publish data to a broker?

For example, if we have a broker with ip: [testing.mangoh.com:1338](http://testing.mangoh.com:1338) and password testing. Is there a command you give on the command line, or do you hardcode the IP in the application? Could not really find a guide on a more “step by step” and I am trying to figure this out. I also have access to a mangOH green board, if that is any different.

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:** [October 30, 2017, 3:08pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/2 "2017-10-30T15:08:13Z")

</div>

If you look at the `.api` file for the MQTT client, you can get an idea of what you have to do. Basically it’s this:

1. `CreateSession()`
2. `SetConnectionOptions()`
3. `AddConnectionLostHandler()`
4. `Connect()`
5. `Publish()`

---

<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:** [October 31, 2017, 6:40am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/3 "2017-10-31T06:40:31Z")

</div>

Hey,

Thanks for the answer! If I understand it correctly, you write a new application using the API file and then install it on the module?

Does the mqttClient rely on any other application to work? I mean I would like to stop as many applications as possible and still be able to connect to a broker. If I checked correctly, the mqttClient does not rely on other applications installed.

---

<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:** [October 31, 2017, 4:10pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/4 "2017-10-31T16:10:48Z")

</div>

You can see by looking at the bindings section here:

> <https://github.com/mangOH/MqttClient/blob/use_paho_linux_version/mqttClient.adef#L62>

that MqttClient doesn’t depend on any other applications. Note that MqttClient does not attempt to create a data connection, so you need to deal with that yourself. Take a look at DataConnectionService in Legato if you’re interested in that.

---

<div class="post-metadata">

**Author:** ![lyao](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lyao](https://mangoh.discourse.group/u/lyao)\
**Post date:** [November 22, 2017, 11:12am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/5 "2017-11-22T11:12:56Z")

</div>

I don’t understand what is the relation between the mqttClient that you mentioned and the mqttClient app [here](https://github.com/mangOH/MqttClient/tree/6b24c67721832748c25d9a62751690afe52ed6fc). The api files are completely different. Which one should I use and how?

---

<div class="post-metadata">

**Author:** ![lyao](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lyao](https://mangoh.discourse.group/u/lyao)\
**Post date:** [November 22, 2017, 11:43am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/6 "2017-11-22T11:43:01Z")

</div>

Hi @dfrey, now I understand that the one you mentioned is another solution for MQTT which is based on PAHO. But can I put this app in a standalone folder outside the more AirVantage based MQTTClient? Because when I make it in a standalone folder it says:

> /home/lichen/mangOH/apps/MqttClient-use\_paho\_linux\_version/mqttClientService/mqttClientService.c:4:24: fatal error: MQTTClient.h: No such file or directory  
> #include “MQTTClient.h”  
> ^  
> compilation terminated.  
> ninja: build stopped: subcommand failed.  
> Makefile:9: recipe for target ‘wp85’ failed  
> make: \*\*\* [wp85] Error 1

---

<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 22, 2017, 3:47pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/7 "2017-11-22T15:47:43Z")

</div>

I think that the `use_paho_linux_version` branch of MqttClient is going to be nicer to use than the master branch. It’s a little bit tricky to build though. Go to the MqttClient folder and do `git checkout use_paho_linux_version` and then `git submodule init && git submodule update`. You should now have the correct version of the paho mqtt source code checked out as a submodule. Now do `make paho` in the MqttClient folder. This will build the paho shared library. Now you should be able to build your system.

---

<div class="post-metadata">

**Author:** ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)\
**Post date:** [November 26, 2017, 12:52pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/8 "2017-11-26T12:52:23Z")

</div>

Hello, did you get it to work?

---

<div class="post-metadata">

**Author:** ![lyao](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lyao](https://mangoh.discourse.group/u/lyao)\
**Post date:** [November 26, 2017, 2:32pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/9 "2017-11-26T14:32:15Z")

</div>

Hi @dfrey, is it still true that the paho version mqttClient cannot work together with dataRouter which relies on the master branch of mqttClient? Because the latest post about this issue was from a year ago. If so, can I build paho version and master version separately and install them to get dataRouter working?

---

<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 26, 2017, 3:48pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/10 "2017-11-26T15:48:28Z")

</div>

Hi,

Yes I got it working. It is succesfully publishing data to my own MQTT broker. Are you stuck on anything?

---

<div class="post-metadata">

**Author:** ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)\
**Post date:** [November 26, 2017, 6:30pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/11 "2017-11-26T18:30:50Z")

</div>

Thanks for the replay,  
I am having a hard time with that and other examples as well.  
I need a simple app example that can publish data to MQTT broker.  
I have MangOH red with WP7502 modem and right now I have Legato AF version 17.10.0 (latest) on it. The other problem is that I did not manage to build any 16.X.X version for WP7502 and most of the examples does not work with 17.X.X or maybe I am doing something wrong.

My MQTT broker needs  
username  
password

Can you tell me what steps did you do?

---

<div class="post-metadata">

**Author:** ![lyao](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lyao](https://mangoh.discourse.group/u/lyao)\
**Post date:** [November 26, 2017, 7:20pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/12 "2017-11-26T19:20:12Z")

</div>

Hi @dfrey and @basti, can you please tell me what `clientId` as a input parameter of `mqtt_CreateSession` is?

---

<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 27, 2017, 8:02am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/13 "2017-11-27T08:02:41Z")

</div>

Hi,

@raxsix you might want to have a look at this thread: [MQTT Client Support](http://mangoh.discourse.group/t/mqtt-client-support/160)  
There are other people having the same issues on there. I am using Legato 17.09 and it is working fine. Are you using Developer studio?

@lyao If I understand it correctly, `clientID` takes a string of your choosing as input. In the example code is is using the device’s IMEI number plus “-pub” at the end. So for example `123451612-pub`

---

<div class="post-metadata">

**Author:** ![lyao](https://avatars.discourse-cdn.com/v4/letter/l/e8c25b/32.png) [@lyao](https://mangoh.discourse.group/u/lyao)\
**Post date:** [November 27, 2017, 9:57am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/14 "2017-11-27T09:57:27Z")

</div>

Thanks @basti. I read the example code but I just do not understand where this clientID comes from.

---

<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 28, 2017, 5:15pm UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/15 "2017-11-28T17:15:32Z")

</div>

I was away for a few days, so I’m just catching up now.

ArduinoBridge depends on DataRouter and DataRouter depends on the master branch of MqttClient. If I had the time, I would like to get TLS working in the `use_paho_linux_version` branch of MqttClient and then rewrite the DataRouter to use the `use_paho_linux_version` of MqttClient. I don’t know if/when that will happen though.

The clientID is typically the IMEI of the WP module when connecting to AirVantage. You can see how the `master` branch of the MqttClient gets the client id here: [https://github.com/mangOH/MqttClient/blob/master/mqttClientComp/src/mqttClient.c#L1716](https://github.com/mangOH/MqttClient/blob/master/mqttClientComp/src/mqttClient.c#L1716)

The `use_paho_linux_version` branch of MqttClient doesn’t have any built-in code for getting setting the client id based on the IMEI because that is an AirVantageism and the `use_paho_linux_version` branch is intended to be broker agnostic.

---

<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 29, 2017, 10:49am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/16 "2017-11-29T10:49:20Z")

</div>

Hi @dfrey,

I am not using any AirVantage APIs, and `use_paho_linux_version` branch of the MqttClient, but I am getting the IMEI. Shouldn’t it work the same way as in your linked code, since in `COMPONENT_INIT` you find:

> LE\_ASSERT\_OK(le\_info\_GetImei(deviceIMEI, NUM\_ARRAY\_MEMBERS(deviceIMEI)));

---

<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 29, 2017, 11:58am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/17 "2017-11-29T11:58:04Z")

</div>

The code I linked to was intended to be an example of how to get the IMEI so that you can fill in the client id parameter when using the `use_paho_linux_version` branch of the app. Can you try to rephrase what is currently blocking you?

---

<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 30, 2017, 6:50am UTC](https://mangoh.discourse.group/t/publishing-data-to-mqtt-broker/854/18 "2017-11-30T06:50:43Z")

</div>

Yes sorry, of course. I meant that even if I do not use any applications using AirVantage, I am still able to get the IMEI.  
I understood from your previous answer that you can only retrieve it if connecting to AV.
