We created an Azure Function for you. It is triggered every time the Iot hub receives a message from your Pi and forwards the needed content to the Azure Machine Learning service. In return it also receives the rain prediction to the sensor temperature and humidity data.
We will stay on your local machine to implement this.
The automated Machine Learning model should have trained by now. In order to be able to use the trained Machine Learning model, we first need to deploy it. Therefore, please follow the next steps:
(Btw, If you have trained and deployed the Machine Learning model using the ML Designer, you can skip this step.)
-
Navigate back to the Azure Machine Learning Studio (via the portal move to your AML service and from there to the studio).
-
Navigate to Jobs and select your experiment predictRain.
-
Under Display name you should see the details of your run. Select the only run there is.
-
There select the VotingEnsemble - it should be the best ML Algorithm for the given data. (Note: The first algorithm in the list is the best one.)
-
Select Deploy so we can consume this ML model as an endpoint. Choose the Deploy to web service option:
There give it a name e.g. mlendpoint and for Compute type select Azure Container Instance. Switch Enable authentication to on. After that hit Deploy.
This will take a bit so let's move on to the next task.
Open a terminal on your local computer again and make sure your prefix is still stored in it.
- We will start by creating our general-purpose storage account.
az storage account create --name $prefix'awjstorage' --location westeurope --resource-group $prefix'iotpirg' --sku Standard_LRS
- We need to create an Azure function at this point. We are going to keep using Python.
az functionapp create --resource-group $prefix'iotpirg' --consumption-plan-location westeurope --runtime python --runtime-version 3.9 --functions-version 3 --name $prefix'iotfunction' --os-type linux --storage-account $prefix'awjstorage'
-
Now we start off locally. To work with Azure Funcitons locally we need to install the Azure Functions Core Tools. Follow these instructions to do so. Restart your terminal and enter this to make sure everything works:
func --version
-
Make sure you are up to date on the AzureIoTHack git repo. While in the repo check:
git pull
We want to activate a virtual environment named .venv. We have already created all necessary parts, for you to create the Azure function. Therfore, change into the directory, where the Azure function files are stored.
cd raspberrypi_function
For Azure Functions we need to use Python Version 3.7, 3.8 or 3.9. Have a look whether you have the correct version installed. If not please do so.
python --version
If you have one or more versions installed you can set the version of the virtual environment you will create next by adding
-3.7
,-3.8
or-3.9
to the command.Using PowerShell: py -3.9 -m venv .venv (In case you have several versions of Python installed. Run as Administrator)
py -m venv .venv
.venv/scripts/activate
Using bash:
python -m venv .venv
source .venv/bin/activate
sudo apt-get install python3-venv
-
While we implemented the function for you, it still needs connection to your resources. We are starting by adding the AzureWebJobsStorage and the IoT hub ConnectionString values in the 'local.settings.json'. To do so open the function in the IDE of your choice. E.g. with VS Code:
cd raspberrypi_function code .
Get the needed values:
# for AzureWebJobsStorage az storage account show-connection-string --name $prefix'awjstorage' --resource-group $prefix'iotpirg' --output tsv
Paste the output 'DefaultEndpointProtocol=https;A...' as value for AzureWebJobsStorage in the local.settings.json.
# for ConnectionString az iot hub connection-string show -n $prefix'iotpihub' --default-eventhub --output tsv
Paste the output 'Endpoint=sb://...' as value for ConnectionString in the local.settings.json.
# for DeviceConnectionString az iot hub connection-string show -n $prefix'iotpihub' --output tsv
Paste the output 'HostName=...' as value for DeviceConnectionString in the local.settings.json.
-
We will also enter our Machine Learning model endpoint and its key. The az CLI extension for this is currently still experimental so we need to navigate back to the Azure Machine Learning studio. Under Endpoints select the endpoint you previously deployed.
On the Consume tab of your endpoint you will find a REST endpoint. Paste the value 'http://...' as value for AzureMLurl in the local.settings.json. Under Authentication copy the Primary key and paste the value 'yqpie4...' as value for AzureMLkey in the local.settings.json.
As you are already here go to the Test tab and test your endpoint. Your function is now ready to run. If you are using VS Code hit F5 to start the function. If not start the function from the raspberrypi_function folder by entering:
Now, ensure you have installed all required modules and packages shown in file requirements.txt
pip install -r requirements.txt
Your function is now ready to run. If you are using VS Code hit F5 to start the function. If not start the function from the raspberrypi_function folder by entering:
func start
Connect to your Pi again. Open a terminal and run the 'temphumidrain.py' script. Now we can run the application. Make sure you are in the 'raspberry_app' folder and run the following:
python3 temphumidrain.py
Temperature and humidity data are still send to the Azure IoT Hub and displayed on the Sense Hat's LEDs.
Now our Pi also listens to the Azure IoT hub, which forwards the result of your ML model to your Pi. There it displays the result as a sun, if the prediction from temperature and humidity data is no rain, and an umbrella if the result is rain.
- Now we are going to run our function in Azure. Since this command takes the Python version of the current environment make sure to run it from within the .venv environment.
func azure functionapp publish $prefix'iotfunction'
- There is one thing missing. Our Connection String and the connection to the Azure Storage account currently reside in the
local.settings.json
file of the function project. This file will not be uploaded to Azure (see.funcignore
for the files that will not be uploaded). We can set the needed keys in the Azure portal. So first navigate to the portal. - There find your Azure Function and from there the function
iothubtrigger
you just uploaded underFunction
. - Here you can also see an overview of how often the function was triggered. For now we need to enter the keys that we did have in the
local.settings.json
. Navigate toFunction Keys
, select+ New function key
and kopy all the key-value-pairs from yourlocal.settings.json
. TThe result should look like this with the addition of the DeviceConnectionString:
This is how your final object should look like:
Go to the next steps