Get a list of messages sent by IoT device - azure

I am looking for a way to see which device has sent which message in Azure.
Via "IoT-hub" we can get a list of the devices but I cannot seem to find a way to correlate the messages to the devices.
Doe anyone have any idea?
Thanks in advance.
Regards

Have a look at this document for more details about the message format.
The device id is a part of the IoT Hub message system properties such as ConnectionDeviceId.
The following example shows a query of the ASA job. You can see how to get the device id from the telemetry message via the stream pipeline:
WITH subquery as (
SELECT
System.Timestamp as time,
counter,
temperature,
humidity,
EventProcessedUtcTime,
IoTHub.ConnectionDeviceId as deviceId,
IoTHub.MessageId as messageId
FROM iot Timestamp by time
)
SELECT
*
INTO
outBlob
FROM
subquery
another example is for Azure EventHubTrigger Function (AF). The telemetry message from the stream pipeline (events endpoint) is serialized into the EventData object and pushed to the AF:
public static async Task Run(EventData ed, TraceWriter log)
{
log.Info($"Label = {ed.SystemProperties["iothub-message-source"]} -{ed.SystemProperties["iothub-connection-device-id"]}/{ed.SequenceNumber}");
// ...
}

You could try Azure IoT Toolkit extension for VS Code to monitor all the messages sent to Azure IoT Hub. You could see which device has sent what message to Azure IoT Hub.
You could also refer to this blog post for more details about how to use this extension to monitor messages.

When a message arrives in IoT Hub, the hub adds a number of system properties to the message, including the deviceid of the device that sent the message - for more information about message properties, see https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messages-construct
If you're using C# to read the messages, see the ConnectionDeviceId property of this class: https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.messagesystempropertynames?view=azure-dotnet

Related

Is IoT hub message feedback absolute confirmation?

I'm working Azure Function that sends data to few devices via IoT hub. I'm trying to log whole process and I am unsure if my current solution is sufficient.
So far I'm using message feedback(as mentioned in documentation) to log if device received send message.
"The IoT hub doesn't generate a feedback message. If the cloud-to-device message reaches the Completed state, the IoT hub generates a feedback message." As I understand it if I receive said feedback it is confirmation that message was successfully/unsuccessfully received by device.
Is my understanding that this is absolute confirmation that message was or wasn't received by device correct? Or is there another option to get is confirmation?
I recommend reading through the section Receive Cloud to Device Delivery feedback for better understanding on this. The section explains how you can set the Acknowledgment feedback option. The Azure IoT Hub provides feedback in both Positive and Negative scenarios.
If you have set the message Ack to full as indicated in the article using the following code commandMessage.Ack = DeliveryAcknowledgement.Full;, you will receive a message in both Completed as well as Dead lettered scenarios (Positive and Negative outcome).
If you are specifically targeting the success messages, you would need to set the acknowledgement to Positive. The feedback you then receive is a confirmation proving that message was successfully received by device.
Hope this helps!
I followed the below steps to send a message to an IoT device using azure function.
Additional answer wrto #LeelaRajesh_Sayana.
Created a IoT hub and added the device
Add the device name and click on save
Select the device you created and click on send message
Enter the message to send
To create a particular condition, we have code it. I have used C# function time trigger to send message IoT Hub messages and added the below condition .
try
{
log.Loginformation($"Message sent : {message}");
}
catch (Exception ex)
{
log.LogError($"Error sending message :{ex.Message});
}
using System;
using Microsoft.Azure.Devices;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace AzureFunctionIoT
{
public static class SendMessageToIoTDevices
{
[FunctionName("SendMessageToIoTDevices")]
public static void Run([TimerTrigger("0 0 0 * * *")]TimerInfo myTimer, ILogger log)
{
string connectionString = Environment.GetEnvironmentVariable("IoTHubConnectionString");
ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
var message = new Microsoft.Azure.Devices.Message(System.Text.Encoding.ASCII.GetBytes("Hello IoT Devices"));
serviceClient.SendAsync("<DeviceId>", message).GetAwaiter().GetResult();
log.LogInformation("Message sent successfully");
}
}
}
Run the messages with Azure IoT Hub (.NET) Code

How to represent C2D message in IoT Plug & Play DTDL definition

I'd like to know how to represent C2D message and External message on IoT Edge module by IoT Plug & Play DTDL definition.
I thought that a Command with a value of "asynchronous" in commandType property is used as C2D message. But when I check the behavior in IoT Central, such a command is handled as a direct method invocation.
Is it possible to represent C2D message by IoT PnP model? If yes, please let me know how to describe that.
regards,
It is a fully managed service which enable secure bidirectional communication between large no of devices and back end.
Simulated Device: connects to your IoThub and receive cloud-to-device messages.
SendCloudToDevice: app Sends a cloud to device message to device app with the help of IOT hub.
To receive cloud-to-device messages from the IoT hub.
Inside visual studio, in the Simulated Device project add given method to Simulated Device class.
private static async void ReceiveC2dAsync()
{
Console.WriteLine("\nReceiving cloud to device messages from service");
while (true)
{
Message receivedMessage = await s_deviceClient.ReceiveAsync();
if (receivedMessage == null) continue;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received message: {0}",
Encoding.ASCII.GetString(receivedMessage.GetBytes()));
Console.ResetColor();
await s_deviceClient.CompleteAsync(receivedMessage);
}
}
Add ReceiveC2dAsync() in main method before console.ReadLine().

Azure Device Provisioning - Setting Device Twin property

I want to use the device provisioning in Azure for my devices.
I'm using the Azure IoT SDK (Java).
I would like to create a new device that have some specific properties inside the JSON of the device twin.
I want to see my new device in Azure portal with a custom JSON for the device twin.
In the ProvisioningTpmSample class (Link GitHub). There is this piece of code:
try
{
deviceClient = DeviceClient.createFromSecurityProvider(iotHubUri, deviceId, securityClientTPMEmulator, IotHubClientProtocol.MQTT);
deviceClient.open();
Message messageToSendFromDeviceToHub = new Message("Whatever message you would like to send");
System.out.println("Sending message from device to IoT Hub...");
deviceClient.sendEventAsync(messageToSendFromDeviceToHub, new IotHubEventCallbackImpl(), null);
}
I find the method setProperty for the Message. It's like
messageToSendFromDeviceToHub.setProperty("test", "test");
Is it possible to defined the desired and required properties of the device twin by this setProperty method?
Thanks a lot

Azure topic and sending message over MQTT

Is it possible to send messages to azure to any custom topic via mqtt. All example that I've found so far work with standard topics
'devices/' + hubName + '/messages/events/'
'devices/' + hubName + '/messages/devicebound/#'
Btw, I tried to send/subscribe on the topic/subscription above with MQTT.fx application and never received messages, although the connection was successful.
The topic filter of devices/{device_id}/messages/devicebound/# is to subscribe receiving message from IoT Hub(Cloud-To-Device message), it think you need to understand the difference about device-to-cloud message and cloud-to-device message from here.
The topic of devices/{device_id}/messages/events/ is for sending a device-to-cloud message from device.
You can use Device Explorer to test this issue. When you send a message to device with the tool, MQTT.fx will receive the message.

Retrieve properties of Service Bus message in Node.js Azure Function

I use Azure Service Bus to communicate IoT Hub with Node.js Functions. On Function side, I need to have access to message body as well as custom properties.
By retrieving messages elsewhere, I have noticed that my message in Service Bus consists of:
Body (the string set on IoT Device side).
Custom properties (include properties set on IoT Device side, IoT Device ID, and some additional metadata).
I have found online that in C# it is possible to access these custom properties using BrokeredMessage object. However, there is no mention on how to make this happen in Node.js. To give some details, I am printing the message as soon as it arrives to Function, using the following code:
module.exports = function(context, message) {
context.log('Message', message, 'of type', (typeof message));
...
}
What I am getting in the log console is:
message { test: true } of type object
Where "{ test: true }" is the content of a message set by IoT Device. No trace of properties though...
Is there some formal way or at least a trick to receive and extract these properties?
Thank you!
Browsing resources mentioned in the link posted by Ling Toh in a comment I got inspired to have a look at the context object.
Apparently, all of the Service Bus custom properties are available in context.bindingData.properties object.
In my case:
properties:
{
type: 'sometype', // <- this is the property I have set manually in IoT Hub message
'iothub-connection-device-id': 'mydeviceid',
'iothub-connection-auth-method': '{"scope":"somescope","type":"sometype","issuer":"external","acceptingIpFilterRule":null}',
'iothub-connection-auth-generation-id': 'someid' // <- These are added by IoT Hub
}

Resources