Receiving alerts/commands from Azure IoT Hub to device
Internet of Things
IoT Frameworks
.
2 years ago
0
0
0
0
+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.