# Create MQTT library

**URL:** <https://forum.xod.io/t/create-mqtt-library/986>\
**Category:** Uncategorized\
**Created:** [July 27, 2018, 10:06am UTC](https://forum.xod.io/t/create-mqtt-library/986 "2018-07-27T10:06:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![moi-yashik2](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/moi-yashik2/32/1764_2.png) [@moi-yashik2](https://forum.xod.io/u/moi-yashik2)\
**Post date:** [July 27, 2018, 10:06am UTC](https://forum.xod.io/t/create-mqtt-library/986/1 "2018-07-27T10:06:01Z")

</div>

Can anyone import the **MQTT** library from Arduino into XOD? This Protocol is now very relevant in the _Internet of things_. This module would be very helpful in implementing the _Internet of things_ with XOD.

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [July 27, 2018, 11:48am UTC](https://forum.xod.io/t/create-mqtt-library/986/2 "2018-07-27T11:48:41Z")

</div>

Yeah, it will be nice. BTW, what library are you talking about? I found several MQTT libs for Arduino.

I wonder, what’s your most popular scenario of using an MQTT broker? I mean, what kind of projects you’re interested in. Is it a sensor network or, maybe, a remote control for some actuator?

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [July 27, 2018, 12:09pm UTC](https://forum.xod.io/t/create-mqtt-library/986/3 "2018-07-27T12:09:51Z")

</div>

I’d also be very interested in an MQTT Lib and willing to help. Although i have literally no experience in C / C++.  
But i guess i could get into it.

If we would collaborate on it - maybe we can get it done.

Best  
Tobias

---

<div class="post-metadata">

**Author:** ![moi-yashik2](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/moi-yashik2/32/1764_2.png) [@moi-yashik2](https://forum.xod.io/u/moi-yashik2)\
**Post date:** [July 27, 2018, 12:51pm UTC](https://forum.xod.io/t/create-mqtt-library/986/4 "2018-07-27T12:51:37Z")

</div>

\<PubSubClient.h \> the library works well on Arduino.  
The MQTT makes it easy to integrate any device into a smart home by type Majordomo or Domoticz (I personally collect leak sensors with sensor modules from esp8266 (but there are no deep sleep libraries and EEPROM in XOD) and server-side value processing (OrangePiPC).

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [August 7, 2018, 7:14pm UTC](https://forum.xod.io/t/create-mqtt-library/986/5 "2018-08-07T19:14:03Z")

</div>

Hi guys,  
How could we proceed with this?

I was recommended paho  
[https://www.eclipse.org/paho/](https://www.eclipse.org/paho/)

Has you heard of it already?  
Best Tobias

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [August 9, 2018, 10:25am UTC](https://forum.xod.io/t/create-mqtt-library/986/6 "2018-08-09T10:25:10Z")

</div>

Hi again,

@nkrkv i am going to plan an IoT system using MQTT. Basically it is going to be a network of several controllers for a heating system which is meant to be controlled remotely via MQTT.

So, can anybody of you can give me a hint on how i can start to implement for example PubSubClient.h into XOD? Is there any kind of documentation i can use to start porting the library?

Best  
Tobias

---

<div class="post-metadata">

**Author:** ![gweimer](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@gweimer](https://forum.xod.io/u/gweimer)\
**Post date:** [August 9, 2018, 2:03pm UTC](https://forum.xod.io/t/create-mqtt-library/986/7 "2018-08-09T14:03:04Z")

</div>

Not a big help, but the 1st step will be to copy the header file into the XOD install (usually `<USER>\AppData\Local\Programs\xod-client-electron\resources\arduino-libraries`). This will need to be repeated each time you re-install or upgrade XOD (there are on-going discussions about how to make this part easier).

The next step is to determine how the calls available in the header file should map to XOD nodes. If there is any one-time initialization that doesn’t need data passed to later calls, you might be able to put it in a separate node; otherwise you may need to put initialization in each node and store the data in State. The library gweimer/ir-remote/ir-read is a fairly trivial example of doing the latter. Since specifying port is tied to the initialization, initialization is only done if the port has not been stored, or it has been changed. Various function calls provided by the IRremote headers are then used to perform the desired action(s).

The built-in nodes for Real Time Clock (search for RTC to find the read/write nodes) provide another example that is probably closer to what you want to do as far as designing nodes for communication, but it uses I2C calls rather than header files.

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [August 9, 2018, 3:16pm UTC](https://forum.xod.io/t/create-mqtt-library/986/8 "2018-08-09T15:16:25Z")

</div>

Hi gweimer,  
this is big help for me.  
Thank you very much as i got a clue on how to start.  
I already was shown the ongoing decision process on github by nkrkv.

Anyways, thanks a lot again. I can dig deeper into this stuff.

Tobias

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [September 11, 2018, 3:33pm UTC](https://forum.xod.io/t/create-mqtt-library/986/9 "2018-09-11T15:33:16Z")

</div>

Hi guys,  
today we could make some progress on this.  
We were able to include PubSubClient.h and make the first part of the basic example of PubSubClient work.

I now can receive a message sent from a basic xod node. Which firstly will only consist of a “not-implemented-in-xod” node.

In there we have global and state information that needs to be implemented in each mqtt node i guess.  
Also, it runs the code for publishing “hello world from xod”.  
A callback is meant to lead to receiving a subscription.  
This was also implemented in the global area.  
But I do not get the subscription message back.

All IN- and OUTPUTS are not used so far.

For communicating i use mosquitto broker and mqtt.fx as GUI.

I attached the .xodball in case anybody is interested and possibly can help.

[mqtt\_xod.xodball](https://canada1.discourse-cdn.com/flex027/uploads/xod/original/1X/defaab35c033e7fcec558b88e532df578e110238.xodball) (4.2 KB)

Best,  
Tobias

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [September 12, 2018, 4:37pm UTC](https://forum.xod.io/t/create-mqtt-library/986/10 "2018-09-12T16:37:38Z")

</div>

I want to make the client connection accessible to other nodes.

At the moment the PubSubClient is called in the global area of the one node i created.

```auto
{{#global}}
EthernetClient ethClient;
PubSubClient mqttclient(ethClient); 
{{/global}}

```

a little bit further down in

`void evaluate(Context ctx)`

the IP addresses of the client and server are set.

I thought of defining a new datatype via output-self for the connection to the mqtt broker called mqtt. This type holds the connection to the mqtt broker. Similar to the inet datatype of w5500.

Is that the right way? Will the connection stay open once set up and be accessible for other nodes via input-mqtt?

I hope you got my aim.

Best Tobias

---

<div class="post-metadata">

**Author:** ![nkrkv](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/nkrkv/32/8_2.png) [@nkrkv](https://forum.xod.io/u/nkrkv)\
**Post date:** [September 13, 2018, 4:37pm UTC](https://forum.xod.io/t/create-mqtt-library/986/11 "2018-09-13T16:37:58Z")

</div>

I think you’re going in the right direction. I mean wrapping the `PubSubClient` into a new custom type. However, I would suggest to define the new custom type as a _pointer_ to `PubSubClient` (in other words: `PubSubClient*`) and keeping the original object in the state of the node defining the type. That way passing the client around you will indeed share the same instance, not its copies.

From what I see, the task you’re trying to accomplish is quite similar to what @brusher_ru describes in the guide on [wrapping Arduino classes](https://xod.io/docs/guide/wrapping-arduino-libraries/).

---

<div class="post-metadata">

**Author:** ![tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@tobias](https://forum.xod.io/u/tobias)\
**Post date:** [September 14, 2018, 7:50am UTC](https://forum.xod.io/t/create-mqtt-library/986/12 "2018-09-14T07:50:12Z")

</div>

Thanks a lot for your answer.  
I followed your release yesterday and am impressed again. I like the efficiency of your work and how you implement users suggestions and improve XOD continuously.

I also saw the guide for wrapping an existing library. I will get into it and keep on working.  
Thank you @brusher_ru for the guide.  
Thank you @nkrkv for the hint with the _pointer_.

---

<div class="post-metadata">

**Author:** ![moi-yashik2](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.xod.io/moi-yashik2/32/1764_2.png) [@moi-yashik2](https://forum.xod.io/u/moi-yashik2)\
**Post date:** [January 2, 2019, 10:13am UTC](https://forum.xod.io/t/create-mqtt-library/986/13 "2019-01-02T10:13:12Z")

</div>

Hello. Tell me, is it possible now to use nodes for MQTT?  
In the library did not find.
