1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Sending Data to Azure IoT Hub via MQTT with Gravio (SensorDataDb)

Last updated at Posted at 2020-06-15

In this post, we will explore using Gravio to publish temperature sensor data to Azure IoT Hub via the MQTT protocol.

This post will use the SensorDataDb component. A sibling post does the same thing with the CreateRecord component.

Data flow from our sensor to Azure

Prerequisites

Hardware

Windows 10 (minimum version 1809)
Gravio Client Sensor (e.g., Temperature Sensor)

Software

Gravio

  • Gravio Studio (minimum version 3.8)
  • Gravio Server (minimum version 3.8)
    Azure IoT Explorer
  • This tutorial is using version 0.10.18 (Preview)

Azure IoT Hub Setup

Resource Creation

First we need to create an IoT Hub Resource in Azure.
Create an IoT Hub Resource

Resource Assignment

We need to fill in some required information. You can consult the following table for the values we will use:

Item Value
Subscription Pay as you go
Resource Group Your choice of group. If none, create a new one
Region Your choice of region. This tutorial will use East US
IoT Hub Name Your choice of name. This tutorial will use gravio-demo.

Azure IoT Hub Resources

Network Settings

Next we will select the network settings. Make sure that Public Endpoint is selected.
Azure IoT Hub Network Settings

Size and Scale

Select F1: Free Tier
Azure IoT Hub Size and Scale Settings

Tags

Any tags you want can be added here. This is an optional step. We're using GravioDemo:TemperatureSensorDemo.
Azure IoT Hub Tags Settings

Review and Create

Review the settings before creating our new IoT Hub.
Azure IoT Hub Review

Deployment in Progress

After clicking Create, you may have to wait a few minutes while Azure provisions your new resource:
Azure IoT Hub Deployment in Progress

Deployment Complete

Once the deployent finishes, click on Go to resource
Azure IoT Hub Deployment Complete

Device Assignment

Click on IoT Devices, then + New to create a new IoT Device linked to this IoT Hub:

Azure Device Creation

Device Creation

The choice of name for your new device is up to you. We will use TemperatureSensor. Leave the default choices as is:

Item Value
Authentication Type Symmetric key
Auto-Generate Keys Checked
Connect this device to an IoT Hub Enable

Azure Device Access Settings

Access Settings

From the side menu, select Shared access policies, click iothubowner, and copy the Connection string - primary key field:

Azure IoT Hub Connection String

We'll use this connection string in the following section.

Azure IoT Explorer

Connect to the IoT Hub

Using the Azure IoT Explorer, paste in the connection string from earlier, then connect to the IoT Hub.
Explorer Connection

Select the IoT Device

Select the IoT Device we made earlier.
Explorer Device Selection

SAS Token Acquisition

Select Device Identity and scroll down to Connection string with SAS Token. We'll use the following settings:

Item Value
Symmetric Key Primary key
Expiration (minutes) Your choice. Default is 5, this tutorial will use 60.

Then click Generate.

Explorer SAS Token Generation

The generated key will look like this.

HostName=gravio-iot-demo.azure-devices.net;DeviceId=TemperatureSensor;SharedAccessSignature=
SharedAccessSignature sr=gravio-iot-demo.azure-devices.net%2Fdevices%2FTemperatureSensor&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&se=1591932352```


The important part is the highlighted section:
`SharedAccessSignature sr=gravio-iot-demo.azure-devices.net%2Fdevices%2FTemperatureSensor&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&se=1591932352`

This is the actual SAS Token. Keep this token in mind for the next section.

**Be aware that these keys have a limited lifetime. You will need to regenerate them frequently!**
## Live Monitoring of Data
Click the `Telemetry` tab, then `Start` to begin monitoring data sent to our IoT Device. In the next section, we will set up Gravio to send data to this device.
![Explorer Telemetry View](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/1733d7e5-9d90-e0e1-0741-bf31def0ebd8.png)

# Gravio Setup
Now we can begin to use Gravio to send data over to Azure.

## Sensor Setup
For pairing and setting up the sensor in Gravio, please refer to this article:
[Setting up a temperature sensor to send data to Excel with Gravio (Japanese)](https://qiita.com/mikawan/items/3d93543566680afda868#step1%E3%82%BB%E3%83%B3%E3%82%B5%E3%83%BC%E3%81%AE%E8%A8%AD%E5%AE%9A)

## Action Setup
Our action will use the single most recent record from the SensorDataDb component.

On the Action Creation screen, we'll use the following component flow:

SensorDataDb > MQTTPublish

### (1) SensorDataDb
#### Property Setup
We'll use the following settings for our SensorDataDb component:

|Property|Value|
|-----|-----|
|Area Index|All|
|Layer Index|All|
|Device Index|All|
|From Previous|1|
|Timescale|Records|
|Sender Id*||
|Get All Records|Unchecked|
|Enable Time Rounding|Checked|
\* Leave `Sender Id` blank

![Gravio SensorData Creation](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/6292998b-d181-e3fa-cafc-779dcd5cdd1a.png)

#### Step Results
Next we set up the data we want to supply to MQTTPublish Component.

Click the Arrow button to copy over the variables to `Step Results`.

![Gravio SensorData Step Values](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/27ed2e19-b464-bd3b-d5c2-5647ee864e25.png)

### (2) MQTTPublish
#### Property Setup
We'll use the following settings for our MQTT Publish component:

NB: The quotes around some fields (`""`) are not optional and must be included.

|Property|Description|Example|
|--------|-----------|-----|
|Broker Hostname|IoT Hub Hostname|<span>gravio-iot-demo.azure-devices.net</span>|
|Broker Port||8883|
|Client ID|Device Id|TemperatureSensor|
|MQTT Username|"\<IoT Hub Hostname>/\<Device ID>/api-version=2019-03-31"|"<span>gravio-iot-demo.azure-devices.net/TemperatureSensor/api-version=2019-03-31</span>"|
|MQTT Password|The generated SAS Token|SharedAccessSignature sr=gravio-iot-demo.azure-devices.net%2Fdevices%2FTemperatureSensor&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&se=1591932352|
|Topic|"devices/\<Device ID>/messages/events"|"devices/TemperatureSensor/messages/events"|
|Data Format||JSON|
|QoS|Anything below QoS2|QoS 1 (At most once)|
|Secure Connection||Checked|
|Client Certificate File*|||
|Client Certificate Password*|||
|CA Certificate File*|||
\* Leave blank


After setting up the properties, the component screen should look like this:

![Gravio MQTT Creation](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/b9df92d8-8bc9-5ddb-b20e-afb53e52f766.png)


#### Step Results
Because this is an Output type component, we must set the data to send to the next component. In our case, there is no next component. Nevertheless, this step is still required.

This time, instead of copying from `Step Variables`, we'll copy from `Step InFields`:
![Gravio MQTT Step Results](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/db270ce8-8e1a-7762-835c-df1afa1a471e.png)


## Running the Action
Click the Run button to run the component. If things were set up properly, we should have a successfully run action:

![Gravio Action Results](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/31bf8900-43a7-0cdd-57b7-038676a9dc83.png)


# Viewing the Data
Going back to the Telemetry view of the Azure IoT Explorer, we can see the data that was sent to it:

![Explorer Result Viewing](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/28c11970-cd2e-4d30-14b4-0d168652a876.png)

# Automatic Trigger Settings
## Settings
We'll set up a Trigger to send sensor data to our MQTT service whenever new data is received by the temperature sensor.

Here's the basic settings we'll use:

|Property|Value|
|-----|-----|
|Area|Demo*|
|Layer|Temperature*|
|Action Name|MQTT Test*|
|Interval|5|
|Use Threshold Trigger|Unchecked|
|Temperture|Any 0|
\* Use whatever area and layer has your Temperature sensor, as well the name of whatever action you created earlier.

![Trigger Property Setup](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/cdcb9be8-08bb-2283-22fd-a46a4ce52340.png)

## Enable the Trigger

Make sure to enable the trigger after creating it:

![Trigger Enabling](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/597474/cc005098-a274-8e1b-4ab7-730bd9e45440.png)


# Trigger Confirmation

Using the Azure IoT Explorer's telemetry screen from before, you can see the the data being sent to the IoT Hub.

The trigger is configured to send all data it records to the MQTT service.  

# Conclusion

Gravio makes it easy to send data to services like Microsoft Azure, even for relative newcomers to both technologies. 

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?