This is my last Raspberry PI project. It’s a simple alarm driven by a home-made door sensor which plays an alarm sound and sends me a message via twitter.
The video is in italian.
Here’s the python code which uses the tweepy library:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb, time
import RPi.GPIO as GPIO
import os
import tweepy
import traceback
#fill these variables with your twitter app data
oauth_token = **
oauth_token_secret = **
consumer_key = **
consumer_secret = **
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(oauth_token, oauth_token_secret)
api = tweepy.API(auth)
def send_tweet( api, what ):
try:
print "Tweeting '"+what+"'.."
now = int(time.time())
api.update_status('@danielenick89 '+ str(now) +': '+ what)
print "Tweet id "+str(now)+ " sent"
except:
print "Error while sending tweet"
traceback.print_exc()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
state = GPIO.input(11)
#print "Content-type: text/htmlnn";
while not GPIO.input(11):
time.sleep(0.3)
#print "Activated.."
while GPIO.input(11):
time.sleep(0.3)
send_tweet(api, "RasPI: Unauthorized door opening detected.")
os.system("mplayer alarm2/alarm.mp3");
In order to use this code you have to create an application on twitter, and then fill the variables (oauth_token, oauth_token_secret, consumer_key, consumer_secret) with the relative value you can find in the site after creating the app.