Permalink
f737d98 Nov 15, 2017
241 lines (154 sloc) 10.2 KB
title description services documentationcenter author manager tags keywords ms.assetid ms.service ms.devlang ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
Use an IoT gateway to connect a device to Azure IoT Hub | Microsoft Docs
Learn how to use Intel NUC as an IoT gateway to connect a TI SensorTag and send sensor data to Azure IoT Hub in the cloud.
iot-hub
shizn
timlt
iot gateway connect device to cloud
cb851648-018c-4a7e-860f-b62ed3b493a5
iot-hub
c
article
na
na
06/25/2017
xshi

Use IoT gateway to connect things to the cloud - SensorTag to Azure IoT Hub

[!NOTE] Before you start this tutorial, make sure you’ve completed Set up Intel NUC as an IoT gateway. In Set up Intel NUC as an IoT gateway, you set up the Intel NUC device as an IoT gateway.

What you will learn

You learn how to use an IoT gateway to connect a Texas Instruments SensorTag (CC2650STK) to Azure IoT Hub. The IoT gateway sends temperature and humidity data collected from the SensorTag to Azure IoT Hub.

What you will do

  • Create an IoT hub.
  • Register a device in the IoT hub for the SensorTag.
  • Enable the connection between the IoT gateway and the SensorTag.
  • Run a BLE sample application to send SensorTag data to your IoT hub.

What you need

  • Tutorial Set up Intel NUC as an IoT gateway completed in which you set up Intel NUC as an IoT gateway.
  • An SSH client that runs on your host computer. PuTTY is recommended on Windows. Linux and macOS already come with an SSH client.
  • The IP address and the username and password to access the gateway from the SSH client.
  • An Internet connection.

Create an IoT hub

  1. Sign in to the Azure portal.

  2. Select New > Internet of Things > IoT Hub.

    Azure portal Jumpbar

  3. In the IoT hub pane, enter the following information for your IoT hub:

    • Name: Create a name for your IoT hub. If the name you enter is valid, a green check mark appears.

[!IMPORTANT] The IoT hub will be publicly discoverable as a DNS endpoint, so make sure to avoid any sensitive information while naming it.

  • Pricing and scale tier: For this tutorial, select the F1 - Free tier. For more information, see the Pricing and scale tier.

  • Resource group: Create a resource group to host the IoT hub or use an existing one. For more information, see Use resource groups to manage your Azure resources

  • Location: Select the closest location to you.

  • Pin to dashboard: Check this option for easy access to your IoT hub from the dashboard.

![IoT hub window][2]
  1. Click Create. Your IoT hub might take a few minutes to create. You can monitor the progress in the Notifications pane.

Now that you have created an IoT hub, locate the important information that you use to connect devices and applications to your IoT hub.

  1. After your IoT hub is created, click it on the dashboard. Make a note of the Hostname, and then click Shared access policies.

    Get the hostname of your IoT hub

  2. In the Shared access policies pane, click the iothubowner policy, and then copy and make a note of the Connection string of your IoT hub. For more information, see Control access to IoT Hub.

[!NOTE] You will not need this iothubowner connection string for this set-up tutorial. However, you may need it for some of the tutorials on different IoT scenarios after you complete this set-up.

Get your IoT hub connection string

Register a device in the IoT hub for your device

  1. In the Azure portal, open your IoT hub.

  2. Click Device Explorer.

  3. In the Device Explorer pane, click Add to add a device to your IoT hub. Then do the following:

    Device ID: Enter the ID of the new device. Device IDs are case sensitive.

    Authentication Type: Select Symmetric Key.

    Auto Generate Keys: Select this check box.

    Connect device to IoT Hub: Click Enable.

    Add a device in the Device Explorer of your IoT hub

[!IMPORTANT] The device ID may be visible in the logs collected for customer support and troubleshooting, so make sure to avoid any sensitive information while naming it.

  1. Click Save.

  2. After the device is created, open the device in the Device Explorer pane.

  3. Make a note of the primary key of the connection string.

    Get the device connection string

[!NOTE] Here you register this new device for your SensorTag

Enable the connection between the IoT gateway and the SensorTag

In this section, you perform the following tasks:

  • Get the MAC address of the SensorTag for Bluetooth connection.
  • Initiate a Bluetooth connection from the IoT gateway to the SensorTag.

Get the MAC address of the SensorTag for Bluetooth connection

  1. On the host computer, run the SSH client and connect to the IoT gateway.

  2. Unblock Bluetooth by running the following command:

    sudo rfkill unblock bluetooth
  3. Start the Bluetooth service on the IoT gateway and enter a Bluetooth shell to configure Bluetooth by running the following commands:

    sudo systemctl start bluetooth
    bluetoothctl
  4. Power on the Bluetooth controller by running the following command at the Bluetooth shell:

    power on

    power on the Bluetooth controller on the IoT gateway with bluetoothctl

  5. Start scanning for nearby Bluetooth devices by running the following command:

    scan on

    Scan nearby Bluetooth devices with bluetoothctl

  6. Press the pairing button on the SensorTag. The green LED on the SensorTag flashes.

  7. At the Bluetooth shell, you should see the SensorTag is found. Make a note of the MAC address of the SensorTag. In this example, the MAC address of the SensorTag is 24:71:89:C0:7F:82.

  8. Turn off the scan by running the following command:

    scan off

    Stop scanning nearby Bluetooth devices with bluetoothctl

Initiate a Bluetooth connection from the IoT gateway to the SensorTag

  1. Connect to the SensorTag by running the following command:

    connect <MAC address>

    Connect to the SensorTag with bluetoothctl

  2. Disconnect from the SensorTag and exit the Bluetooth shell by running the following commands:

    disconnect
    exit

    Disconnect from the SensorTag with bluetoothctl

You've successfully enabled the connection between the SensorTag and the IoT gateway.

Run a BLE sample application to send SensorTag data to your IoT hub

The Bluetooth Low Energy (BLE) sample application is provided by Azure IoT Edge. The sample application collects data from BLE connection and send the data to you IoT hub. To run the sample application, you need to:

  1. Configure the sample application.
  2. Run the sample application on the IoT gateway.

Configure the sample application

  1. Go to the folder of the sample application by running the following command:

    cd /usr/share/azureiotgatewaysdk/samples/ble_gateway
  2. Open the configuration file by running the following command:

    vi ble_gateway.json
  3. In the configuration file, fill in the following values:

    IoTHubName: The name of your IoT hub.

    IoTHubSuffix: Get IoTHubSuffix from the primary key of the device connection string that you noted down. Ensure that you get the primary key of the device connection string, not the primary key of your IoT hub connection string. The primary key of the device connection string is in the format of HostName=IOTHUBNAME.IOTHUBSUFFIX;DeviceId=DEVICEID;SharedAccessKey=SHAREDACCESSKEY.

    Transport: The default value is amqp. This value shows the protocol during transpotation. It could be http, amqp, or mqtt.

    macAddress: The MAC address of the SensorTag that you noted down.

    deviceID: ID of the device that you created in your IoT hub.

    deviceKey: The primary key of the device connection string.

    Complete the config file of the BLE sample application

  4. Press ESC and type :wq to save the file.

Run the sample application

  1. Make sure the SensorTag is powered on.

  2. Run the following command:

    ./ble_gateway ble_gateway.json

Next steps

Use IoT gateway for sensor data transformation with Azure IoT Edge