Weather forecast using the sensor data from your IoT hub in Azure Machine Learning
Before you start this tutorial, complete the Raspberry Pi online simulator tutorial or one of the device tutorials; for example, Raspberry Pi with node.js. In these articles, 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
- Complete the Raspberry Pi online simulator tutorial or one of the device tutorials; for example, Raspberry Pi with node.js. These cover 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
Go to the weather prediction model page.
Click Open in Studio in Microsoft Azure Machine Learning Studio.
Click Run to validate the steps in the model. This step might take 2 minutes to complete.
Click SET UP WEB SERVICE > Predictive Web Service.
In the diagram, drag the Web service input module somewhere near the Score Model module.
Connect the Web service input module to the Score Model module.
Click RUN to validate the steps in the model.
Click DEPLOY WEB SERVICE to deploy the model as a web service.
On the dashboard of the model, download the Excel 2010 or earlier workbook for REQUEST/RESPONSE.
Ensure that you download the Excel 2010 or earlier workbook even if you are running a later version of Excel on your computer.
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 provide independent views into the event stream that enable apps and Azure services to independently consume data from the same Event Hub endpoint. In this section, you add a consumer group to your IoT hub's built-in endpoint that is used later in this tutorial to pull data from the endpoint.
To add a consumer group to your IoT hub, follow these steps:
In the Azure portal, open your IoT hub.
On the left pane, select Built-in endpoints, select Events on the right pane, and enter a name under Consumer groups. Select Save.
Create, configure, and run a Stream Analytics job
Create a Stream Analytics job
In the Azure portal, click Create a resource > Internet of Things > Stream Analytics job.
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.
Add an input to the Stream Analytics job
Open the Stream Analytics job.
Under Job Topology, click Inputs.
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 output to the Stream Analytics job
Under Job Topology, click Outputs.
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 a function to the Stream Analytics job to call the web service you deployed
Under Job Topology, click Functions > Add.
Enter the following information:
Function Alias: Enter
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.
Configure the query of the Stream Analytics job
Under Job Topology, click Query.
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
[YourInputAlias]with the input alias of the job.
[YourOutputAlias]with the output alias of the job.
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.
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.
Open Azure Storage Explorer.
Sign in to your Azure account.
Select your subscription.
Click your subscription > Storage Accounts > your storage account > Blob Containers > your container.
Open a .csv file to see the result. The last column records the chance of rain.
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 all extended IoT scenarios, see the following: