0
votes

I am currently doing R & D on Azure IoT hubs using Windows10 IoT core and Raspberry PI 2. I am following the sample for reference here to send alerts to the device from IoT hub when the temperature of a room greater than 25 for example. But the sample is for mbed board.

To do this I developed a sample UWP app for Raspberry Pi which will send temperature data to IoT hub. In Azure I created a stream analytics job which will take IoT hub as input and filter the data( only temperature greater than 25 degrees) then send it to output EventHub. Here I created a worker role/cloud service which will read data from EventHub and send back to IoT hub to the same one which I am using for sending temperature information from raspberry pi.

Here my doubt is how can IoT Hub differentiate the data sent from raspberry pi and the data sent by worker role? and how can I receive only the data sent by worker role?

Because if I read cloud to device messages I am receiving the data which I sent from raspberry pi.

Here I got stuck, I tried the below code to read data from IoT Hub but getting all my messages sent from raspberry pi instead worker role messages that is only temperature greater than 25 messages.

public async void ReceiveDataFromCloud()
    {


        startingDateTimeUtc = DateTime.UtcNow;
        ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString);
        builder.TransportType = ppatierno.AzureSBLite.Messaging.TransportType.Amqp;

        factory = MessagingFactory.CreateFromConnectionString(ConnectionString);

        client = factory.CreateEventHubClient(eventHubEntity);
        group = client.GetDefaultConsumerGroup();
        receiver = group.CreateReceiver(partitionId.ToString(), startingDateTimeUtc);//startingDateTimeUtc
        for (int i = 0; i <= 0; i++)
        {
            while (true)
            {
                EventData data = receiver.Receive();

                if (data != null)
                {
                    var receiveddata = Encoding.UTF8.GetString(data.GetBytes());

                    //var messageString = JsonConvert.DeserializeObject<ConferenceRooms>(receiveddata);                    

                    Debug.WriteLine("{0} {1} {2}", data.SequenceNumber, data.EnqueuedTimeUtc.ToLocalTime(), Encoding.UTF8.GetString(data.GetBytes()));

                }
                else
                {
                    break;
                }

                await Task.Delay(2000);

            }

        }

        receiver.Close();
        client.Close();
        factory.Close();

    }

How to send alerts from IoT Hub back to devices only the messages filtered by stream analytic job?

Update:

When I use the above mentioned code for receiving I am getting all the messages from IoT Hub which are sent by raspberry pi.

But when I use the below code for receiving message, I am getting only the messages sent by worker role to IoT Hub.

while (true)
        {
            Message receivedMessage = await deviceClient.ReceiveAsync();
            if (receivedMessage == null) continue;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
            Console.ResetColor();

            await deviceClient.CompleteAsync(receivedMessage);
        }

This is what my requirement is and I am able to achieve it.

1

1 Answers

0
votes

IoT Hub provides bi-directional asymmetrical way of communication between device and the cloud. The process of receiving messages from the cloud on your IoT device is described quite well in this article.

In short, try to use the following code to receive cloud-to-device messages from IoT Hub:

while (true)
{
    Message receivedMessage = await deviceClient.ReceiveAsync();
    if (receivedMessage == null) continue;

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("Received message: {0}", Encoding.ASCII.GetString(receivedMessage.GetBytes()));
    Console.ResetColor();

    await deviceClient.CompleteAsync(receivedMessage);
}

Here deviceClient is the instance of Microsoft.Azure.Devices.Client.DeviceClient which you create like this:

deviceClient = DeviceClient.Create(iotHubUri, 
  newDeviceAuthenticationWithRegistrySymmetricKey("myFirstDevice", deviceKey));