Search code examples
azureazure-iot-hubazure-iot-sdk

Azure IoT hub simple publish-subcribe communication


I’m new to Microsoft’s Azure IoT Hub and MQTT/AMQP communication in general and I am trying to establish simple communication between my raspberry pi and a Ubunutu VM running on my computer. I intended on using my pi by as a client to publish messages to a service running on my VM that simply echoes the message contents to std out. After reading all the literature on the Azure website/github page I’m having trouble figuring out the best way to do this. Is it possible to host a service on your local network? Are the iotHub service client samples on the SDK meant to be used as services or as clients intended to communicate with existing services on Azure (SQL databases, webserver, etc)? I was able to send messages to my IoT Hub portal with azure-iot-sdk-c/iothub_client/samples/iothub_client_sample_mqtt.c sample but couldn’t figure out where to include the topic my message was getting published to. I would really appreciate any help/input.

Thanks


Solution

  • Azure IoT Hub is just a message channel, and it does not handle messages.

    For device side (raspberry pi), you use a device SDK to send D2C messages with MQTT, and it works. For service side (Ubuntu VM), you need Event Hub SDK to receive D2C messages sent by raspberry pi (Why Event Hub? https://blogs.msdn.microsoft.com/zhqqitest/2017/03/18/do-not-make-it-a-mess-why-therere-so-many-endpoints-in-iot-hub/).

    Also, IoT Hub supports MQTT on device side ONLY, that is to say, you cannot connect to IoT Hub with MQTT on service side (Ubuntu VM), but you need AMQP.

    You may have another question, then what is the AMQP topic to listen for D2C messages on service side? It's /<compatibleName>/ConsumerGroups/<ConsumerGroups>/Partitions/<PartitionID>.

    • compatibleName: IoT Hub Event Hub-compatible name (what a mess :-S), you can find it from Azure portal (Endpoints – Built-in endpoints – Events)
    • ConsumerGroups: You can find it from Azure portal, $Default by default
    • PartitionID: Partition ID, you can get it when you connect to the Event Hub with AMQP, it's a number, ususally start from 0

    If you don't want to call AMQP directly, you can simply use Event Hub SDK on Ubuntu VM with IoT Hub connection string (NOT Event Hub-compatible endpoint or Event Hub-compatible name, the Event Hub SDK will do that for you).