📜 ⬆️ ⬇️

Cross-platform IoT: Device Operations

Hi, Habr! IoT Hub Explorer is a cross-platform node.js based device management tool in the used IoT Hub that can work on Windows, Mac, or Linux. Today we will talk about it in the framework of diagnostics and improvement of IoT Hub Azure. Look under the cat for details!



Cycle of articles "Cross-platform IoT"


1. Cross-platform IoT: Using Azure CLI and Azure IoT Hub
2. Cross-platform IoT: Device Operations
3. Cross-platform IoT: Troubleshooting
3. Loading ...

IoT Hub Explorer makes it easy to perform such operations in the IoT Hub, such as: creating a device, sending messages to an existing device, etc. When testing development or even demonstrating the capabilities of Azure Internet of Things, this function turns out to be especially useful. Theoretically, it can be used to diagnose the working environment. However, to perform these scenarios, we need suitable telemetry data, as well as an operational control channel. One of my favorite features of the tool is monitoring messages about IoT Hub devices, receiving event data and statistics on center device operations. We will use this tool to create a device and view IoT Hub messages. IoT Hub Explorer is available on GitHub here .
')
It should be noted that the Azure IoT CLI, to which the previous publication was devoted, also supports device management and its functionality will partially overlap with the capabilities of the IoT Hub Explorer. If this happens, the Azure CLI will be considered the main tool for working with all IoT Hub operations.

Let's use the IoT Hub browser to create and monitor a device. Before you do this, you must install it. Since this is a node package, it can be installed using npm.

npm install -g iothub-explorer 

Since IoT Hub Explorer is a separate program, we need to first log in using the connection string of our IoT Hub. Open a bash terminal and enter the following:

 iothub-explorer login "HostName=yourhub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=yourkey" 

If you don’t have the connection string at hand, you can enter the az iot hub show-connection-string -g youresourcegroup command, and get the connection string of your IoT Hub. The authorization team should open a temporary session with a fixed policy of access rights to the IoT center. By default, the life of this session is 1 hour.

 Session started, expires on Wed Mar 15 2017 19:59:05 GMT-0500 (CDT) Session file: /Users/niksac/Library/Application Support/iothub-explorer/config 

Note that the above command uses the connection string for the iothubowner policy, which provides full control of your IoT hub.

Create a new device


To create a new device using IoT Hub Explorer, enter the following command:

 iothub-explorer create -a 

The -a symbol is used to automatically generate Id and device credentials when it is created. You can also specify your Id device yourself or add a device JSON file to customize the process of its creation. There are other ways to specify credentials, such as a symmetric key and X.509 certificates. We will publish a separate article on the security of IoT Hub, in which we consider these methods. We currently use standard credentials generated by IoT Hub.

If everything went well, you should see the answer as follows:

 deviceId: youdeviceId generationId: 63624558311459675 connectionState: Disconnected status: enabled statusReason: null connectionStateUpdatedTime: 0001-01-01T00:00:00 statusUpdatedTime: 0001-01-01T00:00:00 lastActivityTime: 0001-01-01T00:00:00 cloudToDeviceMessageCount: 0 authentication: symmetricKey: primaryKey: symmetrickey1= secondaryKey: symmetrickey2= x509Thumprint: primaryThumbprint: null secondaryThumbprint: null connectionString: HostName=youriothub.azure-devices.net;DeviceId=youdeviceId;SharedAccessKey=symmetrickey= 

There are several important things here, and one of them, obviously, is the connectionString . It provides a unique device connection string and allows you to communicate with it. The privileges for the device connection string are based on the policy defined for the device in the IoT Center, the rights are limited only by the DeviceConnect function. Policy based access protects our endpoints and limits the scope of use to a particular device. Learn more about the security of the IoT Hub device here. Also note that the device is activated and the status is disabled. This means that the device was successfully registered in the IoT Center, but it does not have active connections.

Sending and receiving messages


Let's initiate a connection by sending a request to receive a device. There are several ways to send and receive messages in IoT Hub. One of the effective options is the simulate-device command. The simulate-device command allows the tool to act as a device command simulator and device receive simulator. This can be used to send user-defined telemetry messages or commands on behalf of the device. The convenience of this functionality is manifested when testing the integrity of the developments on your device, as this will reduce the amount of code. You can simultaneously create messages and track the send / receive stream. The command also provides features such as send-interval, send-count and receive-count , which allow you to configure simulations. It is worth considering that this is not a tool to test the load or penetration, it can be used to conduct initial tests, anticipating more in-depth tests. Let's send a set of messages to the device we created (from part 1) and then accept the message with the command.

Posting a message


The following command sends 5 messages every 2 minutes to a device with a specific Id.

 niksac$ iothub-explorer simulate-device --send "Hello from IoT Hub Explorer" --device-connection-string "HostName=youriothubname.azure-devices.net;DeviceId=D1234;SharedAccessKey==" --send-count 5 --send-interval 2000 

The final message will look like this:

 Message #0 sent successfully Message #1 sent successfully Message #2 sent successfully Message #3 sent successfully Message #4 sent successfully Device simulation finished. 

Message monitoring


Another useful feature of IoT Hub Explorer is the ability to monitor the events of your device or the IoT Hub as a whole. This is very convenient if you want to diagnose an instance of your IoT Hub. For example, you want to check the correctness of message delivery in the IoT Hub. You can use the monitor-events command to log all device-related events in the terminal; You can also use the monitor-ops command to track the endpoint of operations in the IoT center.

To monitor events, enter the following:

 iothub-explorer monitor-events --login "HostName=youriothub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey==" 

As a result, a listener is created that captures activity in the entire IoT center. As noted earlier, you can specify a device connection string to monitor a specific device.

Now, when sending a message or command to any device of your IoT Hub, the final result will be displayed in the terminal. For example, if you open the monitor-event listener in a terminal window and then rerun the simulate-device --send , the following result should appear in the terminal:

 Monitoring events from all devices... ==== From: D1234 ==== Hello from IoT Hub Explorer ==================== ==== From: D1234 ==== Hello from IoT Hub Explorer ==================== ==== From: D1234 ==== Hello from IoT Hub Explorer ==================== ==== From: D1234 ==== Hello from IoT Hub Explorer ==================== ==== From: D1234 ==== Hello from IoT Hub Explorer ==================== 

IoT Hub Explorer has many other commands available, such as: import / export devices, re-create access rights to SAS, device management commands. You need to try out the various options and commands of IoT Hub Explorer; This will help you avoid writing code for standard operations.

Source: https://habr.com/ru/post/343802/


All Articles