AgroHack

A hands on workshop for an agrotech hackathon 🌽

View the Project on GitHub jimbobbennett/AgroHack

Export IoT Telemetry data to Azure Event Hubs

In the previous step, you wrote the code to capture telemetry from the Raspberry Pi. In this step, you will export IoT telemetry to Azure Event Hubs.

Azure Event Hubs

Azure Event Hubs allow you to take streaming data and connect it to other services, such as storing the data or using Azure Stream Analytics to analyze the data in real time. Azure IoT Central can be configured to stream data to an Azure Event Hubs instance.

To export data you will need an Azure account. If you don’t have one, head to the Azure Account instructions to create an account.

Creating an Azure Event Hubs instance

There are two ways to create an Azure Event Hubs instance - from the Azure Portal or the Azure CLI.

The instance consists of a namespace, containing an event hub.

Creating an Azure Event Hubs instance using the Azure Portal

Create the namespace
  1. Open the Azure Portal

  2. Log in with your Microsoft account if required

  3. From the left-hand menu select + Create a resource

    The create a resource button

  4. Search for Event Hubs and select Event Hubs

    Searching for event hubs

  5. Select Create

    The create event hubs button

  6. Fill in the details for the Event Hubs

    1. Give the Event Hubs a name. This needs to be globally unique, so include things such as the data or your name, for example agrohackjim2020. To make it easier, name it the same as your Azure IoT Central app.

    2. Leave the Pricing Tier as Standard

    3. Select your Azure subscription

    4. For the Resource group, select Create new and name it AgroHack, then select OK

      Creating a new resource group

      Resource groups are logical groupings of Azure services, allowing you to manage all the services for a particular application or project together. At the end of this workshop this Resource Group will be deleted, deleting all the services created.

    5. Select a Location closest to you

    6. Leave the rest of the options as the defaults

    The event hubs namespace settings

  7. Select Review + create

  8. Select Create

  9. Once the deployment has completed, select the Go to resource button.

Create the event hub
  1. From the Event Hub Namespace in the Azure Portal, select + Event Hub

    New event hub button

  2. Name the Event Hub Telemetry

  3. Leave the rest of the options as the defaults

  4. Select Create

Creating an Azure Event Hubs Namespace using the Azure CLI

  1. To install the Azure CLI, head to the installation guide and follow the instructions for your OS.

  2. From your terminal, log in to the Azure CLI using the following command

    az login
    

    A web browser window will be launched to allow you to log in to your Azure subscription

  3. If you have multiple Azure subscriptions, you will need to select the one you want to use

    1. List all the subscriptions using the following command

      az account list --output table
      
    2. Find the subscription id of the subscription you want to use and copy it

    3. Set the active subscription using the following command

      az account set --subscription <subscription id>
      

      For the <subscription id>, use the id you copied in the previous step

Create a resource group
  1. Run the following command to get a list of Azure locations

    az account list-locations --output table
    

    Note the name of the location closest to you

  2. Create a new Resource Group with the following command

    az group create --name AgroHack --location <location>
    

    For the <location>, use the name of the location closest to you.

    This will create a Resource Group called AgroHack in the location you specify.

    Resource groups are logical groupings of Azure services, allowing you to manage all the services for a particular application or project together. At the end of this workshop this Resource Group will be deleted, deleting all the services created.

Create the namespace
  1. Create the event hubs namespace using the following command

    az eventhubs namespace create
     --location <location>
     --name <account_name>
     --resource-group AgroHack
    

    For the <location>, use the name of the location closest to you, the same location that you used for the resource group.

    For the <account_name>, pick a name that is globally unique, so include things such as the date or your name, for example agrohackjim2020. To make it easier, name it the same as your Azure IoT Central app.

    The --resource-group AgroHack setting adds this event hubs namespace to the new Resource Group you created in the previous step.

Create the event hub
  1. Create the event hub inside the namespace using the following command

    az eventhubs eventhub create
     --name telemetry
     --resource-group AgroHack
     --namespace-name <namespace_name>
    

    The --name telemetry names the event hub as telemetry.

    The --resource-group AgroHack setting adds this event hubs namespace to the new Resource Group you created in the previous step.

    For the <namespace_name>, use the name you used when creating the namespace.

Set up data export

Azure IoT Central can export data to a number of different services, either to route the data to multiple other services, or to store data. For example, it can send IoT messages to Azure Event Hubs, and other services can listen on these events and respond - maybe by running code to process each message.

Create the data export

  1. Open the app in Azure IoT Central

  2. Select Data export from the left-hand menu

    The data export menu

  3. Select the New button, then select the Azure Event Hubs option

    New event hubs export option

  4. Give the export a name, such as Export telemetry

  5. Select the Azure Event Hubs Namespace you just created, along with the Event Hub

  6. In the Data to export section, leave Telemetry turned on, and turn off Devices and Device templates

  7. Select Save

    Configuring the export

  8. The new data export job will appear in the list, showing a status. Wait until the status shows Running, this will only take about 30 seconds or so.

Monitor the data export

The easiest way to see messages flowing to the Event Hub is via the Azure Portal.

  1. Open the Azure Portal

  2. Log in with your Microsoft account if required

  3. If you are not on the blade for the event hub you created, search for it by typing the name of the namespace into the search box at the top of the portal, and selecting the Event Hubs Namespace under the Resources section

    Searching for the event hubs namespace

  4. In the Overview tab you should see message throughput on the graph

    Messages coming into the event hub


In this step you exported IoT telemetry to Azure Event Hubs. In the next step, you will create a storage account to store telemetry data.