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 (classic) account.
Deploy the weather prediction model as a web service
In this section you get the weather prediction model from the Azure AI Library. Then you add an R-script module to the model to clean the temperature and humidity data. Lastly, you deploy the model as a predictive web service.
Get the weather prediction model
In this section you get the weather prediction model from the Azure AI Gallery and open it in Azure Machine Learning Studio (classic).
Go to the weather prediction model page.
Click Open in Studio (classic) to open the model in Microsoft Azure Machine Learning Studio (classic).
Add an R-script module to clean temperature and humidity data
For the model to behave correctly, the temperature and humidity data must be convertible to numeric data. In this section, you add an R-script module to the weather prediction model that removes any rows that have data values for temperature or humidity that cannot be converted to numeric values.
On the left-side of the Azure Machine Learning Studio window, click the arrow to expand the tools panel. Enter "Execute" into the search box. Select the Execute R Script module.
Drag the Execute R Script module near the Clean Missing Data module and the existing Execute R Script module on the diagram. Delete the connection between the Clean Missing Data and the Execute R Script modules and then connect the inputs and outputs of the new module as shown.
Select the new Execute R Script module to open its properties window. Copy and paste the following code into the R Script box.
# Map 1-based optional input ports to variables data <- maml.mapInputPort(1) # class: data.frame data$temperature <- as.numeric(as.character(data$temperature)) data$humidity <- as.numeric(as.character(data$humidity)) completedata <- data[complete.cases(data), ] maml.mapOutputPort('completedata')
When you're finished, the properties window should look similar to the following:
Deploy predictive web service
In this section, you validate the model, set up a predictive web service based on the model, and then deploy the web service.
Click Run to validate the steps in the model. This step might take a few minutes to complete.
Click SET UP WEB SERVICE > Predictive Web Service. The predictive experiment diagram opens.
In the predictive experiment diagram, delete the connection between the Web service input module and the Weather Dataset at the top. Then drag the Web service input module somewhere near the Score Model module and connect it as shown:
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.
Make sure 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.
Download 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: