1
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-06-16

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 CreateRecord component. A sibling post does the same thing with the SensorDataDb 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

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)

Action Setup

Our action will use the single most recent record from the CreateRecord component.

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

CreateRecord > MQTTPublish

(1) CreateRecord

The create record component is an unusual component because it has no properties. Its use is to create and propagate a record to another component. You can think of it as a plumbing component

Step Variable Creation

We'll send over a subset of the data recorded by the sensor:

  1. The actual Sensor Data (av.Data)
  2. the area the sensor is located in (av.AreaName)
  3. the timestamp of the recording (av.Timestamp)

Add Step Variables to CreateRecord

Create the following variables under Step Results:

Output Field Name Type Expression Formula
Area String av.AreaName
Data Int av.Data
Timestamp Datetime av.Timestamp

For Data, you can set the Type to Int if you want the mqtt service to record sensor data as numeric.

Set Data to type int

After you finish, you should have 3 Step Variables that look like this:

Completed Step Variables for the CreateRecord Component

That's all we need to do to set up the SendRecord component.

(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 gravio-iot-demo.azure-devices.net
Broker Port 8883
Client ID Device Id TemperatureSensor
MQTT Username "<IoT Hub Hostname>/<Device ID>/api-version=2019-03-31" "gravio-iot-demo.azure-devices.net/TemperatureSensor/api-version=2019-03-31"
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

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

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

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

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

Enable the Trigger

Make sure to enable the trigger after creating it:

Trigger Enabling

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
0
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
0