Raumüberwachung

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
HJS
User
Beiträge: 2
Registriert: Mittwoch 6. August 2014, 09:42

Hallo Gemeinde,

ich versuche mit einem Raspberry Pi, einem PIR-Sensor und einer Webcam eine Raumüberwachung zu realisieren.
Die Aufnahme wird auf eine extrene SD Karte gespeichert und per eMail an mein eMail-Account gesendet.

Das Script macht was es soll, hat aber ein Problem.
Die erste Mail hat ein Bild im Anhang, die zweite Mail 2 Bilder im Anhang usw.

Hat jemand von euch eine Idee wo der Fehleer liegt?!


Gruß und Dank
Joachim

Code: Alles auswählen

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k|
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
#
# Based on pir_1.py by Matt Hawkins
# 
# Detect movement using a PIR module
#
# Author : Matt Hawkins
# Date   : 21/01/2013
#
# Angepasst von HJS
# Date   : April 2014  
# pir motion detect,save picture, send mail

# Import required Python libraries

import RPi.GPIO as GPIO
import time
import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.mime.base import MIMEBase
from email import encoders
from email.Utils import formatdate

msg = MIMEMultipart()

# Initialize GPIO pin 7 for input (Broadcom reference [= BCM])
GPIO_PIR = 7
GPIO.setmode (GPIO.BCM)
GPIO.setup (GPIO_PIR,GPIO.IN)     

Current_State  = 0
Previous_State = 0

print ("PIR motion detection (to exit press Ctrl-C or kill process)")

try:

  print ("Waiting for PIR to settle ...")

# Loop until PIR output is 0
  while GPIO.input(GPIO_PIR) == 1:
    Current_State  = 0    

  print ("  ... Done")     
  
# Loop until users quits with CTRL-C
  while True :
  
    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)
    
    # Check if PIR is triggered
    # The following state combinations are possible:
    # Current_State == TRUE  and Previous_State == FALSE : New PIR event
    # Current_State == FALSE and Previous_State == TRUE  : Rearm PIR trigger logic
    # Current_State == FALSE and Previous_State == FALSE : Idle condition       
    if Current_State == 1 and Previous_State == 0:
      print ("  Motion detected!")

      # PIR is triggered uses Fswebcam to take picture
      os.system ('fswebcam -r 960x720 -S 3 --jpeg 100 --save /rpi_daten/raumueberwachung/bild%d%m%Y%_H%M%S.jpeg')
      os.system ('fswebcam -r 960x720 -S 3 --jpeg 100 --save /tmp/bild.jpeg')

      senderaddr = 'xxx'
      receiveraddr = 'xxx'
      user  = 'xxx'
      pw = 'xxx'

      msg['Subject'] = 'Die Wohnung wurde moeglicherweise betreten'
      msg['From'] = senderaddr
      msg['To'] = receiveraddr
      msg['Date'] = formatdate()
      
      text = MIMEText('Raumalarm - %s' % time.strftime('%H:%M:%S %d.%m.%Y'))
      
      msg.attach(text)

      # Open the file to scan in binary mode
      fp = open('/tmp/bild.jpeg', 'rb')
      attachment = MIMEBase('application', 'octet-stream')
      attachment.set_payload(fp.read())
      encoders.encode_base64(attachment)
      attachment.add_header('Content-Disposition', 'attachment; filename="bild.jpeg"')
      fp.close()
      msg.attach(attachment)

      # Send the email via your own SMTP server.
      server = smtplib.SMTP_SSL('smtp.xxx.net', 465)
      server.login(user,pw)

      server.sendmail(senderaddr, receiveraddr,msg.as_string())
      server.quit()
     
      # Record previous state
      Previous_State = 1
    elif Current_State == 0 and Previous_State == 1:
      
      # PIR has returned to ready state
      print ("  Rearmed")
      Previous_State = 0
      
    # Take a nap to lower overall system burden
    time.sleep (2.00)      
      
except KeyboardInterrupt:
  print ("  Quit")
  
  # Reset GPIO settings
  GPIO.cleanup ()

BlackJack

@HJS: Das ist doch offensichtlich: Du erstellst vor der Schleife ein Message-Objekt und hängst da in der Schleife jedes mal ein weiteres Bild an. Erstelle für jede Mail ein *neues* Message-Objekt.
HJS
User
Beiträge: 2
Registriert: Mittwoch 6. August 2014, 09:42

Hallo BlackJack,

na wenn das nicht mal peinlich ist :oops:

Danke für die Hilfe!!!
Antworten