A Device Online Notification Scheme for Century Interconnected Azure IoT Hub

Keywords: SDK JSON Database REST

This paper describes a scheme for getting device online notifications for Century Interconnected Azure IoT Hub.

 

Video Introduction:

You can watch the video introduction at Station B: https://www.bilibili.com/video/BV1dp4y1X7X3/

Or watch it on the author's blog: https://www.51azure.cloud/post/2020/4/30/azure-iot-hub-device-on-line-report 

 

 

Illustration:

 

For Global Azure IoT Hub, the online and offline events for devices on the Internet of Things can be obtained through an integrated binding with Event Grid, and real-time notifications can be made to their business systems through other paas services such as logical applications or Azure Function.

However, as of April 2020, Event Subscription is not supported by Azure, which is operated by Century Interconnected. This paper demonstrates a solution to get devices online in Azure IoT Hub, which operates in Century Interconnected. The solution is open, and the components are replaceable, and can be adjusted according to the actual business architecture.

 

This scheme mainly utilizes the message routing function to send messages with specific properties when the device is online, such as setting message.properties.add('messageType'),'onlinereport'), once routed according to this message property, device online notifications can be sent to a specific queue or topic, and the results can then be written to a specific database or invoked through the SDK of Functions or Logic APP or Service Bus.

 

 

 

Key steps:

Create Service Bus and Queue:

Enter the name of the resource. In this case, select the standard level, select the area, and click Review+Create:

 

Click Create:

 

 

Create Finish Click Go to Resources

 

Click the Add Queue button to create a queue to store device online messages:

 

Create Queue Enter a queue name and leave the rest unchanged in this example

 

 

 

Create the following message route:

 

Add custom endpoints to Azure IoT Hub message routing:

Enter a name and select the Service Bus and queue you just created:

 

Click Add Route:

 

Enter a name, select the endpoint you just created in the drop-down list, and enter the routing criteria:

messageType="onlinereport"

 

Modify the sample code (node js):

For downloading and using the Node.js sample code, see the following article:

  1. (Video) Azure IoT Hub Manual Training Camp (1) - Pre-conditions (preparation)

  2. (Video) Azure IoT Hub Manual Training Camp (2) - Experiment 1, Send device-to-cloud messages and control devices from the cloud (Node.js Device SDK/Service SDK)

 

'use strict';// Using the Azure CLI:// az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output tablevar connectionString = 'HostName=sean-iot-hub-v.azure-devices.cn;DeviceId=device001;SharedAccessKey=UfXj/60sNrwaCDbqjtcI3FIZsk60bfse1lizLLxuMUM=';var Mqtt = require('azure-iot-device-mqtt').Mqtt;var DeviceClient = require('azure-iot-device').Clientvar Message = require('azure-iot-device').Message;var client = DeviceClient.fromConnectionString(connectionString, Mqtt);// send telemetry message every 10 seconds.setInterval(function(){  // Simulate telemetry.
  var temperature = 20 + (Math.random() * 15);  var message = new Message(JSON.stringify({    temperature: temperature,    humidity: 60 + (Math.random() * 20)
  }));  // Add a custom application property to the message.
  // An IoT hub can filter on these properties without access to the message body.
  message.properties.add('temperatureAlert', (temperature > 30) ? 'true' : 'false');
  message.properties.add('messageType', 'telemetry');  console.log('Sending telemetry message: ' + message.getData());  // Send the message.
  client.sendEvent(message, function (err) {    if (err) {      console.error('send telemetry error: ' + err.toString());
    } else {      console.log('telemetry message sent');
    }
  });
}, 10000);// send heartbeat message every 30 seconds.setInterval(function(){    
  var message = new Message(JSON.stringify({    messageType: 'heartbeat'  
  }));

  message.properties.add('messageType', 'heartbeat');  console.log('Sending heartbeat message: ' + message.getData());  // Send the message.
  client.sendEvent(message, function (err) {    if (err) {      console.error('send heartbeat error: ' + err.toString());
    } else {      console.log('heartbeat message sent');
    }
  });
}, 30000);//send Online report when device startvar message = new Message(JSON.stringify({  messageType: 'onlinereport'  }));

message.properties.add('messageType', 'onlinereport');console.log('Sending onlinereport message: ' + message.getData());// Send the message.client.sendEvent(message, function (err) {  if (err) {    console.error('send onlinereport error: ' + err.toString());
  } else {    console.log('onlinereport message sent');
  }
});

 

Verify the device online message in the queue:

 

View device online notifications in the tool:

 

In real-world environments, take the message out of the message queue and process it using Functions or Logic APP.


Posted by MannyG on Tue, 05 May 2020 03:03:33 -0700