Weather forecast using the sensor data from your IoT hub in Azure Machine Learning

End-to-end diagram

Note

Before you start this tutorial, set up your device. In the article, you set up your Azure IoT device and IoT hub, and you deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.

Machine learning is a technique of data science that helps computers learn from existing data to forecast future behaviors, outcomes, and trends. Azure Machine Learning is a cloud predictive analytics service that makes it possible to quickly create and deploy predictive models as analytics solutions.

What you learn

You learn how to use Azure Machine Learning to do weather forecast (chance of rain) using the temperature and humidity data from your Azure IoT hub. The chance of rain is the output of a prepared weather prediction model. The model is built upon historic data to forecast chance of rain based on temperature and humidity.

What you do

  • Deploy the weather prediction model as a web service.
  • Get your IoT hub ready for data access by adding a consumer group.
  • Create a Stream Analytics job and configure the job to:
    • Read temperature and humidity data from your IoT hub.
    • Call the web service to get the rain chance.
    • Save the result to an Azure blob storage.
  • Use Microsoft Azure Storage Explorer to view the weather forecast.

What you need

  • Tutorial Setup your device completed which covers the following requirements:
    • An active Azure subscription.
    • An Azure IoT hub under your subscription.
    • A client application that sends messages to your Azure IoT hub.
  • An Azure Machine Learning Studio account. (Try Machine Learning Studio for free).

Deploy the weather prediction model as a web service

  1. Go to the weather prediction model page.
  2. Click Open in Studio in Microsoft Azure Machine Learning Studio. Open the weather prediction model page in Cortana Intelligence Gallery
  3. Click Run to validate the steps in the model. This step might take 2 minutes to complete. Open the weather prediction model in Azure Machine Learning Studio
  4. Click SET UP WEB SERVICE > Predictive Web Service. Deploy the weather prediction model in Azure Machine Learning Studio
  5. In the diagram, drag the Web service input module somewhere near the Score Model module.
  6. Connect the Web service input module to the Score Model module. Connect two modules in Azure Machine Learning Studio
  7. Click RUN to validate the steps in the model.
  8. Click DEPLOY WEB SERVICE to deploy the model as a web service.
  9. On the dashboard of the model, download the Excel 2010 or earlier workbook for REQUEST/RESPONSE.

    Note

    Ensure that you download the Excel 2010 or earlier workbook even if you are running a later version of Excel on your computer.

    Download the Excel for the REQUEST RESPONSE endpoint

  10. Open the Excel workbook, make a note of the WEB SERVICE URL and ACCESS KEY.

Add a consumer group to your IoT hub

Consumer groups are used by applications to pull data from Azure IoT Hub. In this tutorial, you create a consumer group to be used by a coming Azure service to read data from your IoT hub.

To add a consumer group to your IoT hub, follow these steps:

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

  2. In the left pane, click Built-in endpoints, select Events on the top pane, and enter a name under Consumer groups on the right pane. Click Save after you change the Default TTL value and return it back to the original value.

    Create a consumer group in your IoT hub

Create, configure, and run a Stream Analytics job

Create a Stream Analytics job

  1. In the Azure portal, click Create a resource > Internet of Things > Stream Analytics job.
  2. Enter the following information for the job.

    Job name: The name of the job. The name must be globally unique.

    Resource group: Use the same resource group that your IoT hub uses.

    Location: Use the same location as your resource group.

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

    Create a Stream Analytics job in Azure

  3. Click Create.

Add an input to the Stream Analytics job

  1. Open the Stream Analytics job.
  2. Under Job Topology, click Inputs.
  3. In the Inputs pane, click Add, and then enter the following information:

    Input alias: The unique alias for the input.

    Source: Select IoT hub.

    Consumer group: Select the consumer group you created.

    Add an input to the Stream Analytics job in Azure

  4. Click Create.

Add an output to the Stream Analytics job

  1. Under Job Topology, click Outputs.
  2. In the Outputs pane, click Add, and then enter the following information:

    Output alias: The unique alias for the output.

    Sink: Select Blob Storage.

    Storage account: The storage account for your blob storage. You can create a storage account or use an existing one.

    Container: The container where the blob is saved. You can create a container or use an existing one.

    Event serialization format: Select CSV.

    Add an output to the Stream Analytics job in Azure

  3. Click Create.

Add a function to the Stream Analytics job to call the web service you deployed

  1. Under Job Topology, click Functions > Add.
  2. Enter the following information:

    Function Alias: Enter machinelearning.

    Function Type: Select Azure ML.

    Import option: Select Import from a different subscription.

    URL: Enter the WEB SERVICE URL that you noted down from the Excel workbook.

    Key: Enter the ACCESS KEY that you noted down from the Excel workbook.

    Add a function to the Stream Analytics job in Azure

  3. Click Create.

Configure the query of the Stream Analytics job

  1. Under Job Topology, click Query.
  2. Replace the existing code with the following code:

    WITH machinelearning AS (
       SELECT EventEnqueuedUtcTime, temperature, humidity, machinelearning(temperature, humidity) as result from [YourInputAlias]
    )
    Select System.Timestamp time, CAST (result.[temperature] AS FLOAT) AS temperature, CAST (result.[humidity] AS FLOAT) AS humidity, CAST (result.[Scored Probabilities] AS FLOAT ) AS 'probabalities of rain'
    Into [YourOutputAlias]
    From machinelearning
    

    Replace [YourInputAlias] with the input alias of the job.

    Replace [YourOutputAlias] with the output alias of the job.

  3. Click Save.

Run the Stream Analytics job

In the Stream Analytics job, click Start > Now > Start. Once the job successfully starts, the job status changes from Stopped to Running.

Run the Stream Analytics job

Use Microsoft Azure Storage Explorer to view the weather forecast

Run the client application to start collecting and sending temperature and humidity data to your IoT hub. For each message that your IoT hub receives, the Stream Analytics job calls the weather forecast web service to produce the chance of rain. The result is then saved to your Azure blob storage. Azure Storage Explorer is a tool that you can use to view the result.

  1. Download and install Microsoft Azure Storage Explorer.
  2. Open Azure Storage Explorer.
  3. Sign in to your Azure account.
  4. Select your subscription.
  5. Click your subscription > Storage Accounts > your storage account > Blob Containers > your container.
  6. Open a .csv file to see the result. The last column records the chance of rain.

    Get weather forecast result with Azure Machine Learning

Summary

You’ve successfully used Azure Machine Learning to produce the chance of rain based on the temperature and humidity data that your IoT hub receives.

To continue to get started with Azure IoT Hub and to explore other IoT scenarios, see the following: