Saturday, December 9, 2017

PIR Motion Detection with Email Notification

How to build a Robot on an Arduino that can be activated by a PIR Motion Sensor:
by J.B. Wylzan
Project 36:  PIR Motion Sensor

This project shows how to use a PIR motion detector 
that sends an email whenever motion is detected.

Infrared Sensor




Hardware:
1 PIR Motion Sensor
1 Led (optional)
1 servo(optional)
1 speaker (optional)
connecting wires
breadboard
Ethernet Shield
Arduino R3 UNO board


Block Diagram:
Note: Although the sensor above is not a PIR but a Humidity and Temperature sensor
the schematic is still the same. (1=gnd, + = 5v,  S = ~)


Arduino Sketch:

/*
  iHacklab Motion Detection via Email
  This sketch was automatically generated by Temboo software. 
  A step by step tutorial is available on their site.
  The sketch varies defending on your arduino and ethernet shields.
 */
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <Temboo.h>
#include "TembooAccount.h"

byte ethernetMACAddress[] = ETHERNET_SHIELD_MAC;
EthernetClient client;

int numRuns = 1;   // Execution count, so this doesn't run forever
int maxRuns = 10;   // Maximum number of times the Choreo should be executed

void setup() {
  Serial.begin(9600);  
  delay(4000);
  while(!Serial);

  Serial.print("DHCP:");
  if (Ethernet.begin(ethernetMACAddress) == 0) {
    Serial.println("FAIL");
    while(true);
  }
  Serial.println("OK");
  delay(5000);

  Serial.println("Setup complete.\n");
}

void loop() {
  if (numRuns <= maxRuns) {
    Serial.println("Running SendEmail - Run #" + String(numRuns++));

    TembooChoreo SendEmailChoreo(client);

    // Invoke the Temboo client
    SendEmailChoreo.begin();

    // Set Temboo account credentials
    SendEmailChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendEmailChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendEmailChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set profile to use for execution
    SendEmailChoreo.setProfile("ver");

    // Identify the Choreo to run
    SendEmailChoreo.setChoreo("/Library/Google/Gmail/SendEmail");

    // Run the Choreo; when results are available,print them to serial
    SendEmailChoreo.run();

    while(SendEmailChoreo.available()) {
      char c = SendEmailChoreo.read();
      Serial.print(c);
    }
    SendEmailChoreo.close();
  }

  Serial.println("\nWaiting...\n");
  delay(30000); // wait 30 seconds between SendEmail

calls
}



/*
HEADER FILE:

IMPORTANT NOTE about TembooAccount.h

TembooAccount.h contains your Temboo account information and must be included
alongside your sketch. To do so, make a new tab in Arduino, call it TembooAccount.h,
and copy this content into it.
*/

#define TEMBOO_ACCOUNT "..."  // Your Temboo account name
#define TEMBOO_APP_KEY_NAME "..."  // Your Temboo app key name
#define TEMBOO_APP_KEY"..."  // Your Temboo app key

#define ETHERNET_SHIELD_MAC {0x0D, 0xE0, 0xAD, 0x0B, 0xE0, 0xEF}

/*
Keeping your account information in a separate file means you can share the
main .ino file without worrying that you forgot to delete your credentials.
*/


Challenges:

1. Add a blinking Led to indicate motion is detected.
2. Play a warning tone when motion is detected.
3. Use other types of sensors
4. Send yourself an hourly temperature/humidity reports via email.

Python Code:

/* You need to install Python on your PC.
 The example code is public domain */

import time
import serial
import smtplib

TO = 'putyour@email.here'
GMAIL_USER = 'putyour@email.here'
GMAIL_PASS = 'putyourpasswordhere'

SUBJECT = 'Intrusion!!'
TEXT = 'Your PIR sensor detected movement'

ser = serial.Serial('COM4', 9600)

def send_email():
    print("Sending Email")
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(GMAIL_USER, GMAIL_PASS)
    header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
    header = header + '\n' + 'Subject:' + SUBJECT + '\n'
    print header
    msg = header + '\n' + TEXT + ' \n\n'
    smtpserver.sendmail(GMAIL_USER, TO, msg)
    smtpserver.close()
 
while True:
    message = ser.readline()
    print(message)
    if message[0] == 'M' :
        send_email()
    time.sleep(0.5)




Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 







No comments:

Post a Comment