Easy as Pi IoT on Windows 10 and Azure IoT Central!
App Dev Manager Rick McGuire demonstrates how to easily build and manage an IoT solution using .NET, Windows 10 IoT, a Raspberry Pi, and Azure IoT Central.
Have you ever tried building an IoT solution on an embedded device like a Raspberry Pi with an ARM processor? What languages did you have to use? What if that device had to send data to the cloud for dashboarding and alerting? How would you build the dashboard and manage alerts for authorized users?
Microsoft has simplified this entire space, using Windows 10 IoT running .NET UWP applications on a Raspberry Pi combined with our SaaS offing for IoT called IoT Central.
In this blog post I will walk through setting up a C# .NET UWP application running on a Windows 10 IoT on a Raspberry Pi device, setting up an Azure IoT Central project to chart your telemetry and send email alerts based on exceeding user-defined telemetry thresholds!
Here is the checklist:
- First thing is to get your hands on a Raspberry Pi 3 and install Windows 10 IoT OS on it. If you prefer to skip this and just demo this application on your laptop you can do that as well!
- Clone the sample C# UWP demo application that will send telemetry data to IoT Central from GitHub and build/run the project using Visual Studio.
- Create an IoT Central account and create an application with the 7-day free trial and setup a device and define a telemetry rule and trigger (alert).
- Build and run the application on your laptop to send telemetry data to IoT Central; verify data is being charted and rules are firing off email alerts when thresholds are exceeded.
- Compile the app for ARM, run the application on the Raspberry Pi (optional but highly recommended).
If you have your heart set on using a remote device, I would suggest you start with a Raspberry Pi 3. I got my hands on the CanaKit (Raspberry Pi 3 Complete Starter Kit); you can find it online for less than $70. You will need to download and install Windows 10 IoT as step 1. Once you have the Raspberry Pi 3, you can follow these instructions to install Windows 10 IoT on the SSD card. Essentially you need to insert the SSD card in the USB converter and then insert that into you USB port on your laptop, then download the IoT App.
Windows 10 IoT Core Dashboard:
If you need more detailed guidance, I recommend watching this channel 9 video. Installing Windows 10 IoT on Raspberry Pi 3 – A Programmer’s Guide to Internet of Things – Part 3: https://channel9.msdn.com/Blogs/MVP-Windows-and-Devices-for-IT/Windows-10-Installation-in-Raspberry-Pi
Now that you have Windows 10 IoT on the Pi, download the sample code for sending temperature data to IoT Central. The Sample C# UWP project is used to simulate a temperature device that sends telemetry data to IoT Central.
The sample C# code for the UWP application is out on GitHub for you to clone. This application has a UI that prompts for a temperature value and once you press the OK button it will send the data to IoT Central. This is a very basic application; but will get you started.
Git Repo: https://github.com/rickmcguire/IoTCentralDevice
Instructions to download and open the project:
Execute a command prompt (cmd) – assuming you already have Git installed.
C:\IoTTest>git clone https://github.com/rickmcguire/IoTCentralDevice
Please open the solution for “App1” and compile for x86 and run.
App1 UWP application screen shot:
Your application will throw an exception until you configure it to talk to IoT Central. First go out and click on “Get started >” to create a free trial account: https://azure.microsoft.com/en-us/services/iot-central/#. Once you have an account created, you will need to create a new application and a device type. Since there is already a great tutorial out there, I am not going to walk through the details in this blog.
Please review this tutorial – Define a new device type in your Azure IoT Central application: https://docs.microsoft.com/en-us/azure/iot-central/tutorial-define-device-type
Once you have created a device you need to gather some information so our IoT device (Pi or PC) can connect to IoT Central. You will need to select your device under device explorer, chose the device you created and click on “Connect” in the upper right corner.
IoT Central Portal:
A dialog will pop up; you need to capture the Scope ID, Primary Key, and the Device ID. Copy these items to notepad or OneNote, etc. You will need these values to create a connection string.
Azure IoT Central uses Azure IoT Hub Device Provisioning service (DPS), enabling IoT Central to support onboarding & connecting of devices at scale. To get the connection string you need to download the node.js package dps-keygen.
Follow these instructions to get the device connection string: https://docs.microsoft.com/en-us/azure/iot-central/concepts-connectivity#getting-device-connection-string
NOTE: If you don’t already have node.js and npm (node package manager) installed on your machine, download them first here: https://www.npmjs.com/get-npm
Once you execute the command line passing in Scope ID, Device ID and Primary key; this will provide the connection string (including the HostName you require (make sure to go to the \bin folder then to the folder for the OS you are running). You will need to unzip the dps_cstr.exe executable first before you can execute the command.
Node.js Command Prompt:
|C:\Users\yourname\AppData\Roaming\npm\node_modules\dps-keygen\bin\windows\dps_cstr>dps_cstr 0ne00028F38 180ivdg I8lEeZnzfesK0rsXxS79c9EJjpBIcCNJp3uHTlDXlJA=
Registration Information received from service: saas-iothub-8d7e4ece-e135-4e66-acec-b78b90d0e2c2.azure-devices.net!
In the MainPage.xaml.cs file update the deviceId, update the deviceKey with the Primary Key, and as well as the iotHubUri value. Once you have the information update the code, recompile and run again, when you enter temperature values and press okay you should no longer receive exceptions.
|//Function to send telemetry data to IoT Central
static async Task SendDeviceToCloudMessagesAsync(int temp)
string iotHubUri = “saas-iothub-8d7e4ece-e135-4e66-acec-b78b90d0e2c2.azure-devices.net”;
string deviceId = “180ivdg”;
string deviceKey = “I8lEeZnzfesK0rsXxS79c9EJjpBIcCNJp3uHTlDXlJA=”;
Once you run and compile the application you should be able to enter tempurature values and verify they are being captured on the IoT Central site. Go to Devices and select the device you added – see below.
One of the nice features of IoT Central is the ability to create email alerts. You can add rules to your devices, here I added a simple rule to send and an email if the temperature exceeds a threshold.
Here is the example email that is sent once a rule detects the temperature exceeding the threshold value. Most of this is straight forward and there is great documentation on the site here: https://docs.microsoft.com/en-us/azure/iot-central/.
Once you have this application running on your laptop, simply compile this for ARM and run it on the Raspberry Pi Device, this will not require any changes to the code! Your application should behave in the same manner. You will need to have you Raspberry Pi connected to the network, and ensure you have a monitor and keyboard connected to your Pi device, so you can enter temperature data.
Deploy a C# app to your Windows 10 IoT Core device: https://docs.microsoft.com/en-us/windows/iot-core/develop-your-app/appdeployment
Good luck and have fun – I hope you will see the value in IoT Central as well as see the potential with Windows 10 IoT to develop UWP applications!