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
Comments