Skip to content

Commit bc6c268

Browse files
committed
MQTT tutorial
1 parent 8c8fcbf commit bc6c268

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

articles/iot/iot-mqtt-connect-to-iot-hub.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ The following list summarizes IoT Hub MQTT implementation-specific behaviors:
295295

296296
For more information, see [Send device-to-cloud and cloud-to-device messages with IoT Hub](../iot-hub/iot-hub-devguide-messaging.md).
297297

298-
## Receiving cloud-to-device messages
298+
## Receive cloud-to-device messages
299299

300300
To receive messages from IoT Hub, a device should subscribe using `devices/{device-id}/messages/devicebound/#` as a **Topic Filter**. The multi-level wildcard `#` in the topic filter is used to enable the device to receive more properties in the topic name. IoT Hub doesn't allow the usage of the `#` or `?` wildcards to filter subtopics. IoT Hub isn't a general-purpose publish-subscribe messaging broker, it only supports the documented topic names and topic filters. A device can only subscribe to five topics at a time.
301301

articles/iot/tutorial-use-mqtt.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
title: "Tutorial: Use MQTT to create an IoT device client"
3-
description: Tutorial - Use the MQTT protocol directly to create an IoT device client without using the Azure IoT Device SDKs
4-
titleSuffix: Azure IoT
3+
description: Tutorial - Use the MQTT protocol directly to create an IoT device client without using the Azure IoT device SDKs
54
author: ryanwinter
65
ms.author: rywinter
7-
ms.date: 04/04/2024
6+
ms.date: 03/11/2025
87
ms.topic: tutorial
98
ms.service: azure-iot
109
ms.custom: devx-track-azurecli
@@ -13,37 +12,34 @@ ms.custom: devx-track-azurecli
1312

1413
# Tutorial - Use MQTT to develop an IoT device client without using a device SDK
1514

16-
You should use one of the Azure IoT Device SDKs to build your IoT device clients if at all possible. However, in scenarios such as using a memory constrained device, you may need to use an MQTT library to communicate with your IoT hub.
15+
You should use one of the Azure IoT Device SDKs to build your IoT device clients if at all possible. However, in scenarios such as using a memory constrained device, you might need to use an MQTT library to communicate with your IoT hub.
1716

1817
The samples in this tutorial use the [Eclipse Mosquitto](http://mosquitto.org/) MQTT library.
1918

2019
In this tutorial, you learn how to:
2120

22-
> [!div class="checklist"]
23-
> * Build the C language device client sample applications.
24-
> * Run a sample that uses the MQTT library to send telemetry.
25-
> * Run a sample that uses the MQTT library to process a cloud-to-device message sent from your IoT hub.
26-
> * Run a sample that uses the MQTT library to manage the device twin on the device.
21+
* Build the C language device client sample applications.
22+
* Run a sample that uses the MQTT library to send telemetry.
23+
* Run a sample that uses the MQTT library to process a cloud-to-device message sent from your IoT hub.
24+
* Run a sample that uses the MQTT library to manage the device twin on the device.
2725

2826
You can use either a Windows or Linux development machine to complete the steps in this tutorial.
2927

3028
If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin.
3129

3230
## Prerequisites
3331

34-
[!INCLUDE [azure-cli-prepare-your-environment-h3](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-h3.md)]
35-
3632
### Development machine prerequisites
3733

38-
If you're using Windows:
34+
# [Windows](#tab/windows)
3935

4036
1. Install [Visual Studio (Community, Professional, or Enterprise)](https://visualstudio.microsoft.com/downloads). Be sure to enable the **Desktop development with C++** workload.
4137

4238
1. Install [CMake](https://cmake.org/download/). Enable the **Add CMake to the system PATH for all users** option.
4339

4440
1. Install the **x64 version** of [Mosquitto](https://mosquitto.org/download/).
4541

46-
If you're using Linux:
42+
# [Linux](#tab/linux)
4743

4844
1. Run the following command to install the build tools:
4945

@@ -57,6 +53,10 @@ If you're using Linux:
5753
sudo apt install libmosquitto-dev
5854
```
5955

56+
---
57+
58+
[!INCLUDE [azure-cli-prepare-your-environment-h3](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-h3.md)]
59+
6060
## Set up your environment
6161

6262
If you don't already have an IoT hub, run the following commands to create a free-tier IoT hub in a resource group called `mqtt-sample-rg`. The command uses the name `my-hub` as an example for the name of the IoT hub to create. Choose a unique name for your IoT hub to use in place of `my-hub`:
@@ -85,6 +85,8 @@ Make a note of the SAS token the command outputs as you need it later. The SAS t
8585
> [!TIP]
8686
> By default, the SAS token is valid for 60 minutes. The `--du 7200` option in the previous command extends the token duration to two hours. If it expires before you're ready to use it, generate a new one. You can also create a token with a longer duration. To learn more, see [az iot hub generate-sas-token](/cli/azure/iot/hub#az-iot-hub-generate-sas-token).
8787

88+
[!INCLUDE [iot-authentication-device-connection-string](../../includes/iot-authentication-device-connection-string.md)]
89+
8890
## Clone the sample repository
8991

9092
Use the following command to clone the sample repository to a suitable location on your local machine:
@@ -148,7 +150,7 @@ Run the _mosquitto_telemetry_ sample. For example, on Linux:
148150

149151
The `az iot hub monitor-events` generates the following output that shows the payload sent by the device:
150152

151-
```text
153+
```output
152154
Starting event monitor, use ctrl-c to stop...
153155
{
154156
"event": {
@@ -199,11 +201,11 @@ if (rc != MOSQ_ERR_SUCCESS)
199201
printf("Publish returned OK\r\n");
200202
```
201203
202-
To learn more, see [Sending device-to-cloud messages](./iot-mqtt-connect-to-iot-hub.md#sending-device-to-cloud-messages).
204+
To learn more, see [Sending device-to-cloud messages](./iot-mqtt-connect-to-iot-hub.md#send-device-to-cloud-messages).
203205
204206
## Receive a cloud-to-device message
205207
206-
The *mosquitto_subscribe* sample shows how to subscribe to MQTT topics and receive a cloud-to-device message from your IoT hub by using the MQTT library.
208+
The _mosquitto_subscribe_ sample shows how to subscribe to MQTT topics and receive a cloud-to-device message from your IoT hub by using the MQTT library.
207209
208210
Run the _mosquitto_subscribe_ sample. For example, on Linux:
209211
@@ -219,7 +221,7 @@ az iot device c2d-message send --hub-name my-hub --device-id mqtt-dev-01 --data
219221
220222
The output from _mosquitto_subscribe_ looks like the following example:
221223
222-
```text
224+
```output
223225
Waiting for C2D messages...
224226
C2D message 'hello world' for topic 'devices/mqtt-dev-01/messages/devicebound/%24.mid=d411e727-...f98f&%24.to=%2Fdevices%2Fmqtt-dev-01%2Fmessages%2Fdevicebound&%24.ce=utf-8&iothub-ack=none'
225227
Got message for devices/mqtt-dev-01/messages/# topic
@@ -252,11 +254,11 @@ void message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_
252254
}
253255
```
254256
255-
To learn more, see [Use MQTT to receive cloud-to-device messages](./iot-mqtt-connect-to-iot-hub.md#receiving-cloud-to-device-messages).
257+
To learn more, see [Use MQTT to receive cloud-to-device messages](./iot-mqtt-connect-to-iot-hub.md#receive-cloud-to-device-messages).
256258
257259
## Update a device twin
258260
259-
The *mosquitto_device_twin* sample shows how to set a reported property in a device twin and then read the property back.
261+
The _mosquitto_device_twin_ sample shows how to set a reported property in a device twin and then read the property back.
260262
261263
Run the _mosquitto_device_twin_ sample. For example, on Linux:
262264
@@ -266,7 +268,7 @@ Run the _mosquitto_device_twin_ sample. For example, on Linux:
266268
267269
The output from _mosquitto_device_twin_ looks like the following example:
268270
269-
```text
271+
```output
270272
Setting device twin reported properties....
271273
Device twin message '' for topic '$iothub/twin/res/204/?$rid=0&$version=2'
272274
Setting device twin properties SUCCEEDED.
@@ -348,17 +350,12 @@ void message_callback(struct mosquitto* mosq, void* obj, const struct mosquitto_
348350
}
349351
```
350352
351-
To learn more, see [Use MQTT to update a device twin reported property](./iot-mqtt-connect-to-iot-hub.md#update-device-twins-reported-properties) and [Use MQTT to retrieve a device twin property](./iot-mqtt-connect-to-iot-hub.md#retrieving-a-device-twins-properties).
353+
To learn more, see [Use MQTT to update a device twin reported property](./iot-mqtt-connect-to-iot-hub.md#update-device-twins-reported-properties) and [Use MQTT to retrieve a device twin property](./iot-mqtt-connect-to-iot-hub.md#retrieve-device-twin-properties).
352354
353355
## Clean up resources
354356
355357
[!INCLUDE [iot-pnp-clean-resources](../../includes/iot-pnp-clean-resources.md)]
356358
357359
## Next steps
358360
359-
Now that you've learned how to use the Mosquitto MQTT library to communicate with IoT Hub, a suggested next step is to review:
360-
361-
> [!div class="nextstepaction"]
362-
> [Communicate with your IoT hub using the MQTT protocol](./iot-mqtt-connect-to-iot-hub.md)
363-
> [!div class="nextstepaction"]
364-
> [MQTT Application samples](https://github.com/Azure-Samples/MqttApplicationSamples)
361+
Now that you've learned how to use the Mosquitto MQTT library to communicate with IoT Hub, a suggested next step is to review the [MQTT Application samples](https://github.com/Azure-Samples/MqttApplicationSamples) on GitHub.

0 commit comments

Comments
 (0)