IoT: Our First Application

In this article, I want to share my passion for .NETFramework with all those like me who are passionate about the world of programming.

Introduction

Third introductory article on the 'Internet of Things, written in collaboration with Piero Sbressa. After the installation of Windows 10 IoT Core found at this link, Now we will try to create an application in order to blink an LED, but stated that normally turn on or flashing an LED is not considered IoT, but in this case, serves to give an introduction and an idea of how to develop future applications or prototypes Raspberry Pi2 with Visual Studio 2015.

Electronic circuit

The material that we need can be found in the following image:

Requirement material
                                                           Figure 1: Requirement material

  • 1 LED color you want (red, green or yellow.)
  • 1 Resistance 220 Ohms.
  • 2 cable connectors
  • 1 Breadboard

The final connection you see in the following picture:

Final connection
                                                                  Figure 2: Final connection

Connection
                                                                  Figure 3: Connection

Pin Circuit with LED
                                                         Figure 4: Pin Circuit with LED

Circuit with LED
                                                               Figure 5: Circuit with LED

Below is the diagram of the electronic circuit. It states that to create a circuit or draw, are necessary for my personal experience, as well as dedicated software and hardware parts of the bases on the basics of electronics, otherwise my advice and devote themselves to the development of the code and leave to others the task earlier.

Electronic circuit
                                                            Figure 6: Electronic circuit

From the previous image, we note that we have two cables, one black and one red. The black cable representing zero volt connect me to pin GPIO5 corresponding to the pin 29 that are on the GPIO card Raspberry Pi2. The red wire on pin 3.3V PWR corresponding to pin 1. Below the connector pin GPIO thus facilitate us to connect.

GPIO thus facilitate us to connect
                                             Figure 7: GPIO thus facilitate us to connect

Importantly, the Led diode to work, must be properly connected, ie the katodo (because it recognizes and the shorter leg) must be connected to zero volts, the anode that is the other leg on the positive, which in this case It will be given by pin 3.3V PWR. Wrong wiring will not allow the diode LED work, you may damage the component. The resistance is important because generally a Led diode operates at a voltage of about 3.3V dc, in this case means that there is a voltage drop necessary and thus provide the correct voltage.

Setting up your PC in developer mode

Before starting to develop code, you must set the mode "developer" of our PC, otherwise it will not be possible to develop and test their applications. The procedure is very simple. We point the mouse on the icon of the notifications as shown.

Notifications
      Figure 8: Notifications

The next dialog box, select "All Settings".

select "All Settings"
                           Figure 9: select "All Settings"

We will be led to the next screen, select the command "Update and security."

Select the command "Update and security"
                                       Figure 10: Select the command "Update and security"

We go to the next screen select the "For developers," and soon after Developer mode.

Select the "For developers"
                                                   Figure 11: Select the "For developers"

A dialog box will appear, where we have to confirm with "Yes" key to activate this mode.

Confirm with "Yes" key to activate this mode
                                       Figure 12: Confirm with "Yes" key to activate this mode

Let's confirmation and we have enabled our PC development.

Enabled our PC development
                                                         Figure 13: Enabled our PC development

Now we are ready to create our first project IoT.

Creating the project

Before starting with the development, we have yet to download and install the Windows library IoT Core Project Templates found at this link. In this library, you will find everything you need for the development of Raspberry Pi2 and beyond, we will see later in the article. Now open Visual Studio, and create a new project Universal Windows App, in C#.

Create a new project Universal Windows App
                                    Figure 14: Create a new project Universal Windows App

We need at this point to add a Reference to 'Windows SDK IoT exstension for the UWP, as shown below.

Windows SDK IoT exstension for the UWP
                                                Figure 15: Windows SDK IoT exstension for the UWP

Now let's add in the Grid Xaml the following code:

  1. < StackPanel HorizontalAlignment = "Center"  
  2. VerticalAlignment = "center" > < Ellipse x: Name = "LED"  
  3. Fill = "LightGray"  
  4. Stroke = "White"  
  5. width = "100"  
  6. Height = "100"  
  7. Margin = "10" / > < TextBlock x: Name = "GpioStatus"  
  8. Text = "Waiting to initialize GPIO ..."  
  9. Margin = "10,50,10,10"  
  10. TextAlignment = "Center"  
  11. FontSize = "26667" / > < Button Name = "AccendeSpegneLEDButton"  
  12. Content = "Turn on LED"  
  13. HorizontalAlignment = "Center"  
  14. Click = "AccendeSpegneLEDButton_Click" / > < / StackPanel>  
  15. While the code-behind we put the following code:  
  16. / / Copyright(c) Microsoft.All rights reserved.  
  17. using System;  
  18. using Windows.Devices.Gpio;  
  19. using Windows.UI.Xaml;  
  20. using Windows.UI.Xaml.Controls;  
  21. using Windows.UI.Xaml.Controls.Primitives;  
  22. using Windows.UI.Xaml.Media;  
  23. Blinky namespace {  
  24.     public sealed partial class MainPage: Page {  
  25.         private const int LED_PIN = 5;  
  26.         Private GpioPin pin;  
  27.         Private GpioPinValue pinValue;  
  28.         Private SolidColorBrush yellowBrush = new SolidColorBrush(Windows.UI.Colors.Yellow);  
  29.         Private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);  
  30.         public MainPage() {  
  31.             InitializeComponent();  
  32.             InitGPIO();  
  33.         }  
  34.         private void InitGPIO() {  
  35.             var gpio GpioController.GetDefault = ();  
  36.             // Show an error if there is no GPIO controller if (gpio == null) {pin = null; GpioStatus.Text = "There is not the GPIO controller of this device."; return; }  
  37.             pin = gpio.OpenPin(LED_PIN);  
  38.             pinValue = GpioPinValue.High;  
  39.             pin.Write(pinValue);  
  40.             pin.SetDriveMode(GpioPinDriveMode.Output);  
  41.             GpioStatus.Text = "GPIO pin properly initialized.";  
  42.         }  
  43.         private void AccendeSpegneLEDButton_Click(object sender, RoutedEventArgs e) {  
  44.             if (pinValue == GpioPinValue.High) = {  
  45.                 pinValue GpioPinValue.Low;  
  46.                 pin.Write(pinValue);  
  47.                 LED.Fill = yellowBrush;  
  48.                 AccendeSpegneLEDButton.Content = "Turn Off LED";  
  49.             } Else {  
  50.                 pinValue = GpioPinValue.High;  
  51.                 pin.Write(pinValue);  
  52.                 LED.Fill = grayBrush;  
  53.                 AccendeSpegneLEDButton.Content = "LED Light";  
  54.             }  
  55.         }  
  56.     }  
  57. }  
After the piece of code before test the application, you need to set a few things to see. The first being that we are developing on Raspberry Pi2, and to set the ARM compilation mode, you can perform from the menu as in this figure.

Set the ARM compilation mode
                                                                  Figure 16: Set the ARM compilation mode

In reference to the previous image, you will notice that is on the run as "Remote computer". This is because we want to run the application on the card Raspberry Pi2. Select this mode, and the following screen should be placed the device name and the IP address of the Raspberry Pi2, clearing authentication, or you can change these settings by selecting the project, right-click, select "Properties" and immediately after "Debug", we will be led by the following screen.

Select "Properties" and immediately after "Debug"
                                 Figure 17: Select "Properties" and immediately after "Debug"

Execution

We can now proceed with the execution of our first application. F5 key and if we did everything correctly our LED lights up when you click on the button that we have in the MainPage, as we can see from the following pictures.

LED Lights on
                                                                Figure 18: LED Lights on

LED Lights off
                                                               Figure 19: LED Lights off

Conclusion

In this article, we created our first application with Raspberry Pi2 and Visual Studio 2015, starting from the design, implementation of the electronic circuit, set the PC mode developer, to finish with the programming and coding for the management of the LED. In subsequent articles, we will try to create other test projects, the purpose of these mini-articles is to give an introduction on how to design and create their own project and why not be able then to make us business. Article written by Piero Sbressa. And Carmelo La Monica, Microsoft Contributor.