How to get data from mqtt

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

I’m running a self-written setup 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.

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

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(). You basically send via UDP:

root/foo:hello

and

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.

It’s not, but I just took a quick look at paho.mqtt.python’s Python lib 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:

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.

1 Like

Thank you, it works. :slight_smile: