Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Selene/Selenites/MQTT.sel
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
66 lines (52 sloc)
1.31 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!./Selene | |
| -- Simple MQTT receiving test | |
| unpack = unpack or table.unpack | |
| if not table.pack then | |
| function table.pack (...) | |
| return {n=select('#',...); ...} | |
| end | |
| end | |
| --[[ | |
| -- Errors code test | |
| print 'Error code handling test' | |
| print '------------------------' | |
| code = SelMQTT.ErrConst("MQTTCLIENT_DISCONNECTED") | |
| print(code, SelMQTT.StrError(code)) | |
| --]] | |
| print '\nTesting broker connection' | |
| print '--------------------------' | |
| -- Callbacks | |
| function handle_tata( topic, msg ) | |
| print("Lua received t:'" .. topic .."' m:'".. msg .."'") | |
| SelShared.set('tata', msg) | |
| return true | |
| end | |
| -- Update functions | |
| function update_tata() | |
| print("Trigger for Tata :", SelShared.get('tata')) | |
| end | |
| function update_toto() | |
| print("Trigger for toto :", SelShared.get('toto')) | |
| end | |
| -- Connection, subscription and finally waiting for messages | |
| Brk, err = SelMQTT.connect( "tcp://localhost:1883", { reliable=false } ) | |
| if not Brk then | |
| print( err ) | |
| return | |
| end | |
| _, 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 ) | |
| return | |
| end | |
| while true do | |
| local rt = table.pack( Selene.WaitFor() ) | |
| for _,ret in ipairs(rt) do | |
| if type(ret) == 'function' then | |
| ret() | |
| end | |
| end | |
| end | |