Introduction
IoT is simply the network of interconnected things/devices which are embedded with sensors, software, network connectivity, and necessary electronics, that enables them to collect and exchange data making them responsive.
Raspberry Pi is a small computer that we can use for developing IoT based application. Raspberry Pi includes Windows 10 IoT core Operating System. In this article, we will discuss how to develop IoT application, using UWP.
Before reading this article, please go through the following article.
Reading this article, you can learn how to develop IoT Application using Universal Windows Apps development with XAML, Visual C#, and Raspberry Pi Device.
The following important tools are required for developing UWP-
- Windows 10 (Recommended)
- Visual Studio 2015 Community Edition (It is a free software available online)
- Raspberry Pi Model 2 or higher
- LED light, breadboard, 2 x Female to Male connector wires
Now, let's discuss step by step app development.
Step 1 - Open Visual Studio 2015 -> Start -> New Project-> Select Universal (under Visual C#->Windows)-> Blank App. Give it a suitable name App (UWPIoT) ->OK
Step 2 - Choose the Target and Minimum platform version that your Windows Universal Application will support. After this, the Project creates App.xaml and MainPage.xaml.
Step 3 - Open (double click) the file MainPage.xaml in the Solution Explorer. Click on the Toolbox tab on the left, to open the list of Common XAML controls. Expand Common XAML Controls, and drag the required control to the middle of the design canvas.
Add a Text Block and change the name and Text Property for Title.
Add a Button and change the name and content property for LED ON.
Add Button and change the name and content property for LED OFF.
Create a click event for both the LED buttons.
Step 4 - Add Reference for Windows IoT Extensions for the UWP.
Select UniversalWindows-> Extensions-> Windows IotExtension fro the UWP (Versions 10.0.14393 - Depends on your Raspberry Pi-2 Windows 10 Core operating system),
Step 5 - Add the following namespace and code for LED ON and LED OFF.
- using Windows.Devices.Gpio;
- public sealed partial class MainPage: Page {
-
- GpioPin _pin;
-
- public MainPage()
-
- {
-
- this.InitializeComponent();
-
- Loaded += MainPage_Loaded;
-
- }
-
- private void MainPage_Loaded(object sender, RoutedEventArgs e)
-
- {
-
- var controller = GpioController.GetDefault();
-
- _pin = controller.OpenPin(26);
-
- _pin.SetDriveMode(GpioPinDriveMode.Output);
-
- _pin.Write(GpioPinValue.High);
-
- }
-
- private void btnON_Click(object sender, RoutedEventArgs e)
-
- {
-
- _pin.Write(GpioPinValue.High);
-
- }
-
- private void btnOFF_Click(object sender, RoutedEventArgs e)
-
- {
-
- _pin.Write(GpioPinValue.Low);
-
- }
-
- }
Note - Automatically, the following code will be generated in XAML code View when we are done in the design View.
- <Page x:Class="UWPIoT.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPIoT" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
-
- <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
-
- <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="77,39,0,0" TextWrapping="Wrap" Text="IoT UWP LED Blink Demo" VerticalAlignment="Top" FontWeight="Bold" FontSize="16" Height="26" Width="231" />
-
- <Button x:Name="btnON" Content="LED ON" HorizontalAlignment="Left" Margin="77,112,0,0" VerticalAlignment="Top" Click="btnON_Click" />
-
- <Button x:Name="btnOFF" Content="LED OFF" HorizontalAlignment="Left" Margin="77,157,0,0" VerticalAlignment="Top" Click="btnOFF_Click" />
-
- </Grid>
- </Page>
Step 6 - In Raspberry Pi2, connect the wires form GPIO 26 to bread board for Led signal and Pin 6 is connected for ground.