Creating IoT Led Blinking App With Raspberry Pi 2

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 responsiv
e.

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-

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)
  3. Raspberry Pi Model 2 or higher
  4. 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. 

  1. using Windows.Devices.Gpio;  
  2. public sealed partial class MainPage: Page {  
  3.   
  4.     GpioPin _pin;  
  5.   
  6.     public MainPage()  
  7.   
  8.     {  
  9.   
  10.         this.InitializeComponent();  
  11.   
  12.         Loaded += MainPage_Loaded;  
  13.   
  14.     }  
  15.   
  16.     private void MainPage_Loaded(object sender, RoutedEventArgs e)  
  17.   
  18.     {  
  19.   
  20.         var controller = GpioController.GetDefault();  
  21.   
  22.         _pin = controller.OpenPin(26);  
  23.   
  24.         _pin.SetDriveMode(GpioPinDriveMode.Output);  
  25.   
  26.         _pin.Write(GpioPinValue.High);  
  27.   
  28.     }  
  29.   
  30.     private void btnON_Click(object sender, RoutedEventArgs e)  
  31.   
  32.     {  
  33.   
  34.         _pin.Write(GpioPinValue.High);  
  35.   
  36.     }  
  37.   
  38.     private void btnOFF_Click(object sender, RoutedEventArgs e)  
  39.   
  40.     {  
  41.   
  42.         _pin.Write(GpioPinValue.Low);  
  43.   
  44.     }  
  45.   
  46. }  
 
Note - Automatically, the following code will be generated in XAML code View when we are done in the design View.

  1. <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">  
  2.   
  3.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  4.   
  5.         <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" />  
  6.   
  7.         <Button x:Name="btnON" Content="LED ON" HorizontalAlignment="Left" Margin="77,112,0,0" VerticalAlignment="Top" Click="btnON_Click" />  
  8.   
  9.         <Button x:Name="btnOFF" Content="LED OFF" HorizontalAlignment="Left" Margin="77,157,0,0" VerticalAlignment="Top" Click="btnOFF_Click" />  
  10.   
  11.     </Grid>  
  12. </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.

 

Step 7-
 Change the target device as remote machine in Debug option in the project.



Step 8
 - Deploy your UWP App in Raspberry Pi2 – IoT Kit (Remote Machine ).

 

The output of the UWPIoT App is the following.


Summary - Thus, you have successfully created and deployed your IoT UWP Application in Visual C# - UWP with Raspberry Pi2 – IoT Kit.