Adaptable Sensor and Notification System

A easy to use adaptable sensor and notification system that is designed to be used as a temporary change of state notifier.

Adaptable Sensor and Notification System

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
× 1
SW-420 Motion Sensor Module Vibration Switch Alarm Sensor Module for Arduino
× 1
Light Dependent Resistor
× 1
Switch
× 4
9V battery (generic)
9V battery (generic)
× 1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
UWP program to control the arduino and send the email notification.
Firmata

Story

Representative overall sketch

This is a schematic sketch of the overall set-up. The pins chosen do not matter here.
Adaptable Sensor and Notification System schematics

Code

MainPage.xaml.cs file for the UWP Windows 100 App. Use NuGet to install EASendMailRT which I used to be able to send emails.
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System.Diagnostics;
using Microsoft.Maker.Serial;
using Microsoft.Maker.RemoteWiring;
using EASendMailRT;


namespace AdaptableSensorRig
{

    public sealed partial class MainPage : Page
    {
        PinState pbPinValue = PinState.LOW;
        PinState knockSensorValue = PinState.LOW;

        private const byte LED = 6;
        private const byte KNOCK_SENSOR = 2;
        private const byte PIR_SENSOR = 3;
        private const string LDR_SENSOR = "A0";
        private const byte KNOCK_SENSOR_SWITCH = 6;
        private const byte PIR_SENSOR_SWITCH = 7;
        private const byte LDR_SENSOR_SWITCH = 8;
        public const string GMAIL_SERVER = "smtp.gmail.com";
        private const string ARDUINO_HOST_IP = "192.168.1.5";
        private const int ARDUINO_CONNECTION_PORT = 3030;
        public const int PORT = 587;
        private DispatcherTimer pbPollTimer;
        RemoteDevice arduino;
        NetworkSerial netWorkSerial;

        private bool knockSensorActive, pirSensorActive, ldrSensorActive;

        public MainPage()
        {
            this.InitializeComponent();
            this.InitArduino();
            this.pbPollTimer = new DispatcherTimer();
            this.pbPollTimer.Interval = TimeSpan.FromMilliseconds(250);
            this.pbPollTimer.Tick += PBTimer_Tick;
            this.pbPollTimer.Stop();
        }


        public void InitArduino()
        {
            //Establish a network serial connection. change it to the right IP address and port
            netWorkSerial = new NetworkSerial(new Windows.Networking.HostName(ARDUINO_HOST_IP), ARDUINO_CONNECTION_PORT);

            //Create Arduino Device
            arduino = new RemoteDevice(netWorkSerial);

            //Attach event handlers
            netWorkSerial.ConnectionEstablished += NetWorkSerial_ConnectionEstablished;
            netWorkSerial.ConnectionFailed += NetWorkSerial_ConnectionFailed;

            //Begin connection
            netWorkSerial.begin(115200, SerialConfig.SERIAL_8N1);
            knockSensorActive = false;
            pirSensorActive = false;
            ldrSensorActive = false;
        }


        private void NetWorkSerial_ConnectionEstablished()
        {
            Debug.WriteLine("Connection established");
            OnButton.IsEnabled = true;
            OffButton.IsEnabled = true;

            //Testing LED
            arduino.pinMode(LED, PinMode.OUTPUT);

            //Set inputs for detectors and associated switches
            arduino.pinMode(KNOCK_SENSOR, PinMode.INPUT);
            arduino.pinMode(KNOCK_SENSOR_SWITCH, PinMode.INPUT);
            arduino.pinMode(PIR_SENSOR, PinMode.INPUT);
            arduino.pinMode(PIR_SENSOR_SWITCH, PinMode.INPUT);
            arduino.pinMode(LDR_SENSOR, PinMode.ANALOG);
            arduino.pinMode(LDR_SENSOR_SWITCH, PinMode.INPUT);

            this.pbPollTimer.Start();
        }
		
        
        
		
        private void PBTimer_Tick(object sender, object e)
        {
            CheckSwitchValues(); 
        }

        PinState knockSensorSetValue = PinState.LOW;
        PinState pirSensorSetValue = PinState.LOW;
        ushort ldrSensorSetValue = 0;

        private void CheckSwitchValues()
        {
            //Read switches
            PinState knockSensorSwitchValue = arduino.digitalRead(KNOCK_SENSOR_SWITCH);
            PinState pirSensorSwitchValue = arduino.digitalRead(PIR_SENSOR_SWITCH);
            PinState ldrSensorSwitchValue = arduino.digitalRead(LDR_SENSOR_SWITCH);

            //Read sensors
            PinState knockSensorValue = arduino.digitalRead(KNOCK_SENSOR);
            PinState pirSensorValue = arduino.digitalRead(PIR_SENSOR);
            var ldrSensorValue = arduino.analogRead(LDR_SENSOR);

            if (knockSensorSwitchValue == PinState.HIGH && !knockSensorActive)
            {
                knockSensorActive = true;
                knockSensorSetValue = arduino.digitalRead(KNOCK_SENSOR);
            }

            if (knockSensorValue != knockSensorSetValue && knockSensorActive)
            {
                SendEmail("Knock Sensor Tripped!", "The knock sensor was tripped from the base state at " + DateTime.Now.ToLocalTime());
                knockSensorSetValue = knockSensorValue;
            }

            if (pirSensorSwitchValue == PinState.HIGH && !pirSensorActive)
            {
                pirSensorActive = true;
                pirSensorSetValue = arduino.digitalRead(PIR_SENSOR);
            }

            if (pirSensorValue != pirSensorSetValue && pirSensorActive)
            {
                SendEmail("PIR Sensor Tripped!", "The PIR sensor was tripped from the base state at " + DateTime.Now.ToLocalTime());
				pirSensorSetValue = pirSensorValue;
			}

            if (ldrSensorSwitchValue == PinState.HIGH && !ldrSensorActive)
            {
                ldrSensorActive = true;
                ldrSensorSetValue = arduino.analogRead(LDR_SENSOR);
            }

            if(((ldrSensorSetValue * 1.1) >= ldrSensorValue) || (ldrSensorSetValue * 0.9) <= ldrSensorValue)
            {
                SendEmail("LDR Sensor Tripped!", "The LDR sensor was tripped              from the base state at " + DateTime.Now.ToLocalTime());
				ldrSensorSetValue = ldrSensorValue;
			}

			if(knockSensorSwitchValue == PinState.LOW && knockSensorActive)
			{
				knockSensorActive = false;
			}
			
			if(pirSensorSwitchValue == PinState.LOW && pirSensorActive)
			{
				pirSensorActive = false;
			}
			
			if(ldrSensorSwitchValue == PinState.LOW && ldrSensorActive)
			{
				ldrSensorActive = false;
			}

        }

        private async void SendEmail(string emailSubject, string emailBody)
        {
            try
            {
                SmtpMail mailBody = new SmtpMail("TryIt");
                SmtpClient smtpClient = new SmtpClient();
                mailBody.From = new MailAddress("<enter email address>");
                mailBody.To.Add(new MailAddress("<enter email address>"));

                mailBody.Subject = emailSubject;
                mailBody.TextBody = emailBody;

                SmtpServer gmailServer = new SmtpServer("smtp.gmail.com");

                gmailServer.User = "<enter email address>";
                gmailServer.Password = "<enter password"; //May need to get app                 -specific password here for 2 factor authentication.
                gmailServer.Port = PORT;
                gmailServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                await smtpClient.SendMailAsync(gmailServer, mailBody);

                Email_Status.Text = "Email sent!";
                Debug.WriteLine("Email should have been sent");
            }

            catch (Exception ex)
            {
                Debug.WriteLine("Unable to send the email. Error : " + ex);
                Email_Status.Text = "Error with email";
            }
        }


        private void NetWorkSerial_ConnectionFailed(string message)
        {
            Debug.WriteLine("Arduino Connection Failed: " + message);
        }
		
		//For testing only
        private void OnButton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(6, PinState.HIGH);
 
        }
		
		//For testing only
        private void OffButton_Click(object sender, RoutedEventArgs e)
        {
            arduino.digitalWrite(6, PinState.LOW);
        }
    }
}

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top