# How to get data from mqtt

**URL:** <https://community.info-beamer.com/t/how-to-get-data-from-mqtt/1183>\
**Category:** Help\
**Created:** [February 11, 2023, 9:55am UTC](https://community.info-beamer.com/t/how-to-get-data-from-mqtt/1183 "2023-02-11T09:55:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ytvwld](https://community.info-beamer.com/user_avatar/community.info-beamer.com/ytvwld/32/378_2.png) [@ytvwld](https://community.info-beamer.com/u/ytvwld)\
**Post date:** [February 11, 2023, 9:55am UTC](https://community.info-beamer.com/t/how-to-get-data-from-mqtt/1183/1 "2023-02-11T09:55:14Z")

</div>

Hi, this is my first post here, so please correct me, if I’m doing anything wrong.

I’m running a [self-written setup](https://github.com/chaosdorf/freitagsfoo-infobeamer) on an info-beamer hosted pi to display various stuff during short talks in a hackspace. I’m using [the python3 overlay for the service](https://github.com/info-beamer/package-python3).

Now, I’m wondering it if would be possible to access an mqtt server? We do have an instance that gets streamed different data that might be interesting.

My first idea was to make the service connect to the mqtt server, wait for relevant events and then write them to local files which the lua part is listening for. But then I was wondering if the python mqtt library is even available. I extracted the squashfs and didn’t find it. Is there even a mechanism to install python packages?

Thanks. 🙂

---

<div class="post-metadata">

**Author:** ![infobeamer-fw](https://community.info-beamer.com/user_avatar/community.info-beamer.com/infobeamer-fw/32/3_2.png) [@infobeamer-fw](https://community.info-beamer.com/u/infobeamer-fw)\
**Post date:** [February 11, 2023, 11:21am UTC](https://community.info-beamer.com/t/how-to-get-data-from-mqtt/1183/2 "2023-02-11T11:21:36Z")

</div>

> [@ytvwld](#):
>
> My first idea was to make the service connect to the mqtt server, wait for relevant events and then write them to local files which the lua part is listening for.

Sounds correct. An alternative (if relevant events happen frequently) is to send the data directly to the info-beamer process using UDP. See [util.data\_mapper()](https://info-beamer.com/doc/info-beamer#util.data_mapper). You basically send via UDP:

```auto
root/foo:hello

```

and

```lua
util.data_mapper{
    foo = function(value) 
        print(value)
    end
}

```

would print out “hello”. Advantage is that you don’t have to generate local temporary files.

> [@ytvwld](#):
>
> But then I was wondering if the python mqtt library is even available.

It’s not, but I just took a quick look at [paho.mqtt.python’s Python lib](https://github.com/eclipse/paho.mqtt.python/tree/master/src/paho) and it seems to work without complicated installation process or further dependencies: I “brutally” placed the linked `mqtt` folder next to my test python script which looks like this:

```python
import mqtt.client as mqtt

def on_connect(mqttc, obj, flags, rc):
    print(obj, flags, rc)
    
def on_message(mqttc, obj, msg):
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
    
def on_publish(mqttc, obj, mid):
    print("mid: " + str(mid))
    
def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))
    
def on_log(mqttc, obj, level, string):
    print(string) 
    
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_pre_connect = None
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
# mqttc.on_log = on_log
# mqttc.username_pw_set("user", "password")
mqttc.connect("192.168.1.42", 1883, 60)
mqttc.subscribe("#", 0)
mqttc.loop_forever()

```

and when running it, it shows me a stream of all published messages.

---

<div class="post-metadata">

**Author:** ![ytvwld](https://community.info-beamer.com/user_avatar/community.info-beamer.com/ytvwld/32/378_2.png) [@ytvwld](https://community.info-beamer.com/u/ytvwld)\
**Post date:** [February 11, 2023, 1:16pm UTC](https://community.info-beamer.com/t/how-to-get-data-from-mqtt/1183/3 "2023-02-11T13:16:21Z")

</div>

Thank you, it works. 🙂
