xHasKx / luamqtt

luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client
https://xhaskx.github.io/luamqtt/
MIT License
160 stars 42 forks source link

Getting the error Connection Refused, not authorized again and again #45

Closed zt190 closed 1 year ago

zt190 commented 1 year ago

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
xHasKx commented 1 year ago

Hi @zt190 , please note that the error message Connection Refused, not authorized is from the CONNACK packet handler with rc=5, which means your issue is not about the MQTT library or MQTT protocol, but about authorization on the connected broker. So please check your username, password, IP whitelisting on the broker, connection and authorization policies, and whatever is related.

So this is not a luamqtt issue.