我是 AzureIot 的初学者,我已经获得了拥有 IOT 中心的 azure 帐户的访问权限。从 azure 门户中,我看到了以下形式的连接字符串:
HostName=iothub-mycompany.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=_somekey_in_base64_
现在我只需要简单地将 JSON 有效负载发送到这个 IOT 集线器。任何协议都可以。
我假设当我发送消息时,我可以使用此设备资源管理器工具看到此消息。但我不确定我是否做错了什么,或者我的理解存在缺陷。
我尝试了博客、msdn 等中的示例最后我想出了下面的代码
它运行,但是我不知道它在做什么以及在哪里查找此消息是否已发送。
设备资源管理器不显示任何内容。
我检查了活动登录 portal.azure.com,但它看起来更像是登录活动日志。
- 同样在 azure portal Under explorers I See IoT Devices。我看到了我需要定位的设备名称。(我知道它存在)
这是代码,是 .Net4.5.2 ConsoleApplication 的一部分。
Program.cs
:
class Program
{
static DeviceClient deviceClient;
const string IOT_HUB_CONN_STRING = "connection str in format specified above.";
const string IOT_HUB_DEVICE_LOCATION = "_some_location.";
const string IOT_HUB_DEVICE = "device_id";
static void Main(string[] args)
{
var ret = SendMessageToIoTHubAsync("temperature", 15);
Console.WriteLine("completed:" + ret.IsCompleted);
}
private static async Task SendMessageToIoTHubAsync(string sensorType, int sensorState)
{
deviceClient = DeviceClient.CreateFromConnectionString(IOT_HUB_CONN_STRING, TransportType.Mqtt);//i tried other trsnsports
try
{
var payload = "{" +
"\"deviceId\":\"" + IOT_HUB_DEVICE + "\", " +
"\"location\":\"" + IOT_HUB_DEVICE_LOCATION + "\", " +
"\"sensorType\":\"" + sensorType + "\", " +
"\"sensorState\":" + sensorState + ", " +
"\"localTimestamp\":\"" + DateTime.Now.ToLocalTime() + "\"" +
"}";
var msg = new Message(Encoding.UTF8.GetBytes(payload));
Debug.WriteLine("\t{0}> Sending message: [{1}]", DateTime.Now.ToLocalTime(), payload);
await deviceClient.SendEventAsync(msg);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("!!!! " + ex.Message);
}
}
}
作为旁注,我的“设备”是ClearScada。我将有 C# 驱动程序,它将从 ClearScada 实例中获取数据,该驱动程序应该将该数据发送到 azure iot 集线器。基本上我只需要计算从 C# 到 Azure iot 的发送。 问题:
- 我在哪里可以看到我的消息。?
- 我的功能是正确的还是我的理解有缺陷。