![](/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.