Table of Contents

Class MqttConnectionClient

Namespace
nanoFramework.Aws.IoTCore.Devices
Assembly
nanoFramework.Aws.IoTCore.Devices.dll

AWS IoT Core MQTT Connection Client for .NET nanoFramework

public class MqttConnectionClient : IDisposable
Inheritance
MqttConnectionClient
Implements
Inherited Members
Extension Methods

Remarks

Constructors

MqttConnectionClient(string, string, X509Certificate2, QoSLevel, X509Certificate)

Creates an MqttConnectionClient class.

public MqttConnectionClient(string iotCoreUri, string uniqueId, X509Certificate2 clientCert, MqttConnectionClient.QoSLevel qosLevel = QoSLevel.AtMostOnce, X509Certificate awsRootCert = null)

Parameters

iotCoreUri string

The AWS IoT Core fully qualified domain name (example: 'instance'.'region'.amazonaws.com)

uniqueId string

A unique identity for your device (Device ID / Thing Name).

clientCert X509Certificate2

The certificate used to connect the device to the MQTT broker (containing both the private certificate and private key).

qosLevel MqttConnectionClient.QoSLevel

The default quality of service level for the delivery of MQTT messages, (defaults to the lowest quality)

awsRootCert X509Certificate

The Root (AWS) certificate for the connection to AWS IoT Core

Properties

ConnectionStatus

The latest connection status.

public ConnectorState ConnectionStatus { get; }

Property Value

ConnectorState

IsConnected

True if the device connected successfully.

public bool IsConnected { get; }

Property Value

bool

LastShadow

The latest Shadow received.

public Shadow LastShadow { get; }

Property Value

Shadow

QosLevel

The default quality of service level.

public MqttQoSLevel QosLevel { get; set; }

Property Value

MqttQoSLevel

ThingName

The name of the "Thing"

public string ThingName { get; }

Property Value

string

Remarks

Otherwise known as the "clienttoken" or "Device ID"

Methods

Close()

Close the connection with AWS IoT Core MQTT and disconnect the device.

public void Close()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

GetShadow(CancellationToken, string)

Gets the device shadow.

public Shadow GetShadow(CancellationToken cancellationToken = default, string namedShadow = "")

Parameters

cancellationToken CancellationToken

A cancellation token

namedShadow string

A named shadow

Returns

Shadow

The shadow.

Remarks

It is strongly recommended to use a cancellation token that can be canceled and manage this on the caller code level. A reasonable time of few seconds is recommended with a retry mechanism.

Open()

Open the connection with AWS IoT Core. This will connect AWS IoT Core (via MQTT) to the device.

public bool Open()

Returns

bool

True for a successful connection

Open(string)

Overrides the default topic prefix of "device"

public bool Open(string topicPrefix)

Parameters

topicPrefix string

The topic prefix for telemetry, device messages and LWT.

Returns

bool

True for a successful connection

Open(string, string, string)

Overrides the default connection topics

public bool Open(string telemetryTopic, string deviceMessageTopic, string lwtTopic)

Parameters

telemetryTopic string

The telemetry topic

deviceMessageTopic string

The device message topic

lwtTopic string

The last will and testament topic

Returns

bool

True for a successful connection

Reconnect()

Reconnect to AWS Iot Core MQTT.

public void Reconnect()

SendMessage(string, CancellationToken)

Send a message to Aws IoT Core.

public bool SendMessage(string message, CancellationToken cancellationToken = default)

Parameters

message string

The message to send.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

Returns

bool

True for successful message delivery.

UpdateReportedState(Shadow, CancellationToken, string)

Update the device shadow reported state.

public bool UpdateReportedState(Shadow shadowToSend, CancellationToken cancellationToken = default, string namedShadow = "")

Parameters

shadowToSend Shadow

The Shadow to send.

cancellationToken CancellationToken

A cancellation token. If you use the default one, the confirmation of delivery will not be awaited.

namedShadow string

The supplementary Shadow name.

Returns

bool

True for successful message delivery.

Events

CloudToDeviceMessage

Cloud to device message received event.

public event CloudToDeviceMessage CloudToDeviceMessage

Event Type

CloudToDeviceMessage

ShadowUpdated

Device shadow updated event.

public event ShadowUpdated ShadowUpdated

Event Type

ShadowUpdated

StatusUpdated

Status change event.

public event StatusUpdated StatusUpdated

Event Type

StatusUpdated