DEV Community

Hassan BOLAJRAF
Hassan BOLAJRAF

Posted on

Azure | Build .Net IoT App using C#

Note
You can check other posts on my personal website: https://hbolajraf.net

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

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);
Enter fullscreen mode Exit fullscreen mode

Receive Cloud-to-Device Messages

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);
}
Enter fullscreen mode Exit fullscreen mode

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

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);
Enter fullscreen mode Exit fullscreen mode

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

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);
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)