Azure IoT Hub Operations Monitoring

0 votes

I have an Azure IoT hub that I connect devices to. I want to enable monitoring to monitor devices connecting and disconnecting from the hub.

I've enabled Verbose on Connections in the monitoring categories for my Iot hub:

enter image description here

My devices connect to my Hub and show in Device Explorer:

enter image description here

I then have an Azure Function set to log my data from the Operations Monitoring to an Azure SQL db:

using System;
using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;

public static void Run(string myEventHubMessage, TraceWriter log)
{
    log.Info(myEventHubMessage);

    try
    {
        var connectionString = ConfigurationManager.ConnectionStrings["iotAzureSQLDb"].ConnectionString;

        log.Info(connectionString);
        var message = JsonConvert.DeserializeObject<MonitoringMessage>(myEventHubMessage);

        using (var connection = new SqlConnection(connectionString))
        {
            var sqlStatement = "insert into [dbo].[DeviceStatuses] " +
                                "(DeviceId, ConnectionStatus, ConnectionUpdateTime)" +
                                "values " +
                                "(@DeviceId, @ConnectionStatus, @ConnectionUpdateTime)";
            using (var dataCommand = new SqlCommand(sqlStatement, connection))
            {
                dataCommand.Parameters.AddWithValue("@ConnectionStatus", message.operationName);
                dataCommand.Parameters.AddWithValue("@DeviceId", message.deviceId);
                dataCommand.Parameters.AddWithValue("@ConnectionUpdateTime", message.time);

                connection.Open();
                dataCommand.ExecuteNonQuery();
                connection.Close();

                log.Info($"Device {message.deviceId} changed state: {message.operationName}");
            }
        }
    }
    catch (Exception ex)
    {
        log.Info(ex.Message);
    }
}

public class MonitoringMessage
{
    public string deviceId { get; set; }
    public string operationName { get; set; }
    public int? durationMs { get; set; }
    public string authType { get; set; }
    public string protocol { get; set; }
    public DateTimeOffset? time { get; set; }
    public string category { get; set; }
    public string level { get; set; }
    public int? statusCode { get; set; }
    public int? statusType { get; set; }
    public string statusDescription { get; set; }
}

If I enable Device Identity Operations in Operations Monitoring, I get create events being logged. So I'm confident the inputs to the function is correct. However, nothing is ever created for Connections???

I can also send messages to my connected devices fine. I'm just seeing no events for connections / disconnections.

I've also tried creating a Stream Analytics and sampling the input for a period where I know I have connections / disconnections and nothing is being found.

Aug 4, 2018 in IoT (Internet of Things) by Matt
• 2,270 points
1,681 views

2 answers to this question.

0 votes

If you just would like to get devices connection states, use REST instead.

https://docs.microsoft.com/en-us/rest/api/iothub/deviceapi#DeviceApi_GetDevices

answered Aug 4, 2018 by anonymous2
• 4,240 points
0 votes

Hi, Can you tell which device you are using? 

I have recently worked with ESP32 device to send the wireless temperature sensor data to Azure stream analytics using this blog which might help you to resolve your issue in this case 

answered Jan 4, 2019 by Varul
• 140 points
Thanks Varul. I used to have this issue. Let me check and get back to you.

Related Questions In IoT (Internet of Things)

0 votes
1 answer
0 votes
1 answer

Certificate issue in Azure IoT Hub

I've fixed this problem: The configured CA certificate ...READ MORE

answered Oct 11, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,492 views
0 votes
1 answer

TCP in Azure IoT Hub

The default Protocol Gateway samples are indeed ...READ MORE

answered Oct 11, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
2,276 views
0 votes
1 answer

Azure IoT Hub : Sending messages using Python via mqtt

The thing is that IoT Hub is not ...READ MORE

answered Jan 4, 2019 in IoT (Internet of Things) by Upasana
• 8,620 points
3,810 views
0 votes
1 answer

How to use the data I receive from Azure IoT Hub?

The payload you receive will be a ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
1,808 views
0 votes
1 answer

Testing if messages from my Device is reaching the Azure IoT Hub?

Download the Device Explorer open source tool ...READ MORE

answered Aug 10, 2018 in IoT (Internet of Things) by DataKing99
• 8,250 points
3,864 views
0 votes
1 answer

Error when connecting Azure IoT Suite with Raspberry Pi

down vote I've verified the tutorial works on ...READ MORE

answered Sep 18, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
902 views
0 votes
1 answer

Displaying Table Schema using Power BI with Azure IoT Hub

Answering your first question, Event Hubs are ...READ MORE

answered Aug 1, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,310 views
0 votes
1 answer
0 votes
1 answer

Control LED Blinking Using Java in Azure IoT Hub

Your description of what you did is ...READ MORE

answered Sep 5, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
980 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP