Nikita Polyakov
Published © MIT

IoT Door Bell 101 - The basics

Replace your dumb doorbell with a smart one! Push Button get Push Notification, LED light. Windows 10 IoT Core + Azure Notification Hub.

BeginnerShowcase (no instructions)2 hours5,206
IoT Door Bell 101 - The basics

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
Running Windows 10 IoT Core
×1
Generic Push Button
Simple button available in most Starter Kits
×1
LED (generic)
LED (generic)
×1
Breadboard (generic)
Breadboard (generic)
For LED and Button mounting
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Microsoft Azure
Microsoft Azure

Story

Read more

Schematics

Microsoft's Push Button Sample Schematic

MIT Lic. Microsoft 2015

Code

Modified Push Button Sample Code

C#
Original MIT Lic. Microsoft 2015
// Copyright (c) Microsoft. All rights reserved.
//using Microsoft.Azure.NotificationHubs;
using System;
using Windows.Devices.Gpio;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace PushButton
{
    public sealed partial class MainPage : Page
    {
        private const int LED_PIN = 6;
        private const int BUTTON_PIN = 5;
        private GpioPin ledPin;
        private GpioPin buttonPin;
        private GpioPinValue ledPinValue = GpioPinValue.High;
        private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);
        private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);

        public MainPage()
        {
            InitializeComponent();
            InitGPIO();
        }

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

            // Show an error if there is no GPIO controller
            if (gpio == null)
            {
                GpioStatus.Text = "There is no GPIO controller on this device.";
                return;
            }

            buttonPin = gpio.OpenPin(BUTTON_PIN);
            ledPin = gpio.OpenPin(LED_PIN);

            // Initialize LED to the OFF state by first writing a HIGH value
            // We write HIGH because the LED is wired in a active LOW configuration
            ledPin.Write(GpioPinValue.High); 
            ledPin.SetDriveMode(GpioPinDriveMode.Output);

            // Check if input pull-up resistors are supported
            if (buttonPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
                buttonPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            else
                buttonPin.SetDriveMode(GpioPinDriveMode.Input);

            // Set a debounce timeout to filter out switch bounce noise from a button press
            buttonPin.DebounceTimeout = TimeSpan.FromMilliseconds(50);

            // Register for the ValueChanged event so our buttonPin_ValueChanged 
            // function is called when the button is pressed
            buttonPin.ValueChanged += buttonPin_ValueChanged;

            GpioStatus.Text = "GPIO pins initialized correctly.";
            
            ledPin.Write(GpioPinValue.Low); // turn the Door Bell Light on!
        }

        private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
        {
            // toggle the state of the LED every time the button is pressed
            if (e.Edge == GpioPinEdge.FallingEdge)
            {
                ledPinValue = (ledPinValue == GpioPinValue.Low) ?
                    GpioPinValue.High : GpioPinValue.Low;
                ledPin.Write(ledPinValue);
            }
            else if (e.Edge == GpioPinEdge.RisingEdge) 
            {
                ledPinValue = (ledPinValue == GpioPinValue.Low) ?
                    GpioPinValue.High : GpioPinValue.Low;
                ledPin.Write(ledPinValue);
            }

            // need to invoke UI updates on the UI thread because this event
            // handler gets invoked on a separate thread.
            var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
                if (e.Edge == GpioPinEdge.FallingEdge)
                {
                    ledEllipse.Fill = (ledPinValue == GpioPinValue.Low) ? 
                        redBrush : grayBrush;
                    GpioStatus.Text = "Button Pressed";

                    SendNotificationsAsync(); // Send Push Notification
                }
                else
                {
                    ledEllipse.Fill = (ledPinValue == GpioPinValue.Low) ?
                        redBrush : grayBrush;
                    GpioStatus.Text = "Button Released";
                }
            });
        }

        private static async void SendNotificationsAsync()
        {
            //NotificationHubClient hub = NotificationHubClient
            //    .CreateClientFromConnectionString("<connection string with full access>", "<hub name>");
            //var toast = doorbellToast;
            //await hub.SendWindowsNativeNotificationAsync(toast);
            //var tile = doorbellTile;
            //await hub.SendWindowsNativeNotificationAsync(tile);
        }

        string doorbellTile = @"<?xml version='1.0' encoding='utf-8'?>
<tile>
  <visual>
    <binding template = 'TileSquareText02' >
      <text id='1'>Door Bell Rang!</text>
      <text id='2'>On September 18th 2015 at 11:11 AM</text>
    </binding>  
  </visual>
</tile>";

        string doorbellToast = @"<?xml version = '1.0' encoding='utf-8'?>
<toast>
  <visual>
    <binding template = 'ToastText01' >
      < text id='1'>Door Rang!</text>
    </binding>
  </visual>
</toast>";

    }
}

Push Client App

C#
Inspired by Mike Tualtys
using Microsoft.WindowsAzure.Messaging;
using System;
using Windows.Networking.PushNotifications;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace IoTDoorBell.ClientApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void NotificationButton_Click(object sender, RoutedEventArgs e)
        {
            NotificationButton.IsEnabled = false; // Turn-Off button
            NotificationButton.Content = "Registering with Azure..."; // Let the user know what we are doing

            var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            var hub = new NotificationHub("<hub name>", "<hub connection string key>");
            
            var result = await hub.RegisterNativeAsync(channel.Uri);

            NotificationButton.Content = "Registered for Notifications. Close me."; // Let the user we ready
            
        }
    }
}

Credits

Nikita Polyakov

Nikita Polyakov

3 projects • 8 followers
.NET Developer 10+ years Past Microsoft MVP in Mobility 5 years Published Author on Mobility 1 book Microsoft Dynamics CRM Mobile & Portal Solutions

Comments