As you might know by now, for these challenges I like to utilize additional tools to enrich the solutions you are creating. Azure is probably the biggest toolset that can bring lots of functionality to your solutions. This month, we will explore Azure and learn a few things. The first Azure topic that we will discuss is IoT. Other topics probably will be addressed in future challenges. If you have a suggestion, let me know on the discussion board.
Challenge Objectives
🎯 Learn about Azure
🎯 Turn your phone into an IoT device
🎯 Build Azure IoT resources
🎯 Connect Azure resources to the Power Platform
🎯 Get familiar with the Adaptive Cards designer
🎯 Implement Adaptive Cards in Teams
🎯 Collect the input
🎯 Share your connector
🤓 What is Azure
Azure is Microsoft’s cloud platform. You might know the cloud from an online storage location. This is one of the products that Azure entails. It consists of 200 different products that you can deploy. A big advantage of the cloud is that you can deploy resources within minutes with a pay-as-you-go model. No need for expensive hardware and skilled employees to maintain this hardware. Another big advantage is that it can scale in line with how a business grows.
Enough of the marketing talk. What can we do with it? Well, we can do some cool IoT stuff and use that in our Power Platform solutions.
🤓 What is IoT
You probably know this already, IoT is an abbreviation of the Internet of Things. Practically, it means that assets are equipped with all sorts of sensors. The telemetry of those sensors is collected to discover insights. A common use of this data is that it feeds a dashboard. Some take it a step further and use this telemetry data for predictive analytics.
Some assets can also execute a command. A good example are Dutch birdges that used to have a bridge operator at site. Nowadays almost all bridges are operated from one location. A more consumer related application of this technology is your smart home. By the press of a button all the lights switch off and the door locks. Just like you can program your lights to switch on when you come home, command supporting IoT assets can be programmed to execute whenever you want it.
These two things, reading telemetry data and programming a command, is what we will be doing during this challenge.
📡 Create an IoT Device
No, we will not be soldering, nor will we be using an Arduino or Raspberry Pi. These things are really fun if you are into IoT, but the aim of these challenges is that everyone can expand their skills, without any prerequisites. So, we will turn your phone into an IoT component. It has all sorts of sensors and we can also make it do stuff. Cool, huh?
Create an Azure Resource Group
A resource Group is a container for your Azure services. It can be used to allocate costs, have separate resource groups for global operation corporations, etc. We only need one so we can deploy the resources we need for this challenge.
With your developer account, go to the Azure portal.
Fill in the form to register. The features that will be used will not cost a thing. The credit card credentials are just for verification and if other resources are deployed (this challenge is FREE). You will also receive $200 to spend on resources. So, if you want to detour or dive in a bit further, you can!
Once you have your account, create a resource group.
Name your Resource Group Challenge006 and select the region you are in.
Select the Review + create tab and select Create.
Your Resource Group will be created now. This might take a few seconds. Once done, you will be notified.
Download the IoT Plug and Play app
This is an app made by Microsoft for us to test IoT functionality. It’s available for Android and iOS.
Once installed, you can see that when you launch the app, it asks to connect to the Azure IoT cloud. We have Azure, but no IoT resources deployed, yet. So, let’s fix that.
Create an IoT Central Application in Azure
If not still opened, go to the Azure portal.
As mentioned, Azure resources are deployed to a resource group, so navigate to the earlier created resource group.
Within your resource group, select Create.
Search for IoT Central application and select Create this resource.
Name it challenge006iotapp, select Custom application as a template and select the region you are in.
Select Pricing plan Standard 2.
Click Create.
The resource will be deployed to your cloud tenant. This will take just a few second.
Connect Your Device
Now that we have both the app and the cloud resource, we just have to connect.
Navigate to your newly created IoT Central application resource.
On the right side you can see the URL to this cloud application. Open it.
On the Devices tab, create a new device.
Specify the required fields. I entered iPhoneMiguelXr for both the name and ID. The device template us just unassigned.
Once created, open the device by clicking on the record in the list of devices.
Click on Connect and select the QR code tab. A QR code will be shown.
On your phone app select Scan QR code, do so, and wait for it to connect.
All done! Now your phone telemetry data is sent to the cloud. If you select your device within the IoT Central application, you can see all telemetry in the overview tab.
Device Templates
On the left side panel, you can dee the Device templates tab. If you click on it, you will see the IoT Plug and Play mobile template. Because we scanned the QR code, everything is done for us. You can see that the component capabilities are sectioned into Sensors and Device information. Earlier we discussed telemetry and command capabilities of IoT assets. That is exactly what you can find here. There is also the property type which is static information about the IoT Asset itself that can be retrieved.
If you want to create IoT devices yourself, you will also to develop this template. That is way too far out of my comfort zone, so I won’t dive into that. However, it is interesting to know that these templates exist. Manufacturers can develop these templates for their IoT hardware, to make the implementation of their devices easier for their clients. For Arduino there are also templates available at aka.ms/arduino if you want to dig a little deeper.
📡 Get Device Telemetry in Your Solutions
We can now get the device telemetry in our solution. This is premium functionality, so additional licenses are required. You can start a trial in the Microsoft 365 admin center. Make sure the app is active during the following sections.
Power Automate
There is a connector for IoT Central, so this part should be fairly easy to do.
Create a manually triggered flow.
Search for IoT Central and select the Azure IoT Central V3 connector
Get component telemetry value
You can select the Azure IoT App challenge006iotapp we earlier created
Enter the device ID you specified (in my case iPhoneMiguelXr)
This is a bit weird of the connector, but you should first specify a Device template before you specify the other fields. There is only the one the app populated for us listed, so select that.
In the Device component section you should specify the sensors. Remember that when we looked into the device template, only the sensors section contained telemetry types. That’s why if you specify device information, no telemetry can be selected.
I want you to stay focused, so select the Pressure telemetry. That way you don’t have to look out of the window to know what the weather is like.
Add a new step and search for the Get device properties action of the Azure IoT Central V3 connector. This way you will retrieve the property types.
As we have seen in the device template, there is also a command available. Let’s see what we can make our phone do by triggering a flow.
Add a new step with the Run a device command action
Specify what is required. Again, the template must be specified before the available Device Command options are listed. You can see that there is one command that can be executed, which is turning the light on.
Enter 1 at Parameter Duration, 1 at Parameter Pulses interval, and 20 at Parameter Pulses. This means that when you trigger the flow your flash turns on 1 second, then of for one second, and that 20 times.
Now save your flow and run it.
If your pressure value is over 1020, you might want to take a break and actually have a look out of the window, or maybe even go out. And don’t worry about your flashlight pulsing. You just made it do that. Actions one and three clearly show the core functionality of IoT. Getting sensor data from an internet-connected device and remotely make the device execute commands. I hope your creativity is triggered and you start thinking about what is possible with other types of sensors and commands. Please share your thoughts on the discussion board on what you think might be an interesting use case for IoT.
We’ve only shown Power Automate, but there are more products. Let’s get this data into Power Apps.
Power Apps
As you know, connectors can be used in Power Automate and Power Apps. So, it wouldn’t be too much of a challenge you might think. Well, this one is a bit different. The devices that you connect to IoT Central all have different sensors, properties, and commands. We’ve seen our device template where the capabilities were listed. A different device will have different capabilities. The way we specify our request must be flexible to the capabilities of our devices. This is called a dynamic schema. Power Automate is much better at dealing with dynamic schemas compared to Power Apps. But we will make it work.
Create a new canvas app.
Go to Settings and enable the Experimental feature Dynamic schema.
Save the app and reload the page. If you expand the Power Fx function bar, you will see a button has been added (see image above). This will enable you to capture the schema manually.
On the Data tab, select Add data and search for the Azure IoT Central V3 connector. Because of the flow the connection should already exist.
Add a button. We will save the pressure telemetry value to a variable by clicking the button. let’s name that variable pressure. Set(pressure,
The value for the variable will be retrieved through the connector. Set(pressure, AzureIoTCentralV3. If you type the dot, you will get a list of possible connector calls. The naming is different from what you’ve seen in Power Automate. So how do we know which operation to use? In Power Automate there is a Peek code function, that shows what the connector actually uses as an input. Here you see the operationId is Devices_GetComponentTelemetryValue_V1. This is what we have to type into the Power Fx function, but without the underscores. Set(pressure, AzureIoTCentralV3.DevicesGetComponentTelemetryValueV1(
Once that is entered, you can see the input the connector expects. Enter all items that it asks for. Use the Peek code functionality of Power Automate is again a useful way to retrieve that information. For me, this is the result. Set(pressure, AzureIoTCentralV3.DevicesGetComponentTelemetryValueV1("iPhoneMiguelXr", "sensors", "barometer", "ef2d41bb-dcc4-436d-9288-e4492439bf50", {template: "dtmi:azureiot:l826ixahacx"})
If you run your flow, you can see the telemetry output is a timestamp and a value. We want to capture the value. Normally that means we type add .value to the function. But when you type a dot, only the timestamp shows up. Now if you click Capture schema it will show value. Nice! For me, this is my final function. Set(pressure, AzureIoTCentralV3.DevicesGetComponentTelemetryValueV1("iPhoneMiguelXr", "sensors", "barometer", "ef2d41bb-dcc4-436d-9288-e4492439bf50", {template: "dtmi:azureiot:l826ixahacx"}).value)
Add a label to your canvas that shows the pressure variable and press the button.
You now have the skill to get telemetry data into your app. Getting the device properties in should now be an easy thing for you to do, so go ahead and add that too. I couldn’t get the command function working properly directly within Power Apps. If you use Peek code on that action in Power Automate, you can see that three parameters are used as an input in the request body. To bypass my own limitation, I triggered a flow from within Power Apps. I assume you know how to do this, so again no explanation is needed from my side. The telemetry action can also be done with a power-apps-triggered flow and would be my preferred route in real-world scenarios. But this is for learning, and now you know about dynamic schemas and the value of the peek code functionality in Power Automate.
Power BI
Until now, we made a single request with Power Automate and Power Apps. Within the IoT Central application, there is a dashboard that looks similar to Power BI. The cool thing is that it shows a line chart of your pressure telemetry data. The downside is that it’s within IoT Central. This is a Power Platform challenge, so that alone should be enough reason to get that data into Power BI. That’s what we will be doing.
NOTE Additional Azure resources will be deployed that come with a cost. If you are still within the $200 trial budget you can proceed. Just make sure to turn of or delete the whole resource group after completion
As you might know, Power BI has a streaming dataset functionality for dashboards. We won’t be creating a dataset with Power Query editor and use that in a report. Just a dashboard will do the trick. This is done in the browser. Go to YOUR WORKSPACE and create a new dashboard. When you click edit, you can add a tile. There you see the real-time option Custom Streaming Data. Sounds good to me! if you click through the interface, you will see three options. We will be creating the Azure stream option in this section. The image below shows how we get from the phone data to Power BI. IoT Central is the second icon, so we have fixed that. The third and fourth must be deployed, which are Azure Event Hubs and Azure Stream Analytics.
Data Flow
Create The Required Resources
In your resource, search for Event Hubs and click create.
Specify everything that is required. Specify the Standard pricing tier. At the time of writing it is Saturday early morning, so I just named it challenge006.
Once deployed, a single instance should be created. I named this challenge006, again…
In the instance, click the Process data card.
Click explore and press Create Stream Analytics job. Again, specify everything required. For the Event Hub policy name, you can select the RootManageSharedAccessKey option.
Click create. All resources are ready to be used. We only have to link everything together.
From IoT Central to Azure Event Hubs
To make sure we get to icon 3 of the data flow image, we have to make sure the data from IoT central is sent to Azure Event Hubs.
Go to you IoT Central application.
On the left navigation bar, select Data export.
Add a destination. The destination type is Azure Event Hubs.
For the Authorization, select connection string.
For the connection string, go to the Event Hubs Namespace, Shared access policies and select the RootManageSharedAccessKey. Copy the Connection string primary key and enter it in IoT Central.
Save the Destination.
Create an export.
In the Data section, select Telemetry.
Select the Destination you just created and save.
This should do the trick. Open your Azure Event Hubs Namespace to see messages coming in. The last step is to set up the stream job to send the info to Power BI.
Stream Analytics Job
The stream Analytics job enables us to forward data from Event Hubs to Power BI. It basically needs an input and an output. Optionally we can adjust the query to specify what the job forwards. That’s what we will do now.
Open the Stream Analytics job from your resource group.
On the inputs page, add a stream input.
You’ve created an Event Hub, so that should be the option to select. In the form, select the existing Event hub you’ve set up in the previous step.
On the Outputs page, create a new output and select the Power BI option.
Enter the required fields. You need to login for authorization. You can specify your own workspace for now, because we won’t be sharing it with others.
Open the Query page.
In the bottom section you can preview what is coming in and going out. There are quite a lot of data points that we won’t need for our tile. The query language is SQL. the asterisk indicates everything is forwarded. If you test the query, you will see the same table structure as the output. We only need a DateTime and the barometer value for the Power BI tile. Below you can see the adjusted query I created. The things I adjusted are the INTO part. This is now the output we just created. I also changed the asterisk as you can see. Note how I drill down into the input table to retrieve the barometer value. If you are familiar with Power Fx from Power Apps, you probably understand the function. The AS operation transforms the column name to pressure. This is helpful for our next step in PowerBI. Try to get it working yourself and inspect your test results to see how you transformed the input with a SQL query.
If you are ready, you can save the query.
On the overview screen, start the Stream Job. If you want to adjust it later on, you need to stop it and start it once you’ve made your adjustments.
SELECT
challenge006.[telemetry].barometer AS pressure
,challenge006.enqueuedTime AS time
INTO
[PowerBI]
FROM
[challenge006]
I am really proud of you if you made it here. Azure can be quite overwhelming at first sight, but you’ve done it. The last part is within Power BI.
Create The Power BI Tile
You’ve created a dashboard earlier in your workspace. Open your workspace to see that a dataset has been added. The Azure Analytics Stream job did this for you. Select that data set to edit it. On the side panel, you will see the data that is coming in. Note that it is structured as you specified earlier in your SQL query. Make sure that the pressure value is datatype Number and the time value is datatype DateTime. If this is sorted out, you are ready to add the tile to your dashboard.
Add a new tile to your dashboard
Select the Custom Streaming Data option, just like how we started.
Select the dataset that has been added to this workspace.
Select Line chart as Visualization Type.
Put the time value on the axis and the pressure value as the value.
Proceed and press apply.
You should now be able to see telemetry data coming into your Power BI dashboard.
Pretty cool right? You have turned your phone into an IoT device, and connected it to the cloud to interact with the data in three ways: Power Automate, Power Apps, and Power BI. Finishing the last step especially is quite impressive.
Additional Info
As you might have experienced, my naming of resources is quite rubbish in this challenge. If you want to create a real solution, I recommend reading the Azure naming best practice. Maybe I should do that too…
🎒 Key takeaways
👉🏻 We all have an IoT device
👉🏻 The cloud enables us to interact with these assets, no matter where we are
👉🏻 IoT assets collect telemetry data and some can execute a command
👉🏻 We can combine Azure resources with the Power Platform
Super blog, I learnt a lot from this one blog