Publishing data to MQTT broker



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: 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.



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()



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.


You can see by looking at the bindings section here:

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.


I don’t understand what is the relation between the mqttClient that you mentioned and the mqttClient app here. The api files are completely different. Which one should I use and how?


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


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.


Hello, did you get it to work?


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?



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


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

Can you tell me what steps did you do?


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



@raxsix you might want to have a look at this thread: MQTT Client Support
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


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


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:

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.


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)));


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?


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.