I have created a IoT hub in Azure.
Register a device into IoT hub using below code -
public async Task<HttpResponseMessage> RegisterDeviceAsyncData(DeviceData deviceApp)
{
deviceApp.PlatformName = _appSettingsAccessor.GetAppSettingValue(DEFAULT_HUB_KEY);
var result = await _deviceRegistrationHandler.RegisterDeviceAsync(deviceApp);
response = HttpResponseMessageFactory.CreateMessageWithObjectBody(results);
}
it is successfully registering devices into Iot hub, now I want to send data to these devices using C#.
I have code to send data to device below but I don't know how to get connection string of particular device or key to send data.
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);
}
}
}
For now I tried to get connection string using portal.azure.com, but in real time after register device how can I retrieve a connection string? Can I use a SAS token or something like that?