Receiving alerts/commands from Azure IoT Hub to device

Internet of Things IoT Frameworks . 2 years ago

  0   1   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating
_x000D_ _x000D_ 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(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.

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (1)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ 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));
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community