1

I am new to raspberry pi and am trying to set my Raspberry pi 3B+ so that upon clicking a button, it should all the functions declared in gpio_callback. However I get no output at all. What am I doing wrong?

import picamera

import boto3

s3 = boto3.resource('s3')

import RPi.GPIO as GPIO

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders


GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


def gpio_callback(self):
    capture_image()
    time.sleep(0.3)
    print('Captured')
    upload_image()
    time.sleep(2)
    send_email()


GPIO.add_event_detect(17, GPIO.FALLING, callback=gpio_callback, bouncetime=3000)


def capture_image():
    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.start_preview()
        camera.capture('sample.jpg')
        camera.stop_preview()
        camera.close()
        return


def upload_image():
    file = open('sample.jpg','rb')
    object = s3.Object('xywz','sample.jpg')
    ret = object.put(Body=file,
            Metadata={'FullName':'Guest'}
            )
    print(ret)
    return


def send_email(): 
    fromaddr = "abcd@gmail.com"
    toaddr = "abcd@hotmail.com"

    msg = MIMEMultipart()

    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "New Guest"

    body = "A new guest is waiting at your front door. Photo of the guest is attached."

    msg.attach(MIMEText(body, 'plain'))

    filename = "sample.jpg"
    attachment = open("/home/pi/sample.jpg", "rb")

    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

    msg.attach(part)

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(fromaddr, "xxxxxx")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

My hunch is I have declared the functions, but not exactly executing a command to callgpio_callback. I can't get head around this. Can someone help me?

  • your code lacks [infinite loop](https://stackoverflow.com/questions/61515521/how-to-disable-raspberry-pi-gpio-event-for-certain-time-period-after-it-runs-in). It registers listener and then completes execution so your callback has no chance to fire. – Maxim Sagaydachny Apr 30 '20 at 06:04

0 Answers0