Barsonax
Barsonax

Reputation: 312

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.

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.

Iam using the free tier of the iothub is this a limitation?

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

Upvotes: 1

Views: 1423

Answers (1)

Rita Han
Rita Han

Reputation: 9700

It is nothing to do with free tier of the iothub. There is no such limitation.

You can't receive Device-To-Cloud(D2C) messages using DeviceClient that you used in ReceiveCommands(). It is by designed. You look like having some misunderstanding of Azure IoT Hub message types and SDKs.

There are two kinds of message types: Device-To-Cloud(D2C) message and Cloud-To-Device(C2D) message.

And two kinds of SDKs: device SDK and service SDK.

Device SDK is used to connect to and send D2C messages to Azure IoT Hub. While service SDK is used to manage and send C2D messages to devices.

So, if you send C2D messages to device in Device Explorer you will receive these messages in your ReceiveCommands method.

If you want to receive D2C message you can utilize Event Hub-compatible endpoint(messages/events). 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.

Upvotes: 4

Related Questions