Post

Azure | Build .Net IoT App using C#

Build .Net IoT App using C#

Azure IoT provides a robust platform for building Internet of Things (IoT) solutions. Here are some C# examples to help you get started with Azure IoT services.

Azure IoT Hub

Azure IoT Hub is a fully managed service that enables reliable and secure communication between IoT devices and the cloud. Here’s how to interact with it using C#:

Send Telemetry Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using Microsoft.Azure.Devices.Client;
using System.Text;
using System.Threading.Tasks;

string deviceConnectionString = "Your Device Connection String";
using var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, TransportType.Mqtt);

var telemetryData = new
{
    temperature = 25.5,
    humidity = 60
};
var messageString = JsonConvert.SerializeObject(telemetryData);
var message = new Message(Encoding.UTF8.GetBytes(messageString));

await deviceClient.SendEventAsync(message);

Receive Cloud-to-Device Messages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using Microsoft.Azure.Devices.Client;
using System.Text;
using System.Threading.Tasks;

string deviceConnectionString = "Your Device Connection String";
using var deviceClient = DeviceClient.CreateFromConnectionString(deviceConnectionString, TransportType.Mqtt);

Message receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
    var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
    Console.WriteLine($"Received message: {messageData}");
    await deviceClient.CompleteAsync(receivedMessage);
}

Azure IoT Device Provisioning Service

Azure IoT Device Provisioning Service (DPS) simplifies the initial setup of IoT devices. Here’s how to use it with C#:

Register a Device with DPS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using Microsoft.Azure.Devices.Provisioning.Service;
using Microsoft.Azure.Devices.Shared;
using System.Threading.Tasks;

string idScope = "Your DPS ID Scope";
string registrationId = "Your Device Registration ID";
string primaryKey = "Your Device Primary Key";

var provisioningServiceClient = ProvisioningServiceClient.CreateFromConnectionString($"HostName={idScope};SharedAccessKeyName=provisioningserviceowner;SharedAccessKey={primaryKey}");

var individualEnrollment = new IndividualEnrollment(registrationId)
{
    Attestation = new TpmAttestation(),
    ProvisioningStatus = ProvisioningStatus.Enabled,
    DeviceID = "Your Device ID"
};

await provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollment);

Azure IoT Edge

Azure IoT Edge extends IoT Hub to edge devices. You can run code and manage devices on the edge. Here’s how to get started with C#:

Create an IoT Edge Module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Edge.Agent.Core;
using Microsoft.Azure.Devices.Edge.ModuleUtil;

string connectionString = "Your IoT Hub Connection String";
string deviceId = "Your Device ID";
string moduleId = "Your Module ID";

var edgeAgentModule = new DockerModule(
    "mcr.microsoft.com/azureiotedge-agent:1.0",
    Constants.EdgeAgentModuleName,
    Constants.EdgeRuntimeContainerName,
    new DockerConfig("linux/amd64", new DockerLoggingConfig(), new DockerRestartPolicy(0, "never")), null);

var deploymentConfig = new DeploymentConfig("1.0");

await ModuleUtil.DeployModuleAsync(connectionString, deviceId, moduleId, edgeAgentModule, deploymentConfig);

What Next?

These examples cover some of the essential tasks you can perform using C# with Azure IoT services. For more advanced scenarios and detailed documentation, refer to the Azure IoT documentation. Make sure to replace placeholders like “Your Device Connection String” or “Your DPS ID Scope” with your actual Azure IoT service information.

This post is licensed under CC BY 4.0 by the author.