Can't connect through MQTT to IoT Hub

David 0 Reputation points
2024-11-15T11:37:27.34+00:00

Hello everyone. I want to ask a question regarding an issue that I've been dealing with. I am trying to connect a Schneider SpaceLYnk device to my MS Azure IoT hub through a LUA script using mosquitto. However it does not seem to connect. Does anyone know if the script is correct or if I am entering the right details? Thank you

Here's a screenshot of my script and some info about the IoT hub I am trying to connect to. It says some messages are coming in but event monitor isn't showing any. And my device isn't logging "con" on connect as it should.

mqtt = require("mosquitto")
client = mqtt.new()
client.ON_CONNECT = function()
  	log("con")
  	client:publish("world", "hello") 
  	local qos = 1 
  	local retain = true 
  local mid = client:publish("/SpaceLynk", "my payload", qos, retain)
end

  client.ON_PUBLISH = function()
  client:disconnect()
  end

  broker = 'MyIoTDS.azure-devices.net'

  client:login_set("SpaceLynk","SECRET KEY")  
client:connect(broker)  
client:loop_forever()


Event monitor

My IoT Hub

My IoT Hub - devices

My IoT Hub - device settings

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,213 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 33,396 Reputation points MVP
    2024-11-15T12:08:35.3633333+00:00

    Hello David,

    welcome to this moderated Azure community forum.

    You try to send a message to the Azure IoT Hub over MQTT and you use topic '/SpaceLynk'.

    Although the Azure IoT Hub offers MQTT support for receiving messages and sending commands, it does not offer full MQTT support.

    It is not a plain MQTT broker. The IoT Hub only supports a limited amount of specific topic, honnoring the interface of the device twin (eg. receiving messages, desired/reported properties, cloud messages, direct method).

    So, the topic '/SpaceLynk' will nog be accepted.

    Check out this blog post regarding the specific device twin related topics.

    Although I have no experience with LUA, it should be possible to connect the IoT Hub if the right guidelines are followed.

    If you want to send messages using the full MQTT protocol, check out the Event Grid namespace MQTT support.

    User's image

    Security is enforced by certificates and you can event prevent spoofing using the various wildcards.

    If you want to test it, read this blog post about this MQTT broker with several examples.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.