Τί θα μάθουμε σημερα Θείο??

...πώς να συνδέσουμε έναν ανιχνευτή κίνησης (PIR movement detector) με ενα Arduino...

... και πως να κανουμε το Arduino να επικοινωνήσει με την Python στο PC μας...

... και τελικά πώς θα στέλνουμε email στο FBI όταν ανιχνευτεί ένας κακός άνθρωπος που πάει να μας κλέψει τη συλλογη με τα γραμματόσημα!

Θα χρειαστούμε αυτά.

PIR motion detector

Arduino

Καλώδια

(jumper wires)

Checklist: ArduinoIDE,Drivers,COM port

Συνδεσμολογία

ή αλλιώς...τι πάει που !

int pirPin = 7;
 
int minSecsBetweenEmails = 5; // the min delay between emails in secs
 
long lastSend = -minSecsBetweenEmails * 1000l;
 
void setup()
{
  pinMode(pirPin, INPUT);
  Serial.begin(9600); // the baud rate
}
 
void loop()
{
  long now = millis();
  if (digitalRead(pirPin) == HIGH)
  {
    if (now > (lastSend + minSecsBetweenEmails * 1000l))
    {
      Serial.println("MOVEMENT"); // what it will print when movement is senced
      lastSend = now;
    }
    else
    {
      Serial.println("Waaaait for it....!"); // what will print while it waits
    }
  }
  delay(500); //the delay between the senses in milliseconds
}

The Arduino Code

Πριν όμως ανεβάσουμε τον κώδικα στο Arduino...

... μπορούμε να το τρέξουμε στο Serial Monitor για να δούμε ότι δουλεύει

Tools -> Serial Monitor  (or)  Ctrl+Shift+M

It's Python time !!!

  • Install Python (2.7) on Windows
    (Mac/Linux already have it)
    http://www.python.org/getit/
     
  • Set python.exe to the PATH
    (windows environmental variables)
     
  • Install PySerial (Python library)
    PySerial Win64bit Python2.7 -> http://goo.gl/1teXfU

..μικρή προεργασία πριν ξεκινησουμε..

The                      code (yeah!)

get it here:  http://goo.gl/i4HxVy

__author__ = 'wehappyfew'


import time
import serial
import smtplib

# SETTINGS
RECIPIENT = "the_recipients email"
GMAIL_SENDER = "the_senders email"
GMAIL_PASS = "the password"

GMAIL_SMTP_SERVER = "smtp.gmail.com"
PORT = 587

SUBJECT = "RED ALERT !!!"
TEXT = "IOU iou IoU IOU IOU iou  \n  " \
	   "It works!!  :P"

# the pyserial library reads the input from the serial port ( COM ) at spesific baud rate
s = serial.Serial('COM3',9600)

#the function that is going to be called to send the email when movement is sensed
def send_email():
	print("Sending the email..")
	# connect to the SMTP server
	smtpserver = smtplib.SMTP( GMAIL_SMTP_SERVER,PORT)
	smtpserver.ehlo()
	smtpserver.starttls()
	smtpserver.ehlo()
	smtpserver.login(GMAIL_SENDER, GMAIL_PASS)
	# Set the header
	header = 'From: ' + GMAIL_SENDER + '\n' + 'To:' + RECIPIENT
	header = header + '\n' +'Subject: ' + SUBJECT + '\n'
	print header
	# create the message
	msg = header + '\n' + TEXT +'\n\n'
	# send the email & close the connection
	smtpserver.sendmail(GMAIL_SENDER,RECIPIENT,msg)
	smtpserver.close()

def send_me_sms(message, receiver=None):
	"""
		Use the twilio free trial account to send arbitrary SMS messages.
	"""
	from twilio.rest import TwilioRestClient
	# Your Account Sid and Auth Token from twilio.com/user/account
	account_sid = "MY_ACOOUNT_SID"
	auth_token  = "MY_AUTH_TOKEN"
	client = TwilioRestClient(account_sid, auth_token)

	#send the SMS to the appropriate receiver
	if receiver == "myself":
		client.messages.create(to="+306911111111", from_="+19804042067", body=message) #from_ = the twilio number assigned to me

	#if receiver == "All my friends and the Police":
		# to idio pragma alla stelnei se ari8mous peran tou dikou mou (only with paid Twilio account)


while True:
	message = s.readline()
	print(message)
	if message[0] == 'M':
		send_email()
		#send_me_sms("ALARMAAAAAA","myself")
	time.sleep(0.5)

Run it.

python movement.py

Και μετά τι...?

SMS ?

Text to Speech ? 

Something else?

Made with Slides.com