kawa_j
Posts: 5
Joined: Tue Dec 03, 2013 10:37 pm

Simple gpio alarm script

Sat Jan 04, 2014 3:11 am

Hi,
i am looking for a simple alarm script that wil detect if a gpio is switched from high to low and then send an email

have tried the privatepiproject(and allot of other scripts) but they are not working properly
also tried to write it myself but i am not good at python coding :mrgreen:

can someone please help me?

greets
kawa

TrevorAppleton
Posts: 74
Joined: Wed May 30, 2012 7:26 pm
Contact: Website

Re: Simple gpio alarm script

Sat Jan 04, 2014 6:59 pm

I recommend this resource to learn how to use the GPIO in Python. It really is very good.

http://raspi.tv/2013/how-to-use-interru ... d-rpi-gpio

Enjoy!
Check out my blog post for Raspberry Pi and Python tutorials.

http://trevorappleton.blogspot.co.uk/

kawa_j
Posts: 5
Joined: Tue Dec 03, 2013 10:37 pm

Re: Simple gpio alarm script

Sun Jan 05, 2014 3:28 am

Thanks for the response,

tried to make the script myself but i suck!
i get this error when i run the script:
File "alarm.py", line 29
server.ehlo()
^
IndentationError: unexpected indent


here is the script :

#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv/
# http://raspi.tv/2013/how-to-use-interru ... d-rpi-gpio

import smtplib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

server = smtplib.SMTP('smtp.gmail.com:587')
gmail_user = "sender@gmail.com"
gmail_pwd = "******"
FROM = 'from@gmail.com'
TO = ['to@gmail.com'] #must be a list
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"

# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# GPIO 23 set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)


try:
GPIO.wait_for_edge(23, GPIO.RISING)
print "\nalarm rising signal"

server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"

except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit



What is going wrong?

greets kawa

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Simple gpio alarm script

Sun Jan 05, 2014 4:16 am

python, unlike many languages, requires indents in certain places.

while and for loops must be indented.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

kawa_j
Posts: 5
Joined: Tue Dec 03, 2013 10:37 pm

Re: Simple gpio alarm script

Tue Jan 07, 2014 2:17 am

Script is up and running! But stil have one problem :cry:
For some reason the script/Pi detects a rising signal (when the script is running for 4min)?
The gpio input has a 1K Ohm resistor so it is not floating

What is going wrong? Can someone please help :roll:

Here is the script:
-------------------------------------------------------------------
import smtplib
import time

def sendemail(from_addr, to_addr_list, cc_addr_list,
subject, message,
login, password,
smtpserver='smtp.gmail.com:587'):
header = 'From: %s\n' % from_addr
header += 'To: %s\n' % ','.join(to_addr_list)
header += 'Cc: %s\n' % ','.join(cc_addr_list)
header += 'Subject: %s\n\n' % subject
message = header + message

server = smtplib.SMTP(smtpserver)
server.starttls()
server.login(login,password)
problems = server.sendmail(from_addr, to_addr_list, message)
server.quit()



import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.OUT, initial=False) #Siren pin setup
try:
GPIO.wait_for_edge(23, GPIO.RISING)
print "\nRising edge ALARM"
sendemail(from_addr = '@gmail.com',
to_addr_list = ['@gmail.coml'],
cc_addr_list = [''],
subject = 'ALARM',
message = 'alarm',
login = '@gmail.com',
password = '****')

GPIO.output(24, True)
time.sleep(5)
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
GPIO.cleanup() # clean up GPIO on normal exit

Return to “Python”