【发布时间】:2017-04-09 18:47:58
【问题描述】:
我正在尝试为我的树莓派制作一个简单的应用程序,该应用程序将向 IOThub 发送一条消息,然后尝试接收响应,但没有任何反应。
我从设备控制器复制了连接字符串。我为这个问题隐藏了它。
我看到它打印出消息已成功发送到 iothub,但是当我检查 iothub 时,我看到 0 条收到的消息。
我正在使用 iothub 的免费层,这是一个限制吗?
public sealed partial class MainPage : Page
{
private const string DeviceConnectionString = "Hidden";
private readonly DeviceClient _deviceClient;
public MainPage()
{
this.InitializeComponent();
_deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Amqp); //Already tried using different transport types but no succes.
}
public async Task SendEvent()
{
Debug.WriteLine("\t{0}> Sending message", DateTime.Now.ToLocalTime());
var commandMessage = new Message(Encoding.ASCII.GetBytes("Cloud to device message."));
await _deviceClient.SendEventAsync(commandMessage);
Debug.WriteLine("Succesfully sended message to IotHub");
}
public async Task ReceiveCommands()
{
Debug.WriteLine("\nDevice waiting for commands from IoTHub...\n");
while (true)
{
var receivedMessage = await _deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
var messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes());
Debug.WriteLine("\t{0}> Received message: {1}", DateTime.Now.ToLocalTime(), messageData);
var propCount = 0;
foreach (var prop in receivedMessage.Properties)
{
Debug.WriteLine("\t\tProperty[{0}> Key={1} : Value={2}", propCount++, prop.Key, prop.Value);
}
await _deviceClient.CompleteAsync(receivedMessage);
Debug.WriteLine("Finishing recieving message");
}
await Task.Delay(TimeSpan.FromSeconds(1));
}
}
private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Debug.WriteLine("Sending event");
await SendEvent();
await ReceiveCommands();
Debug.WriteLine("Received commands");
}
}
【问题讨论】:
-
你能看到你在Device Explorer发送的D2C消息吗?
-
是的,我得到了这个:接收事件... 2017-04-10 20:08:53> 设备:[RaspberryPI],数据:[云到设备消息。]
-
嗯,你是什么意思“我检查了iothub,我看到0条收到的消息。”?你的意思是你使用
ReceiveCommands()来接收这些D2C消息? -
是的,我希望 ReceiveCommands 从 iothub 获取消息。我登录azure并去iothub时没有看到任何消息。
标签: azure raspberry-pi windows-10-iot-core azure-iot-hub