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