【发布时间】:2018-01-14 16:06:12
【问题描述】:
我是 AzureIot 的初学者,我已获得访问具有 IOT 中心的 azure 帐户的权限。
从 azure 门户中,我看到了以下形式的连接字符串:
HostName=iothub-mycompany.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=_somekey_in_base64_
现在我只需要向这个 IOT 中心发送 JSON 有效负载。任何协议都可以。
我假设当我发送消息时,我可以看到带有this device explorer tool 的消息。但我不确定是我做错了什么还是我的理解有缺陷。
我尝试了博客、msdn 等中的示例最后我想出了下面的代码
它运行,但我不知道它在做什么以及到哪里查找此消息是否已发送。
设备浏览器不显示任何内容。
我查看了活动登录 portal.azure.com,但它看起来更像是登录活动日志。
- 也在 azure 门户中在资源管理器下我看到了物联网设备。我看到了我需要定位的设备名称。(我知道它存在)
这是代码,是 .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 hub。 基本上我只需要计算从 C# 发送 到 Azure iot。问题:
- 在哪里可以看到我的消息。?
- 我的功能是正确的还是我的理解有缺陷。
【问题讨论】:
标签: c# azure azure-iot-hub scada