Home > Projects > Adaptable Sensor and Notification System

Adaptable Sensor and Notification System

Summary of Adaptable Sensor and Notification System


This project is a temporary state-change notification system using an Arduino MKR1000 and a Windows 10 UWP app. It detects changes via PIR, vibration, or light sensors and sends email alerts through Firmata over WiFi. The system allows users to set baseline values by toggling switches, triggering notifications only when sensor readings deviate significantly from the base state.

Parts used in the Adaptable Sensor Rig:

  • Arduino MKR1000
  • PIR Motion Sensor (generic)
  • SW-420 Motion Sensor Module Vibration Switch Alarm Sensor Module for Arduino
  • Light Dependent Resistor
  • Switch
  • 9V battery (generic)
  • Microsoft Windows 10 IoT Core UWP program
30 min read

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);
        }
    }
}

Quick Solutions to Questions related to Adaptable Sensor Rig:

  • How does the system detect a change of state?
    The system reads the current value of the selected sensor, sets it as a base value, and triggers a notification if the reading changes significantly.
  • Can I use different sensors with this setup?
    Yes, the project is easily expandable with simple additions to the code beyond the included PIR, Knock, and LDR sensors.
  • What protocol is used for the connection between the Arduino and the computer?
    Connection is done via WiFi using the Firmata protocol.
  • How do I reset the sensor after a trigger?
    To reset the sensor, you must set the corresponding switch to LOW again.
  • Does the Arduino connect to WiFi automatically?
    Yes, the system connects automatically to WiFi upon power-up.
  • What software is required on the controlling device?
    A Windows 10 Universal App needs to be launched on a Windows 10 device to act as the controlling device.
  • How are email notifications sent?
    The system sends an email to a defined address using the EASendMailRT library configured with SMTP settings.
  • Is a separate control computer necessary?
    While the current setup uses a desktop computer, the author suggests a Raspberry Pi 2 could be a better fit to avoid needing a separate control computer.

About The Author

Avatar of Ibrar Ayyub

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
Use Arduino for Projects

Free Arduino projects, circuits, code and tutorials. Pick a board, pick a sensor, build something.

Scroll to Top