2017-04-09 56 views
0

Iam试图为我的树莓派制作一个简单的应用程序,它会向IOThub发送消息,然后尝试接收响应,但没有任何事情发生。物联网集线器没有收到或发送消息

我从设备控制器复制了连接字符串。我把这个问题隐藏起来了。

我看到它打印出消息已成功发送给iothub,但是当我检查iothub时,我看到0收到的消息。

使用iothub的免费层的Iam是这个限制吗?

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"); 
    } 
} 
+0

您能看到您在[Device Explorer]中发送的D2C消息吗(https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer#run-the-sample-应用)? –

+1

是iam得到这个:接收事件... 2017-04-10 20:08:53>设备:[RaspberryPI],数据:[云到设备消息。] – Barsonax

+1

那么,你是什么意思“我检查iothub我看到0个收到的消息。“?你的意思是你使用'ReceiveCommands()'来接收这些D2C消息? –

回答

3

这与iothub的免费层无关。没有这样的限制。

您不能使用ReceiveCommands()中使用的DeviceClient接收Device-To-Cloud(D2C) messages。它是由设计。您看起来对Azure IoT Hub消息类型和SDK存在误解。

有两种消息类型:Device-To-Cloud(D2C) messageCloud-To-Device(C2D) message

还有两种SDK:device SDKservice SDK

Device SDK用于连接并发送D2C消息到Azure IoT Hub。 服务SDK用于管理和发送设备C2D messages

因此,如果您向设备发送C2D消息Device Explorer,您将在ReceiveCommands方法中收到这些消息。

如果您想要接收D2C消息,您可以使用Event Hub兼容的端点(消息/事件)。这里是你可以参考的a console sample。但由于service bus not supported in UWP的原因,UWP无法完成此操作。

相关问题