Arduino – protecting the Minions

Posted: April 8, 2015 in technical
Tags: , ,

1. I have minions!

2. My minions are AWESOME!

3. While I am away from my minions, I wanna make sure that they are safe and happy.

Hence, I am preparing a super (duper) tiny project to alert me of movement around my minions (as I said, only I am allowed around my minions).

Parts

1. Motion detector (PIR sensor)

2. Wires (the sensor needs 3: ground, out, vcc)

3. Arduino board

4. I had Python on my mac-mac, but I also had to install pyserial, in order to communicate to the Arduino board, via the serial port. In my case, the Arduino board is reachable via /dev/cu.usbmodem1421, on other systems it’s probably different.

Picture

20150408_200557

A bit of explanation

Basically, the “plan” is to log something to Serial port from Arduino, everytime I detect movement around my Minions. If that happens, I log “Movement!” to the Serial port. Then, from Python, I monitor the serial port, and if I read “Movement!”, then I send an email to myself, using my Gmail account, alerting me that the safety of the Minions has been compromised 🙂

Code

1. Arduino code

int pirPin = 7;

int spamInterval = 60; // send email max every 1 min

long lastSend = -spamInterval * 1000l; // measured in milliseconds

void setup()
{
pinMode(pirPin, INPUT);
Serial.begin(9600);
}

void loop()
{
long now = millis();
if (digitalRead(pirPin) == HIGH)
{
if (now > (lastSend + spamInterval * 1000l))
{
Serial.println(“Movement!”);
lastSend = now;
}
else
{
Serial.println(“Let’s not spam”);
}
}
delay(500);
}

2. Python code

import time

import serial

import smtplib

import io

TO = ‘cristina.vintila@gmail.com’

GMAIL_USER = ‘cristina.vintila@gmail.com’

GMAIL_PASS = ‘no, you cannot get this one ;)’

SUBJECT = ‘Intrusion!!’

TEXT = ‘Watch the Minions! The Minions are not safe!’

ser = serial.Serial(‘/dev/cu.usbmodem1421’, 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)

#ser.close()

3. Output

rbh:Arduino cristina$ python safeMinions.py

Let’s not spam

Let’s not spam

Movement!

Sending Email

To:cristina.vintila@gmail.com

From: cristina.vintila@gmail.com

Subject:Intrusion!!

Let’s not spam

And I have the email 🙂

Happily, I am right next to the Minions, and I know they are safe

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s