Project showcase

DogWatcher (MKR1000) © GPL3+

This is MKR1000/Windows 10 based device to keep your dog out of areas he should not be in by sounding an alarm and taking his photo.

  • 3,066 views
  • 3 comments
  • 10 respects

Components and supplies

Abx00004 iso both
Arduino MKR1000 & Genuino MKR1000
×1
R8326274 01
Raspberry Pi 2 Model B
You can use Windows 10 IoT on your Raspberry Pi 2, or just run it from any device, such as your Surface or laptop.
×1
13285 01
PIR Motion Sensor (generic)
×1
Adafruit industries ada1536 image
Buzzer
I wanted a buzzer that would get my dog's attention a little better, so I picked this one up from Amazon (http://www.amazon.com/9-15V-HYD-4218-Active-Electronic-Buzzer/dp/B01DOAADWM). The one from Amazon is probably too loud for most situations.
×1
Fairchild semiconductor tip120 image
Darlington High Power Transistor
I used this so I could drive a much larger buzzer using a 12V power supply. This is not needed if using a smaller buzzer that can be powered from the GPIO pin.
×1
12002 04
Breadboard (generic)
×1
08377 02 l
Resistor 330 ohm
×1

Apps and online services

Arduino StandardFirmataWifi
I loaded the MKR1000 with the StandardFirmataWifi example from the example library. The only change was so it would authenticate against my Wifi.
10
Microsoft Windows 10 IoT Core
This can be used on the Raspberry Pi 2 (or 3) or it can be run from any Windows 10 computer.
W9gt7hzo
Microsoft Azure
I used the Azure IoT Hub to send and receive messages from outside of the device running the DogWatcher software.
Vs2015logo
Microsoft Visual Studio 2015
I used the Community Edition.

About this project


DoggieLox (the name has been changed to protect the guilty) is known for making his way upstairs, sleeping on the beds, munching down on the cat's food and general mischief.

I wanted to get him to cooperate with something like a shock collar.  This simple device uses the Arduino MKR1000 with a buzzer and motion detector connected to it.  I am using Windows 10 to control the Arduino using Windows Remote Arduino.  I have a camera hooked up to my Windows 10 Surface so when the alarm goes off, it takes a picture so I know who the trespasser who.  I also log an alert with my Azure IoT hub so I can get notifications (I am currently using the Azure IoT hub explorer to send and receive messages).  I am also monitoring my Azure IoT Hub for messages to arm or disarm the dog watcher, so I can turn it off remotely.

I designed this so it will work either on my Surface or my Raspberry Pi 2.  The Raspberry Pi is a better option so the camera can be left on the stairs, or wherever needed, to catch the culprit in the act.  NOTE: there are a limited number of webcams that work with Windows 10 IoT on the Raspberry Pi, so check the hardware compatibility list.

Code

MainPage.XAMLVBScript
This is the code from the graphical interface (XAML) written in Visual Studio (Visual Basic)
<Page
    x:Class="DogWatcherHub.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:DogWatcherHub"
    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}">
        <Image x:Name="DoggieLoxImage" HorizontalAlignment="Left" Height="480" VerticalAlignment="Top" Width="640" Margin="374,10,0,0" Source="Assets/Dog.jpg"/>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="748" Margin="10,10,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="364"/>
        <Ellipse x:Name="Shape" Fill="Teal" HorizontalAlignment="Left" Height="200" Stroke="Black" VerticalAlignment="Top" Width="200" Margin="813,558,0,0" StrokeThickness="5"/>
    </Grid>
</Page>
MainPage (Visual Basic code)VBScript
This is the guts of the application. This is written Visual Basic (Visual Studio 2015 Community Edition).

This requires Windows-Remote-Arduino, as well as the Microsoft.Azure.Devices.Client.PCL. These are both installed from the NuGet Manager (or console). I have added the Windows IoT Extensions for the UWP primarily for future expansion on the Raspberry Pi 2.

Replace the Azure connect string with yours and specify the IP or HostName of you Arduino.

This establishes the connection with the Arduino, as well as the Azure IoT Hub. Once connected, it processes any messages from the Hub to ARM or disarm the DogWatcher.
Imports Microsoft.Maker.Firmata
Imports Microsoft.Maker.Serial
Imports Microsoft.Maker.RemoteWiring
Imports Microsoft.Azure.Devices.Client
Imports System.Text
Imports Windows.Media

' The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

''' <summary>
''' An empty page that can be used on its own or navigated to within a Frame.
''' </summary>
Public NotInheritable Class MainPage
    Inherits Page

    '=================================================================================================
    ' Device specific configuration.  Set your own values here.
    '-------------------------------------------------------------------------------------------------
    Private Const AzureIoTHubConnectionString = ""
    Private myHost As New Windows.Networking.HostName("x.x.x.x")
    Private myPort As UShort = 3030
    '=================================================================================================

    Private DogWatcherArmStatus As Boolean = False
    Private isArduinoReady As Boolean = False
    Private myMKR1000 As RemoteDevice
    Private myConnection As NetworkSerial

    Private Const buzzerPin As Byte = 4
    Private Const ledPin As Byte = 6
    Private Const motionPin As Byte = 1
    'Private Const motionPin As String = "A1"
    Dim isMoving As Boolean = True

    Dim mySolidColorBrush As New SolidColorBrush()
    Dim myStrokeColorBrush As New SolidColorBrush()

    Dim ArduinoTimer As New DispatcherTimer

    Private Sub MainPage_Loading(sender As FrameworkElement, args As Object) Handles Me.Loading
        mySolidColorBrush.Color = Windows.UI.Color.FromArgb(0, 0, 255, 0)
        Shape.Fill = mySolidColorBrush
        Shape.Fill.Opacity = 25

        myConnection = New Microsoft.Maker.Serial.NetworkSerial(myHost, myPort)
        myMKR1000 = New Microsoft.Maker.RemoteWiring.RemoteDevice(myConnection)
        myConnection.begin()
        AddHandler myConnection.ConnectionEstablished, AddressOf DogWatcherConnectionEstablished
        AddHandler myMKR1000.DeviceReady, AddressOf MKR1000Ready
        'AddHandler myMKR1000.DigitalPinUpdated, AddressOf Arduino_DigitalPinUpdated

        ReceiveDataFromAzure()
        TakePhoto()
        SendDataToAzure("Connected to Azure")
        DogWatcherARM()
        ArduinoTimer.Interval = TimeSpan.FromMilliseconds(100)
        AddHandler ArduinoTimer.Tick, AddressOf checkArduino
        ArduinoTimer.Start()
    End Sub

    Sub checkArduino()
        If isArduinoReady Then
            'Dim checkValue As UShort = myMKR1000.analogRead(motionPin)
            Dim checkValue As PinState = myMKR1000.digitalRead(motionPin)
            Dim currentlyMoving As Boolean = False
            If (checkValue = PinState.HIGH) Then
                currentlyMoving = True
            End If
            If isMoving <> currentlyMoving Then
                If (currentlyMoving) Then
                    mySolidColorBrush.Color = Windows.UI.Color.FromArgb(255, 255, 0, 0)
                    Shape.Fill = mySolidColorBrush
                Else
                    mySolidColorBrush.Color = Windows.UI.Color.FromArgb(127, 0, 0, 255)
                    Shape.Fill = mySolidColorBrush
                End If
                isMoving = currentlyMoving
                If DogWatcherArmStatus Then
                    If isMoving Then
                        myMKR1000.digitalWrite(buzzerPin, PinState.HIGH)
                        TakePhoto()
                    Else
                        myMKR1000.digitalWrite(buzzerPin, PinState.LOW)
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub MKR1000Ready()
        myMKR1000.pinMode(buzzerPin, PinMode.OUTPUT)
        myMKR1000.digitalWrite(buzzerPin, PinState.HIGH)
        myMKR1000.digitalWrite(buzzerPin, PinState.LOW)

        myMKR1000.pinMode(ledPin, PinMode.OUTPUT)
        myMKR1000.digitalWrite(ledPin, PinState.HIGH)
        myMKR1000.digitalWrite(ledPin, PinState.LOW)

        myMKR1000.pinMode(motionPin, PinMode.INPUT)
        isArduinoReady = True
    End Sub

    Private Sub DogWatcherConnectionEstablished()
        Debug.WriteLine("DogWatcher connection established")
        mySolidColorBrush.Color = Windows.UI.Color.FromArgb(127, 0, 255, 0)
        Shape.Fill = mySolidColorBrush
    End Sub

    Private Async Sub Arduino_DigitalPinUpdated(pin As Byte, pinValue As PinState)
        'Private Sub Arduino_DigitalPinUpdated(pin As Byte, pinValue As PinState)
        Debug.WriteLine("Event handler executing")
        Await Task.Delay(1)
        Try
            Debug.WriteLine(String.Format("Pin {0} changed to {1}", pin, pinValue))
        Catch ex As Exception
            Debug.WriteLine("Error: " & ex.Message)
        End Try
    End Sub


    Private Async Sub TakePhoto()
        Try
            Dim m_mediaCaptureMgr As New Windows.Media.Capture.MediaCapture
            Await m_mediaCaptureMgr.InitializeAsync()

            Dim m_photoStorageFile = Await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync("Intruder.jpg", Windows.Storage.CreationCollisionOption.GenerateUniqueName)
            Dim imageProperties As MediaProperties.ImageEncodingProperties = MediaProperties.ImageEncodingProperties.CreateJpeg()
            Await m_mediaCaptureMgr.CapturePhotoToStorageFileAsync(imageProperties, m_photoStorageFile)

            Dim photoStream = Await m_photoStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)
            Dim bmpimg = New BitmapImage()
            bmpimg.SetSource(photoStream)
            DoggieLoxImage.Source = bmpimg
        Catch ex As Exception
            Debug.WriteLine("Error: " & ex.Message)
        End Try
    End Sub

    Private Async Sub SendDataToAzure(Msg As String)
        Try
            Dim DeviceClient As DeviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHubConnectionString, TransportType.Http1)
            Dim msgToHub = New Message(Encoding.UTF8.GetBytes(Msg))
            Await DeviceClient.SendEventAsync(msgToHub)
        Catch ex As Exception
            Debug.WriteLine("Error: " & ex.Message)
        End Try
    End Sub

    Public Async Sub ReceiveDataFromAzure()
        Dim receivedMessage As New Message
        Dim messageData As String = ""

        Try
            Dim DeviceClient As DeviceClient = DeviceClient.CreateFromConnectionString(AzureIoTHubConnectionString, TransportType.Http1)
            Do While True
                receivedMessage = Await DeviceClient.ReceiveAsync

                If receivedMessage IsNot Nothing Then
                    messageData = Encoding.ASCII.GetString(receivedMessage.GetBytes())
                    textBox.Text += messageData & vbCrLf
                    If messageData.Contains("DogWatcher: ARM") Then
                        DogWatcherARM()
                    ElseIf messageData.Contains("DogWatcher: disarm") Then
                        DogWatcherDisarm()
                    End If
                    Await DeviceClient.CompleteAsync(receivedMessage)
                End If
            Loop
        Catch ex As Exception
            Debug.WriteLine("Error: " & ex.Message)
        End Try
    End Sub

    Private Sub DogWatcherARM()
        DogWatcherArmStatus = True
        myStrokeColorBrush.Color = Windows.UI.Color.FromArgb(255, 255, 0, 0)
        Shape.Stroke = myStrokeColorBrush
        myMKR1000.digitalWrite(ledPin, PinState.HIGH)
    End Sub

    Private Sub DogWatcherDisarm()
        DogWatcherArmStatus = False
        myStrokeColorBrush.Color = Windows.UI.Color.FromArgb(255, 0, 0, 63)
        Shape.Stroke = myStrokeColorBrush
        myMKR1000.digitalWrite(ledPin, PinState.LOW)
        myMKR1000.digitalWrite(buzzerPin, PinState.LOW)
    End Sub
End Class

Schematics

Schematic
The specific pins to connect to are documented in the source code. It will depend on what Arduino device you use to host your Firmata and connect to your devices.
Schematic

Comments

Similar projects you might like

Play Polyphonic Tones!

by nathan_ramanathan

  • 2,921 views
  • 3 comments
  • 8 respects

Arduino Tutorial: JARVIS v1 | How to make a Home Automation

Project tutorial by rahulkhanna

  • 5,650 views
  • 10 comments
  • 44 respects

Retro 1980s LED Clock/Count Up Timer

Project in progress by randtekk

  • 1,143 views
  • 4 comments
  • 6 respects

Heart Rate Monitor Using IoT

Project tutorial by Technopaths

  • 3,926 views
  • 2 comments
  • 18 respects

Arduino MEGA Guitar Pedal

Project tutorial by electrosmash

  • 6,884 views
  • 1 comment
  • 31 respects
Add projectSign up / Login