🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 17. Email Sending Movement Detector
  • Overview
  • Parts
  • Part Qty
  • Breadboard Layout
  • Arduino Code
  • Installing Python and PySerial
  • Install Python on Windows
  • Install PySerial
  • Python Code
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 17 Email Sending Movement Detector

PreviousLesson 16 Stepper MotorsNextArm Robot

Last updated 1 year ago

Arduino Lesson 17. Email Sending Movement Detector

Created by Simon Monk

Last updated on 2022-12-01 01:56:46 PM EST

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

Breadboard Layout 7

Arduino Code 7

Installing Python and PySerial 9

  • Install Python on Windows

  • Install PySerial

Python Code 12

Other Things to Do 14

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.

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)!

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.

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 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.

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

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.

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 pops-up 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:

Install PySerial

This will give you a file called: pyserial-2.6.tar.gz

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

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.

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.

Notice also the “Too soon” messages.

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.

Part Qty

1

1

1

To install Python on Windows, download the installer from ().

Whatever your operating system, download the .tar.gz install package for PySerial 2.6 from ()

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 ( ()).

PIR Sensor
Arduino Uno R3
Half-sized Breadboard
Jumper wire pack
http://www.python.org/
getit/
https://pypi.python.org/pypi/pyserial
http://www.7-
zip.org/