This samples uses the Mosquitto (MQTT) library to send a message to an Azure IoT Hub.
-
Follow the general prerequisites
-
Install Visual Studio
NOTE Enable the
Desktop development with C++
workload -
Install CMake
NOTE Enable the
Add CMake to the system PATH for all users
option -
Install the x64 version of Mosquitto
-
Follow the general prerequisites
-
Install the build tools:
sudo apt install cmake g++
-
Install Mosquitto client library:
sudo apt install libmosquitto-dev
-
Change to the
mosquitto
directory in the cloned repository -
Edit the
config.h
file and expand the required connection information:#define IOTHUBNAME "{iothub_name}" #define DEVICEID "{device_id}" #define SAS_TOKEN "{sas_token}"
-
Build the application:
cmake -Bbuild cmake --build build
-
Resulting binaries can be found in the following locations:
- Linux:
./build
- Windows
.\build\Debug
- Linux:
This sample will send a single telemetry message, which can be viewed by monitoring the Hubs events.
-
Start monitoring incoming messages on your Hub:
az iot hub monitor-events -n {iothub_name}
-
Run the sample executable:
mosquitto_telemetry
-
Confirm the following output is seen from monitor-events:
{ "event": { "origin": "pubsub", "module": "", "interface": "", "component": "", "payload": "Bonjour MQTT from Mosquitto" } }
This sample will subscribe to all available MQTT topics, and then display an cloud-to-device message sent from the Hub.
-
Run the sample executable:
mosquitto_subscribe
-
Send an event from the Hub to the device:
az iot device c2d-message send --hub-name {iothub_name} --device-id {device_id} --data "hello world"
-
Confirm the following output from the sample:
Waiting for C2D messages... C2D message 'hello world' for topic 'devices/pubsub/messages/devicebound/...` Got message for devices/pubsub/messages/# topic
This sample will add a temperature reported property to the device twin, and then read the property back.
-
Run the sample executable:
mosquitto_device_twin
-
Confirm the following output from the sample, setting and getting a device twin update:
Setting device twin reported properties.... Device twin message '' for topic '$iothub/twin/res/204/?$rid=patch_temp&$version=2' Setting device twin properties SUCCEEDED. Getting device twin properties.... Device twin message '{"desired":{"$version":1},"reported":{"temperature":32,"$version":2}}' for topic '$iothub/twin/res/200/?$rid=123' Getting device twin properties SUCCEEDED.