crystalblade
Posts: 7
Joined: Wed Dec 27, 2017 7:39 am

Raspberry pi sound detector sends mail when triggered

Mon Feb 12, 2018 5:48 pm

hi everyone,
i'm having trouble with configuring sensors so that it can send an email via gmail when it is triggered.
im testing out first with a sound sensor. The sensor works when it is activated but i can't make it work so that i can be notified when it is.

Here is the code for the sensor working:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)

def callback(channel):
        if GPIO.input(channel):
                print "Sound Detected!"

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback)  # assign function to GPIO PIN, Run function on change

# infinite loop
while True:
        time.sleep(1)

And here is the code for the smtp:

Code: Select all

#!/usr/bin/python
import smtplib
TO = '[email protected]'
SUBJECT = 'PIR'
TEXT = 'Motion is detected'

gmail_sender = '[email protected]'
gmail_passwd = 'mypwd'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join([
       'TO: %s' % TO,
       'From: %s' % gmail_sender ,
       'Subject: %s' % SUBJECT ,
       '',
       TEXT
       ])
try:
   server.sendmail(gmail_sender, [TO], BODY)
   print 'email sent'
except:
   print 'error'

server.[/quote]quit()
I would really appreciate if you gave me an instruction on how to connect those two, and how to learn how to do the same to other sensors, that is to receive email when any sensor is triggered. This is for a project. If any of you could recommend any sites, links that i can learn from, then that would be sweet.

thx again in advance.

pcmanbob
Posts: 4464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Raspberry pi sound detector sends mail when triggered

Tue Feb 13, 2018 1:29 pm

You could just add the mail bit to your existing callback function, but if you intend to add other sensors you might be better making the mail bit in its own function which you can call from your existing callback function and any more you may add.

like this

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import smtplib

#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)

def send_mail_now():
    TO = '[email protected]'
    SUBJECT = 'PIR'
    TEXT = 'Motion is detected'

    gmail_sender = '[email protected]'
    gmail_passwd = 'mypwd'

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo
    server.login(gmail_sender, gmail_passwd)

    BODY = '\r\n'.join([
           'TO: %s' % TO,
           'From: %s' % gmail_sender ,
           'Subject: %s' % SUBJECT ,
           '',
           TEXT
           ])
    try:
       server.sendmail(gmail_sender, [TO], BODY)
       print 'email sent'
    except:
       print 'error'

    server.quit()



def callback(channel):
        if GPIO.input(channel):
                print "Sound Detected!"
                send_mail_now()

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback)  # assign function to GPIO PIN, Run function on change

# infinite loop
while True:
        time.sleep(1)
      
this is un-tested code
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

crystalblade
Posts: 7
Joined: Wed Dec 27, 2017 7:39 am

Re: Raspberry pi sound detector sends mail when triggered

Thu Feb 22, 2018 7:01 am

Hey buddy, thx a lot, it works. Damn, i was looking for this solution for a long time!! cheers man!

Return to “Python”