Hi, I am trying to implement your piece of code in my Sierrawireless RV50X. I do not understand how to use the "run_ioloop" fonction in order to publish message every minute.
can you help please ? thanks in advance
luamqtt - MQTT client written in pure lua
aquaboxsalus, it depends on the Lua runtime available on your Sierrawireless RV50X.
If it has some usual Lua interpreter - you may check out this test as an example: https://github.com/xHasKx/luamqtt/blob/master/tests/spec/ioloop.lua#L43
Here you have a function loop_func() which will be called again and again by the ioloop.
So you can insert a code into it to calculate how much time has passed since the last sending (use os.time for example).
When you calculated a 1-minute diff - send an MQTT message or whatever you need.
thanks. in the documentation i see that we can create an ioloop specifiying sleep argument ( sleep interval after each iteration (default 0))
correct ? is it not better ? how to use that ? thanks
- Edited
aquaboxsalus
Yes, the documentation is correct. This parameter should be adjusted by you on the device. The lesser value leads to your handler being called more often, but check the CPU load and power consumption.
Hello,
I am trying to implement mqtt support into a mudclient (Mudlet) that has lua as its backend script language. And i have almost gotten it to work the way i want it to using luamqtt. My challenge seem to be with the ioloop. If i run the ioloop the way the examples describe, I will basically crash the mudclient as the ioloop is blocking the "main thread" from executing.
I managed to get past this issue, by not running ioloop, but rather setting up a timer that run the client:_ioloop_iteration() at a fixed interval. This works ok'ish when i run it at 1 second period. Now i can publish, and subscribe to messages in Mudlet.
I would have preferred to run it faster though, as the client is "sluggish" when receiving external messages (understandably) with a 1 second period.
I did try to run the client:_ioloop_iteration() at 100ms instead, but then i had masive crashes again...
I guess my question is: Am i doing it wrong? Can i run the ioloop wihtout it crashing the main thread? Or if i need to run the ioloop manually, is this the correct way to do it, by manually calling _ioloop_iteration() at a fixed interval? Or is there a better way, calling some method that will check the inbound queue, fire the callbacks then exit without looping?
Thanks and regards!
- Edited
Hi EspenSolbu ,
To make the MQTT connection work, something in your code should periodically pull packets from the server and monitor a connection state.
The ioloop and its ":run_until_clients()" method (or "mqtt.run_ioloop(client)" wrapper) is the right thing to do that.
I'm not familiar with the Mudlet Lua environment, but if you can run a separate OS thread with the "mqtt.run_ioloop(client)" call in this - this can be the best option. So the entire CPU time of the separate thread will be used for network communication.
The other way is to use the networking capabilities of your Mudlet Lua environment. It's possible that it has its own sockets implementation. In this case, the best option will be to implement a "connector" based on that sockets: https://github.com/xHasKx/luamqtt#connectors
Finally, if you have only timers - your way is also can work. Here is a better code example:
local loop = require("mqtt.ioloop").get(true, {timeout=0.005}) -- NOTE: you can try to adjust this timeout value
function on_timer()
loop:iteration() -- NOTE: instead of your "client:_ioloop_iteration()" call
end
I'm not sure what you mean by "masive crashes", so you may try to adjust the timeout=X value (up or down) in the code above to try to get rid of those crashes.
Thank you for your work and this nice module. I am planning to write a simple Lua client which should get MQTT traffic and send over to our Kronometrix Analytics backend for analysis and visualization. The client will be using Lua 5.4
hello, salam, is it possible to make a mqtt connection between browser and lua over websocket
i have a react app on browser which only can connect to mqtt on websocket, then on openwrt for server side i only can run lua and i want to connect it to mqtt with websocket protocol.
so what is the best answer and practice
- Edited
kafurakafiri answered in the issue https://github.com/xHasKx/luamqtt/issues/33#issuecomment-955537654
- Edited
Hi folk, I'm trying to setup 2 clients running in parallel on the same device (they shall connect to different brokers. 1st to a local broker on the device, 2nd one to an off-board broker, e.g. flespi). I can add the 1st client to the loop. How to add the second client in a proper way? Shall I utilize e.g. the connack callback function? So if the 1st client is successfully connected, the second one shall start connecting. So something like this?
client1 = mqtt.client(client1_config)
mqtt.run_ioloop(client1)
on positive connack callback -->
client2 = mqtt.client(client2_config)
mqtt.run_ioloop(client2)
Dear Kial, is it possible to input PROXY settings into the connection?
I'm behind a corporate Firewall and with paho-mqtt (Python) I hat to adjust proxy settings for the connection to make mqtt running.
My working python solution is the following:
myClient = mqtt.Client(client_id="myID")
myClient.username_pw_set(username = myUsrName,password = myPasswd)
myClient.proxy_set(proxy_type = socks.HTTP, proxy_addr="127.0.0.1", proxy_port = 3128)
connectionStatus = myClient.connect(host=myIP,port=myPORT, keepalive = 5)
Can you help me out doint this with luaMQTT?
HerrmannPaul, the luamqtt lib does not support socks proxy, and brief googling does not help me to find a lua library that can do it transparently for the luasocket (TCP connections layer).
But anyway you can create a custom connector that will pass the TCP connection through your proxy.
And the alternative way for you is to install or create software that will listen for a plain TCP socket on the localhost and pass it through your HTTP proxy to the MQTT broker.
- Edited
Hello,
I am currently working on a project involving Teltonika devices, specifically with smart meters. The objective of this project is to retrieve meter readings and transmit the data to the Azure portal. When I am trying to publish a message to Azure portal with the help of mqtt in Lua language but I am getting the "Connection Refused, not authorized" . I am referring this document https://github.com/xHasKx/luamqtt. I have used the same credentials for sending the data to Azure in python language and the python script is working. I am using the Lua 5.4 version and below is my lua script
local mqtt = require("mqtt")
local json = require("lunajson")
local luasec = require("ssl")
print("using luasec runtime version: ", luasec._VERSION)
local path_to_root_cert = "<root directory>/src/cert/digicert.crt"
local device_id = "Teltonika02";
local sas_token = "SharedAccessSignature sr=<hostname>.azure-devices.net&sig=xeJT3F1XsYB5uIcbLjW3L9do4NXlUIOC1kBfNZ5zHuQ%3D&skn=iothubowner&se=2045963831";
local iot_hub_name = "<hostname>.azure-devices.net/<device id>/?api-version=2021-04-12";
local client = mqtt.client({
uri = "<hostname>.azure-devices.net",
username = iot_hub_name,
secure = {
mode = "client",
protocol = "tlsv1_2",
cafile = path_to_root_cert,
certificate = nil,
key = nil
},
password = sas_token,
version = mqtt.v50,
clean = true
});
print(client, ": client")
local topic = "devices/" .. device_id .. "/messages/events/$.ct=application%2Fjson%3Bcharset%3Dutf-8"
local payload = [[{'id':123}]]
client:on{
connect = function(connack)
print("connack....", connack)
if connack.rc ~= 0 then
print("connection to broker failed:", connack:reason_string(), connack)
return
end
print("Now go for subscribe")
-- connection established, now subscribe to test topic and publish a message after
assert(client:subscribe{ topic=topic, qos=1, callback=function()
assert(client:publish{ topic = topic, payload = payload })
end})
end,
message = function(msg)
print(msg, ": messgae")
assert(client:acknowledge(msg))
-- receive one message and disconnect
print("received message", msg)
client:disconnect()
end,
}
-- run ioloop for client
mqtt.run_ioloop(client)
-- Check if the MQTT client is connected
if client.connected then
print("Connection is established")
else
print("Connection is not established : ", client.connected)
end
zt190 take a look at device policy. It should allow your client to connect to the hub with given client-id, subscribe and publish to specific topics. Enable all options if unsure.
Here is the article we touch Azure device policy a bit: https://flespi.com/kb/flespi-to-azure
Hi guys, many thanks for the great module! I'm trying to utilize SSL/TLS in order to securely communicate with flespi broker. Is my understanding correct, that luamqtt is not supporting certificate based authentication?
So if token is regarded as not secure enough and luamqtt is used as MQTT client, the only way would to go would be via port 8883 or 443?
jp_one I'm trying to utilize SSL/TLS in order to securely communicate with flespi broker. Is my understanding correct, that luamqtt is not supporting certificate based authentication?
If you mean SSL/TLS secure transport for MQTT protocol - it's fully supported if your Lua runtime has a luasec (or compatible) module. And you can specify any custom SSL parameters like certificate file in the secure={...}
table parameter in mqtt client instance constructor (see secure
parameter description there).
If you are talking about new MQTT 5.0 packet type AUTH - it's also supported in the luamqtt with client:auth() method. But this is a broker-specific authentication method, thus you cannot use it with flespi broker.
jp_one So if token is regarded as not secure enough and luamqtt is used as MQTT client
Sorry, I can't understand this part. Can you please rephrase it?
jp_one the only way would to go would be via port 8883 or 443?
I would say that you always should use port 8883 and SSL/TLS secure connection.
But port 443 is for websocket transport, which is not supported by luamqtt.