• MqttProvider
  • MqttProvider Class

    Provides means of communicating to devices via MQTT. More...

    Header: #include <MqttProvider>

    Detailed Description

    Provides means of communicating to devices via MQTT

    The MQTT provider class offers 2 different ways to establish a MQTT connection to a MQTT enabled IoT device: For one it supports establishing a secure channel between the device and the plugin, using nymea's internal MQTT broker. The second approach is to provide a generic MQTT client object connected to nymea's internal MQTT broker.

    Note, if you wish to establish a MQTT connection to an external MQTT broker, you can just create your own MqttClient and connect to wherever you need to. The MqttProvider hardware resource only manages connections to the nymea internal MQTT broker.

    If using MqttProvider hardware resource, the MQTT broker will be automatically configured with appropriate policies.

    \chapterExample 1

    Establishing a secure MQTT directly between plugin and the MQTT device:

    devicepluginexample.h

    #include "network/mqtt/mqttprovider.h"
    #include "network/mqtt/mqttchannel.h"
    
    class DevicePluginExample : public DevicePlugin
    {
    ...
    public:
        DeviceManager::DeviceSetupStatus setupDevice(Device* device) override;
        void deviceRemoved(Device* device) override;
    
    private slots:
        void onDevicePublishReceived(MqttChannel* channel, const QString &topic, const QByteArray &payload);
    
    ...
    
    };

    devicepluginexample.cpp

    DeviceManager::DeviceSetupStatus DevicePluginExample::setupDevice(Device *device) {
        MqttChannel *channel = hardware()->mqttProvider()->createChannel(device->id(), "192.168.0.100");
    
        // Provision credentials to device:
        QString clientId = channel->clientId();
        QString username = channel->username();
        QString password = channel->password();
        hardwareManager()->networkManager()->post("https://192.168.0.100/setup?clientId=" + username + "&username=" + username);
    
        // connect to publishes in this channel
        connect(channel, &MqttChannel::publishReceived, this, onDevicePublishReceived);
    
        // publish to the channel
        channel->publish(channel->prefix() + "/my/test/topic", "Hello world");
    }
    
    void DevicePluginExample::deviceRemoved(Device* device) {
        hardware()->mqttProvider()->releaseChannel(channel);
    }
    
    void DevicePluginExample::onDevicePublishReceibed(MqttChannel *channel, const QString &topic, const QByteArray &payload) {
        qCDebug(dcExample()) << "Publish received from:" << channel->clientId() << topic << payload;
    }

    \chapterExample 2

    Obtaining a MQTT client connected to the nymea internal MQTT broker:

    devicepluginexample.h

    #include "network/mqtt/mqttprovider.h"
    #include "<nymea-mqtt/mqttclient.h>
    
    class DevicePluginExample : public DevicePlugin
    {
    ...
    public:
        DeviceManager::DeviceSetupStatus setupDevice(Device* device) override;
        void deviceRemoved(Device* device) override;
    
    private slots:
        void onPublishReceived(const QString &topic, const QByteArray &payload, bool retained);
    
    ...
    
    };

    devicepluginexample.cpp

    DeviceManager::DeviceSetupStatus DevicePluginExample::setupDevice(Device *device) {
        MqttClient *client = hardware()->mqttProvider()->createInternalClient(device->id());
    
        // subscribe to topics
        client->subscribe("/my/test/topic", Mqtt::QuS1);
    
        // connect to publishes in this channel
        connect(channel, &MqttClient::publishReceived, this, onPublishReceived);
    
        // publish to the broker
        client->publish("/my/test/topic", "Hello world");
    }
    
    void DevicePluginExample::deviceRemoved(Device* device) {
        hardware()->mqttProvider()->releaseChannel(channel);
    }
    
    void DevicePluginExample::onPublishRecei(const QString &topic, const QByteArray &payload, bool retained) {
        qCDebug(dcExample()) << "Publish received from:" << channel->clientId() << topic << payload << retained;
    }

    See also HardwareResource and HardwareManager::mqttProvider().