Arduino Lesson 17. Email Sending Movement Detector

Similar documents
Arduino Lesson 13. DC Motors. Created by Simon Monk

Arduino Lesson 14. Servo Motors

Arduino Lesson 0. Getting Started

Arduino Lesson 1. Blink

Arduino Lesson 5. The Serial Monitor

Arduino Lesson 9. Sensing Light

Arduino Lesson 16. Stepper Motors

Arduino Lesson 4. Eight LEDs and a Shift Register

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable

Wireless Security Camera with the Arduino Yun

Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor

Adafruit's Raspberry Pi Lesson 7. Remote Control with VNC

Adafruit's Raspberry Pi Lesson 5. Using a Console Cable. Created by Simon Monk

SSH to BeagleBone Black over USB

Sending an SMS with Temboo

Adafruit's Raspberry Pi Lesson 11. DS18B20 Temperature Sensing

Adafruit's Raspberry Pi Lesson 1. Preparing an SD Card for your Raspberry Pi

Adafruit's Raspberry Pi Lesson 6. Using SSH

Adafruit NFC/RFID on Raspberry Pi

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

Playing sounds and using buttons with Raspberry Pi

Introducing the Adafruit Bluefruit LE Sniffer

DS1307 Real Time Clock Breakout Board Kit

By: John W. Raffensperger, Jr. Revision: 0.1 Date: March 14, 2008

Set up and Blink - Simulink with Arduino

SMS Alarm Messenger. Setup Software Guide. SMSPro_Setup. Revision [Version 2.2]

C4DI Arduino tutorial 4 Things beginning with the letter i

Department of Engineering Science. Understanding FTP

Short Manual Intellect v SP2 module Unipos Contents:

Iridium Extreme TM Satellite Phone. Data Services Manual

Vmed QUICKSTART PC-DISPLAY INSTALLATION & PC-Display and Bluetooth Instructions

Network Probe User Guide

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Adafruit NFC/RFID on Raspberry Pi. Created by Kevin Townsend

PRODUCTIVITY THROUGH INNOVATION 600 CONTROL DIRECT DRIVE TECHNICAL/OPERATION MANUAL

1. Product Information

Practice Fusion API Client Installation Guide for Windows

How To Sync Google Drive On A Mac Computer With A Gmail Account On A Gcd (For A Student) On A Pc Or Mac Or Mac (For An Older Person) On An Ipad Or Ipad (For Older People) On

Online Backup Client User Manual Linux

Waspmote. Quickstart Guide

r-one Python Setup 1 Setup 2 Using IDLE 3 Using IDLE with your Robot ENGI 128: Introduction to Engineering Systems Fall, 2014

Installing ABACUS ELECTRICS USB Optical Probes under Windows 7

RecoveryVault Express Client User Manual

Adafruit BME280 Humidity + Barometric Pressure + Temperature Sensor Breakout

mybullard Personal Control Panel User Guide

Online Backup Client User Manual

Site Monitor. Version 5.3

Online Backup Linux Client User Manual

Monitor Your Home With the Raspberry Pi B+

UPSMON PRO Linux --- User Manual

Workshop Intel Galileo Board

Online Backup Client User Manual

Motion Detecting Camera Security System with Notifications and Live Streaming Using Raspberry Pi

Using an IR Remote with a Raspberry Pi Media Center

Instructions for the installation of drivers and data reading software (TOOLBOX 4) The simple and reliable way to measure radioactivity.

How to Install Applications (APK Files) on Your Android Phone

B&K Precision 1785B, 1786B, 1787B, 1788 Power supply Python Library

WebIOPi. Installation Walk-through Macros

Remote Viewer Recording Backup

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

Using the T&D Thermo App with TR-7wf Data Loggers

TAS (Tecnosoft Alarm System)

Board also Supports MicroBridge

Guide to Installing BBL Crystal MIND on Windows 7

A REST API for Arduino & the CC3000 WiFi Chip

Internet of Things with the Arduino Yún

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

Tiny Arduino Music Visualizer

e- storage Mail Archive

Building a Basic Communication Network using XBee DigiMesh. Keywords: XBee, Networking, Zigbee, Digimesh, Mesh, Python, Smart Home

ScanWin Installation and Windows 7-64 bit operating system

Initial Setup of Mac Mail with IMAP for OS X Lion

SmartWatch Eco/Eco Compact

Configuring IP cameras for Alarm Recording

Quick Installation Guide Network Management Card

BOTTOM UP THINKING SETUP INSTRUCTIONS. Unique businesses require unique solutions CLIENT GUIDE

Initial Setup of Microsoft Outlook with Google Apps Sync for Windows 7. Initial Setup of Microsoft Outlook with Google Apps Sync for Windows 7

E-Blocks Easy Internet Bundle

GUARD SENIOR. manual

Creating a Java application using Perfect Developer and the Java Develo...

VisiCount Installation. Revised: 8/28/2012

Look for the 'BAERCOM Instruction Manual' link in Blue very near the bottom.

TSL2561 Luminosity Sensor

How to move to your account with MAC Mail

Amcrest 960H DVR Quick Start Guide

This information is provided for informational purposes only.

Arduino Wifi shield And reciever. 5V adapter. Connecting wifi module on shield: Make sure the wifi unit is connected the following way on the shield:


Installing Java (Windows) and Writing your First Program

HYDROSOFT Version INSTALLATION AND OPERATING

Field Manager Mobile Worker User Guide for RIM BlackBerry 1

Intro to Intel Galileo - IoT Apps GERARDO CARMONA

FORWARDING (directed to a non-gcccd account) Revised 3/22/13

Resolving USB Driver Problems

Format the USB Thumb Drive for Ghost

All About Arduino Libraries

First Time On-Campus VLab Setup Windows XP Edition

Local Caching Servers (LCS): User Manual

Transcription:

Arduino Lesson 17. Email Sending Movement Detector Created by Simon Monk Last updated on 2014-04-17 09:30:23 PM EDT

Guide Contents Guide Contents Overview Parts Part Qty Breadboard Layout Arduino Code Installing Python and PySerial Install Python on Windows Install PySerial Python Code Other Things to Do 2 3 4 4 4 7 8 10 10 12 14 17 Page 2 of 17

Overview In this lesson you will learn how to use a PIR movement detector with an Arduino and to have the Arduino communicate with a Python program running on your computer to send an email whenever movement is detected by the sensor. The Arduino is the heart of this project. It 'listens' to the PIR sensor and when motion is detect, instructs the computer via the USB port to send an email. Page 3 of 17

Parts To build the project described in this lesson, you will need the following parts. You will also need a computer with an Internet connection (so you can send email thru it)! Part Qty PIR Sensor 1 Page 4 of 17

Arduino Uno R3 1 Half-sized Breadboard 1 Jumper wire pack Page 5 of 17

Page 6 of 17

Breadboard Layout The only thing that you are connecting to the Arduino is the PIR sensor, so you could if you prefer simply push the wires attached to the PIR sensor directly into the Arduino board. However, the wires from the sensor, are a bit loose in the Arduino sockets so it is probably better to use the breadboard layout below. Page 7 of 17

Arduino Code The Arduino will send a message over USB Serial connection whenever movement is detected. However, this could have the potential to generate a lot of emails. For this reason the Arduino sends a different message if its too soon to send another email. int pirpin = 7; int minsecsbetweenemails = 60; // 1 min long lastsend = -minsecsbetweenemails * 1000l; void setup() { pinmode(pirpin, INPUT); Serial.begin(9600); } void loop() { long now = millis(); if (digitalread(pirpin) == HIGH) { if (now > (lastsend + minsecsbetweenemails * 1000l)) { Serial.println("MOVEMENT"); lastsend = now; } else { Serial.println("Too soon"); } } delay(500); } The variable minsecsbetweenemails can be changed to whatever you feel is a reasonable value. Here it is set to 60 seconds, so emails will not be sent at a rate of more than one a minute. To keep track of when the last request to send an email was sent, a variable lastsend is used. This is initialized to a negative number, equal to the negative of the number of milliseconds specified in the minsecsbetweenemails variable. This ensures that the PIR can be triggered immediately that the Arduino sketch starts. Within the loop, the function millis() is used to get the number of milliseconds since the Page 8 of 17

Arduino started and compare it with that last time the alarm was triggered and only if it is more than the specified number of seconds since last time does it send the message MOVEMENT. Otherwise even though movement has been detected, it just sends the message Too soon. Before you link things up to your Python program, you can test the Arduino setup by just opening the Serial Monitor on the Arduino IDE. Page 9 of 17

Installing Python and PySerial If you are using a Mac or Linux computer, the Python is already installed. If you are using Windows, then you will need to install it. In either case, you will also need to install the PySerial library to allow communication with the Arduino. Install Python on Windows To install Python on Windows, download the installer from http://www.python.org/getit/ (http://adafru.it/azg). This project was built using Python 2.7.3. There are some reported problems with PySerial on Windows, using Python 3, so stick to Python 2. Once Python is installed, you will find a new Program Group on your Start menu. However, we are going to make a change to Windows to allow you to use Python from the Command Prompt. You will need this to be able to install PySerial. We are going to add something to the PATH environment variable. Page 10 of 17

To do this, you need to go to the Windows Control panel and find the System Properties control. Then click on the button labelled Environment Variables and in the window that popsup select Path in the bottom section (System Variables). Click Edit and then at the end of the Variable Value without deleting any of the text already there, add the text: ;C:\Python27 Don't forget the ";" before the new bit! To test that it worked okay, start a new Command Prompt (DOS Prompt) and enter the command python. You should see something like this: Page 11 of 17

Install PySerial Whatever your operating system, download the.tar.gz install package for PySerial 2.6 from https://pypi.python.org/pypi/pyserial (http://adafru.it/azh) This will give you a file called: pyserial-2.6.tar.gz If you are using windows you need to uncompress this into a folder. Unfortunately, it is not a normal zip file, so you may need to download a tool such as 7-zip (http://www.7- zip.org/ (http://adafru.it/azi)). If you are using a Mac or Linux computer, then open a Terminal session, 'cd' to wherever you downloaded pyserial-2.6.tar.gz and then issue the following command to unpack the installation folder. $ tar -xzf pyserial-2.6.tar.gz The rest of the procedure is the same whatever your operating system. Use you Comamnd Prompt / Terminal session and cd into the pyserial-2.6 folder, then run the command: sudo python setup.py install Page 12 of 17

Page 13 of 17

Python Code Now, you need to create the Python program. To do this, copy the code below into a file called movement.py. On Mac / Linux you can use the nano editor, on Windows, it is probably easiest to make the file using the Python editor 'IDLE which is available from the Python program group on your start menu. 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) Before you run the Python program, there are some configuration changes that you need to make. These are all up near the top of the file. The program assumes that the emails are being set from a gmail account. So, if you don't have one, you might like to make yourself one, even if it is just for this project. Page 14 of 17

Change the email address next to TO to the email that you want to receive the notifications. This does not have to be your email address, but probably will be. Change the email address next to GMAIL_USER to the email address of your gmail address and alter the password on the next line to the password you use to retrieve your emails. If you want to, you can also change the subject line and text of the message to be sent, on the lines that follow. You will also need to set the serial port of the Arduino by editing the line below: ser = serial.serial('com4', 9600) For Windows, this will be something like COM4 for Mac and Linux, something like /dev/tty.usbmodem621. You can find this by openning the Arduino IDE and in the bottom right corner, it will show you the port that is connected to the Arduino. When you have made these changes, you can run the program from Command Prompt / Terminal with the command: python movement.py When a movement is triggered you should get some trace like this and shortly after an email will arrive in your inbox. Page 15 of 17

Notice also the Too soon messages. Page 16 of 17

Other Things to Do Now that you have a means of sending email from your Arduino, this opens up all sorts of possibilities You could add different types of sensor, and perhaps send yourself hourly temperature reports by email. The PIR sensor could be used directly with the Arduino to play a warning tone or turn on LEDs. About the Author. Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino (http://adafru.it/1019), 30 Arduino Projects for the Evil Genius (http://adafru.it/868) and Programming the Raspberry Pi (http://adafru.it/am5). Last Updated: 2014-04-17 09:30:24 PM EDT Page 17 of 17