![](/img/trans.png)
[英]How to register a device using device type connection string in Azure IoT Hub c#?
[英]How to get device key using device id from IoT hub using C#?
我在 Azure 中創建了一個 IoT 中心。
使用以下代碼將設備注冊到 IoT 中心 -
public async Task<HttpResponseMessage> RegisterDeviceAsyncData(DeviceData deviceApp)
{
deviceApp.PlatformName = _appSettingsAccessor.GetAppSettingValue(DEFAULT_HUB_KEY);
var result = await _deviceRegistrationHandler.RegisterDeviceAsync(deviceApp);
response = HttpResponseMessageFactory.CreateMessageWithObjectBody(results);
}
它已成功將設備注冊到 Iot 集線器,現在我想使用 C# 將數據發送到這些設備。
我有代碼將數據發送到下面的設備,但我不知道如何獲取特定設備的連接字符串或密鑰來發送數據。
private static async Task SendDeviceToCloudMessagesAsync()
{
DeviceClient deviceClient = DeviceClient.CreateFromConnectionString("deviceConnectionString");
while (true)
{
string messageString = string.Empty;
Message iotHubMsg = new Message();
foreach (BaseMessage msg in currentDevice.Messages)
{
iotHubMsg = msg.ReadAsIotHubMessage();
messageString = Encoding.UTF8.GetString(iotHubMsg.GetBytes());
//Send the current messages and clear them
await deviceClient.SendEventAsync(iotHubMsg);
}
}
}
現在我嘗試使用 portal.azure.com 獲取連接字符串,但在注冊設備后如何實時檢索連接字符串? 我可以使用 SAS 令牌或類似的東西嗎?
我會嘗試使用以下方法之一解決這個問題:
希望這對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.