2017-04-09 57 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無法完成此操作。

相關問題