Sunday, January 14, 2018

Write an IOT application that controls LED with ON and OFF button using Windows IOT

-------------------------------------------------------------------------
Apparatus Required:
1 LED,2 jumper wires,1 resistor,Raspberry pi,breadboard
------------------------------------------------------------------------
XAML
--------------------------------------------------------------------------
<Page
    x:Class="Ledswitch.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Ledswitch"
    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}">
        <Button x:Name="On" Content="On" HorizontalAlignment="Left"
Margin="149,70,0,0" VerticalAlignment="Top" Click="On_Click"/>
        <Button x:Name="button" Content="Off" HorizontalAlignment="Left"
Margin="149,154,0,0" VerticalAlignment="Top" Click="button_Click"/>

    </Grid>
</Page>

CS
-----------------------------------------

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Gpio;

namespace App1
{
 
    public sealed partial class MainPage : Page
    {
        GpioPin _pin;
        public MainPage()
        {
            this.InitializeComponent();
            var controller = GpioController.GetDefault();
            _pin = controller.OpenPin(4);
         
        }

        private void btnOn_Click(object sender, RoutedEventArgs e)
        {
            _pin.SetDriveMode(GpioPinDriveMode.Output);
            _pin.Write(GpioPinValue.High);
        }

        private void btnOff_Click(object sender, RoutedEventArgs e)
        {
            _pin.Write(GpioPinValue.Low);
        }
    }
}

No comments:

Post a Comment