Berndt Hamboeck
Published © LGPL

Windows 10 IoT - Don't move or we grab a pic from you!

WinIoT on the Raspberry Pi, using the HC-SR501 infrared sensor and a RGB LED to detect movement and take a picture from a D-Link DCS-932.

BeginnerWork in progress3,964
Windows 10 IoT - Don't move or we grab a pic from you!

Things used in this project

Hardware components

4 Pin Terminals RGB 3-Color LED
×1
HC-SR501 Human Sensor Module Pyroelectric Infrared
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core

Story

Read more

Schematics

Fritzing file for the RGB-Infrared-Sensor Project

download fritzing and load the project

Code

MainPage.xaml.cs

C#
Create a new Universal APP, ad the WinIOT reference, add an image to MainPage.xaml.cs and use the code below for the code behind file.
using System;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Devices.Gpio;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace BlinkingLed
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private bool _ledStatus = false;
        private const int LED_PIN = 5;
        private GpioPin _pinLed;

        private const int LED_R_PIN = 23;
        private GpioPin _pinRLed;
        private const int LED_G_PIN = 24;
        private GpioPin _pinGLed;

        private const int DISTANCE_PIN = 6;
        private GpioPin _pinDistance;
        public MainPage()
        {
            this.InitializeComponent();

            InitGPIO();

            Unloaded += MainPage_Unloaded;
        }

        private void InitGPIO()
        {
            var gpio = GpioController.GetDefault();

            _pinLed = gpio.OpenPin(LED_PIN);
            _pinLed.Write(GpioPinValue.High);
            _pinLed.SetDriveMode(GpioPinDriveMode.Output);

            _pinRLed = gpio.OpenPin(LED_R_PIN);
            _pinRLed.Write(GpioPinValue.High);
            _pinRLed.SetDriveMode(GpioPinDriveMode.Output);

            _pinGLed = gpio.OpenPin(LED_G_PIN);
            _pinGLed.Write(GpioPinValue.Low);
            _pinGLed.SetDriveMode(GpioPinDriveMode.Output);


            _pinDistance = gpio.OpenPin(DISTANCE_PIN);
            _pinDistance.SetDriveMode(GpioPinDriveMode.Input);
            _pinDistance.ValueChanged += _pinDistance_ValueChanged;
        }

        private void _pinDistance_ValueChanged(GpioPin sender, 
            GpioPinValueChangedEventArgs args)
        {
            var isOn = args.Edge == GpioPinEdge.FallingEdge;

            _pinLed.Write(isOn ? GpioPinValue.High : GpioPinValue.Low);

            _pinRLed.Write(isOn ? GpioPinValue.High : GpioPinValue.Low);
            _pinGLed.Write(!isOn ? GpioPinValue.High : GpioPinValue.Low);

            if(isOn)
            {
                takeImage();
            }
        }

        private async void takeImage()
        {
            try
            {
                var url = "http://10.0.0.12/image.jpg";

                HttpClient client = new HttpClient();
                var byteArray = Encoding.UTF8.GetBytes("admin:");
                client.DefaultRequestHeaders.Authorization = 
                    new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", 
                    Convert.ToBase64String(byteArray));

                var result = await client.GetByteArrayAsync(url);
                ByteToImage(result);
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }
        }

        public async void ByteToImage(byte[] imageData)
        {

            InMemoryRandomAccessStream randomAccessStream = new InMemoryRandomAccessStream();
            var op = randomAccessStream.WriteAsync(imageData.AsBuffer());
            op.Completed = (info, status) => {

                randomAccessStream.Seek(0);

                Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    BitmapImage biImg = new BitmapImage();
                    biImg.SetSource(randomAccessStream);

                    ImageSource imgSrc = biImg as ImageSource;
                    currentImage.Source = imgSrc;
                });
            };
        }

        private void MainPage_Unloaded(object sender, object args)
        {
            // Cleanup
            _pinLed.Dispose();
            _pinRLed.Dispose();
            _pinGLed.Dispose();
            _pinDistance.Dispose();
        }

    }
}

Credits

Berndt Hamboeck

Berndt Hamboeck

5 projects • 17 followers
MCT, C# Developer, Nerd

Comments