これら3つのコードを一緒に結合する際に問題があります。このプロジェクトはSmoke System Alarmです。煙センサーが煙を検知すると、写真が撮影され、写真が添付された電子メールが送信されます。私が混乱していることは、3つのコードを一緒に結合する方法です。Python 3 Together Code Together - Raspberry Pi
ありがとうございました!
カメラコード:
#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *
import pygame.camera
width = 480
height = 360
#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()
#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')
#take a picture
image = cam.get_image()
cam.stop()
#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')
メールコード:
#!/usr/bin/env python
# encoding: utf-8
import os
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
COMMASPACE = ', '
def main():
sender = ''
gmail_password = ''
recipients = ['']
# Create the enclosing (outer) message
outer = MIMEMultipart()
outer['Subject'] = 'SMOKE HAS BEEN DETECTED!'
outer['To'] = COMMASPACE.join(recipients)
outer['From'] = sender
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
# List of attachments
attachments = ['']
# Add the attachments to the message
for file in attachments:
try:
with open(file, 'rb') as fp:
msg = MIMEBase('application', "octet-stream")
msg.set_payload(fp.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
outer.attach(msg)
except:
print("Unable to open one of the attachments. Error: ", sys.exc_info()[0])
raise
composed = outer.as_string()
# Send the email
try:
with smtplib.SMTP('smtp.gmail.com', 587) as s:
s.ehlo()
s.starttls()
s.ehlo()
s.login(sender, gmail_password)
s.sendmail(sender, recipients, composed)
s.close()
print("Email sent!")
except:
print("Unable to send the email. Error: ", sys.exc_info()[0])
raise
if __name__ == '__main__':
main()
MQ2煙センサーコード:
import time
import botbook_mcp3002 as mcp #
smokeLevel= 0
def readSmokeLevel():
global smokeLevel
smokeLevel= mcp.readAnalog()
def main():
while True: #
readSmokeLevel() #
print ("Current smoke level is %i " % smokeLevel) #
if smokeLevel > 120:
print("Smoke detected")
time.sleep(0.5) # s
if_name_=="_main_":
main()