This project is a quick introduction to getting and visualizing data, or telemetry, into Microsoft Azure's IoT Hub using Azure functions and Table Storage. This approach leverages VS Code extensions for Azure so you can do everything via code.
In this sample we set up 3 Azure resources: an IoT Hub, Functions, and Table Storage. IoT Hub connects internet enabled devices, like the Pi, to other Azure services. Azure Functions offers serverless compute which we use to process and save the data, and Table Storage is a no-SQL database so that we have somewhere to store the data. The only service in this sample that does not offer a free tier is Table Storage.
Estimated build time: 25 min.
Difficulty: Beginner++
Estimated Cost: < $1 / month (Note: my total cost was $0.02)
Why Microsoft Azure?
There are dozens of different IoT services that enable you to gather, visualize and use data from hardware like the Raspberry Pi. Just like any tool, the goal is to pick the right service for the right job.
Microsoft Azure is ideal for IoT projects that need strong security and privacy, like for home automation or business applications. It's also a good choice for projects where you are aiming to go from prototype to production and will need the ability to scale projects.
For a general overview of Microsoft Azure, check out this 3-min video.
Prerequisites- An active Azure account. If you don't have one, you can sign up for a free account here.
- VS Code
- Download Azure IoT Hub,Azure Functions, and Azure Storage extensions for VS Code.
- Azure CLI
- Azure Functions Core Tools -- be sure to install this before you begin.
- Python 3
First you'll provision the Azure resources needed for this sample. You're going to use IoT Hub, Azure Functions, and Table Storage.
Preparing your environment1. If you haven't already, clone this repo to your computer.
2. Inside VS Code, open command prompt or terminal and navigate to pi-azure-recipes
3. Add the IoT devices capability to your subscription. In your terminal execute each of the following commands, replacing <VARIABLE>
as needed:
az login
az account set -s '<YOUR SUBCRIPTION NAME>'
az provider register --namespace Microsoft.Devices
4. In command prompt or terminal type and run code 01_iot
. This will open the project folder in VS Code.
5. Press F1 to open the command palette, search for and select Azure Functions: Create New Project
6. Choose browse, and select the folder named data_processing
7. Select Python for programing language and then select the interpreter path
Note: Only python version 3.6, 3.7, and 3.8 are supported
8. Select Skip for now for template.
9. Select No for all the prompts in the creation process.
10. Your function is now initialized in VS Code!
Create IoT Hub1. Next you'll set up and IoT Hub. This will deploy a resource on Azure
2. Press F1 to open the command palette, search for and select Azure IoT Hub: create IoT Hub
3. Select your subscription, then select + Create Resource Group
4. Give your resource group a name
5. Select a Location that is located near you.
Aside: A location close to you will increase the speed of communication.
6. Select F1: Free for the pricing tier
Note: You can only have one free tier active per account
7. The IoT Hub should now appear in the Azure IoT Hub tab
8. Now we need to get some information using Azure CLI
9. First, we need to sign in. Open a terminal and execute each of the following commands, replacing <VARIABLE>
placeholders as needed:
az login
az account set -s '<YOUR SUBCRIPTION NAME>'
10. Now let's retrieve the connection string for the built-in event endpoint:
az iot hub connection-string show -n '<IOT HUB NAME>' --default-eventhub --query connectionString
11. Copy the connection string. Don't close this yet, we'll come back to it in a second!
12. In in the data_processing folder, open the local.settings.json file that was created with your function
13. Add the connection string to Values with the variables name "IoTHubConnectionString". It may have the same name as your storage account, or it may look like the following:
"IoTHubConnectionString": "Endpoint=sb://<NAMESPACE>.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=<SHARED_ACCESS_KEY>;EntityPath=<EVENT_HUB_NAME>"
14. Now let's get the build-in event endpoint name:
az iot hub show --name '<IOT HUB NAME>' --query properties.eventHubEndpoints.events.path
15. In the same local.settings.json, add a new key called eventHubName in the Values section and set the value to the output from the previous step. It may have the same name as your storage account, or it may look like the following:
"eventHubName": "iothub-ehub-xxxxxxxxxx-xxxxxxxx-xxxxxxxxxx"
Create storage account1. Press F1 to open the command palette, search for and select Azure Storage: Create Storage Account
2. Select your subscription
3. Give your storage account a name
Note: this name has to be globally unique
4. Open the command palette, search for and select Azure Storage: Copy Connection String
5. Open the local.settings.json file that was created with your function.
6. Add the connection string to Values with the variables name "AzureWebJobsStorage":
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<STORAGE_ACCOUNT_NAME>;AccountKey=<ACC
Setup your Raspberry Pi Device1. Press F1 to open the command palette, search for and select Azure IoT Hub: Create Device
2. Give the device a name
3. The connection string for the device should print in the Output window, note this connection string, you'll need later on your Raspberry Pi
4. Connect your raspberry Pi to a monitor and keyboard or use the instructions here to setup your pi for SSH
5. Using a USB drive or an SSH file transfer software move the files in the client folder to the Pi
6. Run the python_environment_setup.sh shell script:
bash python_environment_setup.sh
7. Once the script finishes, open the newly created.env file
8. Paste the device connection string between the single quotes. It should look similar to the following:
CONNECTION_STRING='HostName=<IOTHUB_NAME>.azure-devices.net;DeviceId=<DEVICE_NAME>;SharedAccessKey=<SHARED_ACCESS_KEY>'
9. In the client folder on your Pi type:
source ./.venv/bin/activate
10. Then type:
python raspberry_pi_client.py
11. Your device is now sending telemetry to IoT Hub!
Monitoring locallyThis will let you see the raw messages being sent by the Raspberry Pi.
1. In the VS Code, open the command palette with the F1
key
2. Search for and select "Azure IoT Hub: Start monitoring built-in event endpoint"
3. You should see the telemetry from the Pi in the terminal windows:
4. To stop monitoring, open the command palette and select "Azure IoT Hub: Stop monitoring built-in event endpoint"
Deploying your function1. Open data_processing\telemetry_saver\function.json
2. Press F1 to open the command palette, search for and select Azure Functions: Deploy to function app
Note: this will create a few resources in your Azure subscription
3. Select "Create new Function App in Azure" and give your function app a name
4. Select Python 3.8
5. Select a region near where you are located
6. When the function deployment completes you will be given the option to upload your local settings. Select Upload settings to upload your connection string to the App settings in Azure
Monitoring remotelyLet's see your function executing on Azure!
1. Open the command palette
2. Search for and select Azure Functions: Start Streaming Logs, press Enter
3. This will open Azure in your browser. You should see something similar like this:
If you keep the resources you provisioned, you'll continue to incur costs on them. Let's clean them up!
1. In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select Azure Resource Groups: Delete...
2. Choose your resource group, and press Enter. Follow the prompt(s).
3. Now let's delete the storage. Open the command palette again and search for and select Azure Storage: Delete Storage Account...
4. Select the storage account to delete and press Enter
5. Double-check and confirm your decision when you are prompted.
Deletion may take a couple of minutes. When it's done, a notification appears for a few seconds. You can also select the bell icon at the top of the page to view the notification.
Going ForwardCongratulations! You've connected a Raspberry Pi to IoT Hub! If you want to learn more about using the Pi with Azure, check out these tutorials:
- IoT Hub #2: Trigger events on your Raspberry Pi using Azure Functions and IoT Hub
- Computer Vision: Use the Computer Vision Cognitive Service to detect objects in an image
- IoT Central: Connect and control a Raspberry Pi with IoT Central
Please leave any questions in the comments or feel free to open issues in the GitHub repo. Happy making!
Comments