I am trying to build a security system on the Rasberry pi 3 using python 3 that senses motion then takes a photo and sends the photo via email to me. When i run the code it says it can't import the module picamera, I have installed the module on terminal using the 'sudo apt-get install python-pip' command but it says already satisfied. Here is the code:
import RPi.GPIO as GPIO
import time
import picamera
import datetime
import subprocess
import smtplib
import os
AuthUser = '[email protected]'
AuthPass = 'alarmsystem'
toAdd = '[email protected]'
fromAdd = AuthUser
subject = 'Motion Detected'
def getFileName():
return datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264")
from gpiozero import MotionSensor
pir = MotionSensor(4)
from PiCamera import PiCamera
camera = PiCamera()
camera.capture('image.jpg')
while True:
pir.wait_for_motion()
print("Motion Detected")
camera.start_preview()
pir.wait_for_no_motion()
camera.stop_preview()
mail = smtplib.SMTP('smtp.gmail.com',587)
gmailUpload = ()
mail.ehlo()
mail.starttls()
mail.login ('[email protected]', 'alarmsystem')
mail.send_mail = (fromAdd, toAdd)
#sensor setup
sensorPin = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(sensorPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
prevState = False
currState = False
camera = picamera.PiCamera()
from gpiozero import MotionSensor
pir = MotionSensor(4)
while True:
time.sleep(0.1)
prevState = currState
currState = GPIO.input(sensorPin)
if currState != prevState:
newState = "HIGH" if currState else "LOW"
print ("GPIO pin %s is %s" % (sensorPin, newState))
if currState:
fileName = getFileName()
print ("Starting Recording...")
camera.start_preview()
camera.start_recording(fileName)
print (fileName)
else:
camera.stop_preview()
camera.stop_recording()
print ("Stopped Recording")
print ("Sending Mail Notification...")
subprocess.call("mail -s 'Motion Detected' [email protected] < /home/pi/mail/images", shell=True)
print ("Complete")
Please help me fix this issue it would be greatly appreciated.
Thank you