Skip to content

Latest commit

 

History

History

mosquitto

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Using the Mosquitto library

This samples uses the Mosquitto (MQTT) library to send a message to an Azure IoT Hub.

Windows prerequisites

  1. Follow the general prerequisites

  2. Install Visual Studio

    NOTE Enable the Desktop development with C++ workload

  3. Install CMake

    NOTE Enable the Add CMake to the system PATH for all users option

  4. Install the x64 version of Mosquitto

Linux prerequisites

  1. Follow the general prerequisites

  2. Install the build tools:

    sudo apt install cmake g++
  3. Install Mosquitto client library:

    sudo apt install libmosquitto-dev

Build

  1. Change to the mosquitto directory in the cloned repository

  2. Edit the config.h file and expand the required connection information:

    #define IOTHUBNAME      "{iothub_name}"
    #define DEVICEID        "{device_id}"
    #define SAS_TOKEN       "{sas_token}"
  3. Build the application:

    cmake -Bbuild
    cmake --build build
  4. Resulting binaries can be found in the following locations:

    1. Linux: ./build
    2. Windows .\build\Debug

Run

Telemetry sample

This sample will send a single telemetry message, which can be viewed by monitoring the Hubs events.

  1. Start monitoring incoming messages on your Hub:

    az iot hub monitor-events -n {iothub_name}
  2. Run the sample executable:

    mosquitto_telemetry
  3. Confirm the following output is seen from monitor-events:

    {
        "event": {
            "origin": "pubsub",
            "module": "",
            "interface": "",
            "component": "",
            "payload": "Bonjour MQTT from Mosquitto"
        }
    }

Subscribe sample

This sample will subscribe to all available MQTT topics, and then display an cloud-to-device message sent from the Hub.

  1. Run the sample executable:

    mosquitto_subscribe
  2. 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"
  3. 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

Device twin sample

This sample will add a temperature reported property to the device twin, and then read the property back.

  1. Run the sample executable:

    mosquitto_device_twin
  2. 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.