logo

A Twitter Operated Lock Box

What is TweetToLock?

Introduction:

TweetToLock is a Raspberry Pi based project that allows you to unlock a box (or anything else, depending on what you attach it to) by tweeting a certain keyword.

The Hardware:

My setup contains the following:
A Rapberry Pi (of course)
A micro servo (it has enough force for my project but may not be enough for something like opening a dead-bolt lock on a door)
A box (or anything you want attach a servo or other actuator to)

The Software:

Firstly, to make working with a servo much easier a flashed a version of linux to my Pi known as Occidentals. Occidentals is created by Adafruit and available for free on thier website along with tutorials for running a servo with it. So once I had the proper setup for running my servo I created the actual program in Python (see full code below). This is mostly just basics, the trickiest part was pulling the actual data from twitter. For this I took a few hints from a similar project called Tweet-a-Pot (you can check it out on Instructables). It's somewhat different since it uses an arduino but it still uses python to interface with Twitter and gave me a good idea of how to do that.

Future Work:

This is just a quick list of things I would like to add to make this project more polished.
-Checking the current state of the box (open/close) each time it turns on
-Make a web interface that allows you to easily set things such as the Twitter account used and the keywords for opening and closing it.
-Make it easily attach to a door lock or other stationary locking system (since it's really not practical to have a box that needs to be plugged in/ recharged all the time)

The Code

# TweetBox by Gary Grossi
# Created 06/04/2014
# Updated 06/09/2014
# Version 0.4

# library imports
import time
import twitter

# Twitter authentication and setup
keys = twitter.Api(consumer_key="Enter consumer key here",
consumer_secret="Enter consumer secret here",
access_token_key="Enter access token here",
access_token_secret="Enter token secret here")
print "Starting TweetToLock"
status = ""
keywordLock = "colloportus"
keywordUnlock = "alohomora"
prevState = ""

# Tweet reading function
def tweetTL():
global prevState
status = keys.GetUserTimeline("TweetToLock")
receivedStatus = [s.text for s in status]
splitStatus = receivedStatus[0].split()
length = len(splitStatus)
for x in range (0,length):
if splitStatus[x] == keywordLock and splitStatus[x] != prevState:
print "locking"
def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value) def setServo(angle):
set("servo", str(angle))
set("delayed", "0")
set("mode", "servo")
set("servo_max", "180")
set("active", "1")
delay_period = 0.01
for angle in range(0, 160): # Runs servo to close
setServo(180 - angle)
time.sleep(delay_period)
print(angle)
prevState = keywordLock
elif splitStatus[x] == keywordUnlock and splitStatus[x] != prevState:
print "unlocking"
def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value)
def setServo(angle):
set("servo", str(angle))
set("delayed", "0")
set("mode", "servo")
set("servo_max", "180")
set("active", "1")
delay_period = 0.01
for angle in range(0, 160): # Runs servo to open
setServo(angle)
time.sleep(delay_period)
print(angle)
prevState = keywordUnlock
else:
print "neutral"

# Running tweetTL function on loop
while True:
tweetTL()
time.sleep(5)