Azure IoT Hub, push from device fails every time with connection closed, even using mosquitto_pub

Robert 25 Reputation points
2023-04-15T09:55:49.73+00:00

I have a free Azure Iot Subscription and I created a Hub with few devices. I have a embedded device that connects with MQTT DIRECT to Azure IoT Hub using SAS method.
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#using-the-mqtt-protocol-directly Basic connection is OK (otherwise I get connection refused)
But PUSH always fails and Azure closes connection and I cannot see any messages in Azure Iot Explorer Telemetry. Subscribe is OK also, I can subscribe from embedded and receive messages, including from Explorer when I use C2D Messages. I said to try with another client.
Thanks to this post, I configured mosquitto_pub for test purposes (sending messages)
Connecting Mosquitto to the new Azure MQTT backend

.\mosquitto_pub.exe -h XXXXX.azure-devices.net -u "XXXXX.azure-devices.net/mydevice6" -P "SharedAccessSignature sr=XXXXX.azure-devices.net%2Fdevices%2Fmydevice6&sig=WA9qbn7Xa%2FQ%2F90CL7PgnNoD8AllWW757QvGJ3s2Ik2E%3D&se=1681536409" -t "devices/mydevice6/messsages/events/" -p 8883 -d -i mydevice6 -d -V mqttv311 -q 1 -m "{""key"": ""value""}" --cafile "DigiCertGlobalRootG2.crt.pem"
Client mydevice6 sending CONNECT
Client mydevice6 received CONNACK (0)
Client mydevice6 sending PUBLISH (d0, q1, r0, m1, 'devices/mydevice6/messsages/events/', ... (12 bytes))
Error: The connection was lost.

Again, appears that mosquitto_pub works just fine, if I change device ID, signature, or others stuff, I immediately receive "Connection refused" The behavior is exactly what I see on my embedded device, I receive ACK from server, appears that I am able to PUSH, but push where??? The connection shall remain online, instead, Azure it closes immediately. I think this is some kind of stupid mistake, or some bad Azure configuration. I did another test, this time, successfully, from C# (using SDL examples)
I created a small console app, with these parameters

static DeviceClient deviceClient;
static string deviceConnectionString = "HostName=XXXXXX.azure-devices.net;DeviceId=mydevice2;SharedAccessKey=xP+WyHL1quc4ax9pQ7Fc8oKuhs/0iC5dSKH2yUqIjzM=";

            MyData data = new MyData { Id = "Param", Name = "Value" };
            var serializeData = JsonConvert.SerializeObject(data);
            var commandMessage = new Message(Encoding.ASCII.GetBytes(serializeData));

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("Send Message: {0}", serializeData);
            Console.ResetColor();
            Console.WriteLine("");

            await deviceClient.SendEventAsync(commandMessage);

This time message can be seen in Azure IoT Explorer. Now why my embedded device and mosquitto_pub appears that PUSH messages, but

  • I cannot see them anywhere
  • Azure immediately disconnects

I am stuck from few days by now, everything I do, I can connect, Azure sends ACK, but PUSH messages are simply replied with "connection lost". Any help greatly appreciated!!!

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

Accepted answer
  1. Boris Von Dahle 3,221 Reputation points
    2023-04-15T10:17:20.5633333+00:00

    Hello,

    It looks like there might be a typo in the topic name you're using in the mosquitto_pub command.

    The correct topic for sending device-to-cloud messages should be devices/{device_id}/messages/events/.

    In your case, it appears to be misspelled as devices/mydevice6/messsages/events/. The extra 's' in "messsages" is causing the issue.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.