简体   繁体   English

物联网中心无法接收或发送消息

[英]IOT hub not recieving or sending messages

Iam trying to make a simple app for my raspberry pi that will send a message to the IOThub and then try to receive a response however nothing is happening. Iam尝试为我的树莓派制作一个简单的应用程序,该应用程序将向IOThub发送消息,然后尝试接收响应,但是什么也没有发生。

I copied the connectionstring from the device controller. 我从设备控制器复制了连接字符串。 Ofcource I hidden it for this question. 当然,我对此问题隐藏了。

I see it printing out that the message was succesfully sended to the iothub but when I check the iothub I see 0 received messages. 我看到它打印出该消息已成功发送到iothub,但是当我检查iothub时,看到0条收到的消息。

Iam using the free tier of the iothub is this a limitation? Iam使用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");
    }
}

It is nothing to do with free tier of the iothub. 这与iothub的免费层无关。 There is no such limitation. 没有这样的限制。

You can't receive Device-To-Cloud(D2C) messages using DeviceClient that you used in ReceiveCommands() . 您无法使用ReceiveCommands()使用的DeviceClient接收“ 设备到云(D2C)”消息 It is by designed. 它是设计使然。 You look like having some misunderstanding of Azure IoT Hub message types and SDKs. 您似乎对Azure IoT中心消息类型和SDK有一些误解。

There are two kinds of message types: Device-To-Cloud(D2C) message and Cloud-To-Device(C2D) message . 消息类型有两种: 设备到云(D2C)消息云到设备(C2D)消息

And two kinds of SDKs: device SDK and service SDK . 还有两种SDK: 设备SDK服务SDK

Device SDK is used to connect to and send D2C messages to Azure IoT Hub. 设备SDK用于连接到Azure IoT中心并将D2C消息发送到Azure IoT中心。 While service SDK is used to manage and send C2D messages to devices. 而服务SDK用于管理C2D消息并将其发送到设备。

So, if you send C2D messages to device in Device Explorer you will receive these messages in your ReceiveCommands method. 因此,如果您在设备资源管理器中向设备发送C2D消息,您将在ReceiveCommands方法中收到这些消息。

If you want to receive D2C message you can utilize Event Hub-compatible endpoint(messages/events). 如果要接收D2C消息,则可以使用事件中心兼容的终结点(消息/事件)。 Here is a console sample you can reference. 这是您可以参考的控制台示例 But this is can't be done in UWP due to service bus not supported in UWP . 但是由于UWP 不支持服务总线,因此无法在UWP中完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM