Azure IoT libraries for .NET
Overview
Azure IoT Hub is a fully managed service that enables reliable and secure bi-directional communications between millions of devices and a solution back end.
Devices and data sources in an IoT solution can range from a simple network-connected sensor to a powerful, standalone computing device. Devices may have limited processing capability, memory, communication bandwidth, and communication protocol support. The IoT device SDKs enable you to implement client applications for a wide variety of devices and back-end applications.
The device SDK for .NET facilitates building devices running .NET that connect to Azure IoT Hub.
The service SDK for .NET facilitates building back-end applications using .NET that manage and allow controlling devices from the Cloud.
Client library
Use the .NET IoT devices client to connect and send messages to your IoT Hub.
Install the NuGet package directly from the Visual Studio Package Manager console or with the .NET Core CLI.
Visual Studio Package Manager
Install-Package Microsoft.Azure.Devices.Client
dotnet add package Microsoft.Azure.Devices.Client
Code Examples
This example connects to the IoT Hub and sends one message per second.
string deviceKey = "<deviceKey>";
string deviceId = "<deviceId>";
string iotHubHostName = "<IoTHubHostname>";
var deviceAuthentication = new DeviceAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey);
DeviceClient deviceClient = DeviceClient.Create(iotHubHostName, deviceAuthentication, TransportType.Mqtt);
while (true)
{
double currentTemperature = 20 + Rand.NextDouble() * 15;
double currentHumidity = 60 + Rand.NextDouble() * 20;
var telemetryDataPoint = new
{
messageId = _messageId++,
deviceId = deviceId,
temperature = currentTemperature,
humidity = currentHumidity
};
string messageString = JsonConvert.SerializeObject(telemetryDataPoint);
Message message = new Message(Encoding.ASCII.GetBytes(messageString));
message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");
await deviceClient.SendEventAsync(message);
Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);
await Task.Delay(1000);
}
Samples
View the complete list of Azure IoT Upsamples.
View the Azure IoT Hub developer guide for more guidance.
Azure SDK for .NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기