Où sommes nous ?


MQTT messaging

MQTT handling was the primary goal when Séléné was created : it has a quit complete coverage using SelMQTT object as described bellow.

The full source code of this example is here.

Connecting to the broker

Brk, err = SelMQTT.connect( "tcp://localhost:1883", { reliable=false  } )
if not Brk then
        print( err )

SelMQTT.connect() will connect to the broker for which the URL provided as first argument. The second argument is a table of options


_, err = Brk:subscribe( {
{ topic = "tata/+/truc", func=handle_tata, trigger=update_tata, trigger_once=false, qos=SelMQTT.QoSConst("QoS0") },
{ topic = "toto", trigger=update_toto, trigger_once=true }
} )
if err then
print( err )

Broker's subscribe takes a table as argument, a row by topic to listen to, and with following content


The topic to be listen : this argument is obviously mandatory and may contain MQTT's wildcards.

It's possible to specify the Quality Of Service to apply : QoS0 by default.


Specify the function to be launched immediately, in a separate thread, when a message arrives.

function handle_tata( topic, msg )
        print("Lua received t:'" .. topic .."' m:'".. msg .."'")
        SelShared.set('tata', msg)
        return true

As explained in multi-threading discuss, such function has no access to global variables.

If not specified, a default function is called which only create a SelShared with topic as name and containing the message as value. Notez-bien : if you specify yourself a function, this SelShared is not created but if you do it in your function.

If the function return true, trigger function (if specified) is submited to the waiting tasks list. Otherwise, it isn't.


Unlike functions, triggers are not launched immediately at message arrival but are pushed in tasks waiting list and doesn't receive any argument : typically, it retrieves the value to handle from a SelShared.

function update_toto()
        print("Trigger for toto :", SelShared.get('toto'))

To avoid duplication in tasks waiting list, use trigger_once argument.

A rule of thumb, functions are doing data validation and set SelShared corresponding object while triggers are in charge of heavy tasks like updating a GUI or stuffs needing concurrent access arbitration.


It's possible to associate a watchdog to a topic.

The first step is to create a timer

function exhausted()
        print("No message received after 3 seconds")

-- Create the watchdog's timer
wdtimer, err = SelTimer.create { when=3, ifunc=exhausted, clockid=SelTimer.ClockModeConst("CLOCK_MONOTONIC") }

Exhausted() is executed if no data is received within 3 seconds.

Then it is associated to the topic :

_, err = Brk:subscribe( {
{ topic = "toto", trigger=msgreceived, watchdog=wdtimer }
} )

The waiting loop is modified to take in account this timer.

while true do
local rt = table.pack( Selene.WaitFor(wdtimer) )
for _,ret in ipairs(rt) do
if type(ret) == 'function' then

Visitez :
La liste de nos voyages
Nos sorties Ski et rando
Copyright Laurent Faillie 2001-2018
N'oubliez pas d'entrer le mot de passe pour voir aussi les photos perso.
Contactez moi si vous souhaitez réutiliser ces photos et pour les obtenir avec une plus grande résolution.
Visites durant les 7 derniers jours Nombre de visites au total.

Vous pouvez laissez un commentaire sur cette page.