The Lofty Aim
Allow our cats to tweet the mundane events of their lives.
Set up sensors connected to a computer, then program the computer to send tweets when the sensors trigger.
I hatched a cunning plan.
The diagram to the left is missing one thing only: a cooperative cat...
...but how hard can that be?
The Hardware - #1 a "Makey Makey" - $50
A "Makey Makey" is a USB device that the computer thinks is a keyboard, but it actually tells the computer a key has been pressed when you put two wires together.
The Hardware - #2 a "Raspberry Pi" kit - $160
A "Raspberry Pi" is a super-cheap $35 computer... I bought mine from "Aus Pi" for $160 in a kit with a USB hub, wireless card, power etc.
I already had a webcam!
The Software - #1 Rasberrian
I'm familiar with Windows, but the Raspberry Pi computer comes with "Rasberrian" pre-installed. The good news for me is I see desktop similar to Windows, with a start button, desktop icons and a taskbar.
The Software - #2 Python
I need to program the computer to tweet when the cats have their water.
Python is a computer programming language. I have never used it before. The Raspberry Pi comes with "Python" already installed and good to go.
The Learning Process
I had a stack of stuff to learn to make this work. It was much easier than I expected.
I had to figure out:
- how to get around my Raspberry Pi, and specifically how to operate it remotely from another computer for convenience
- how to get my Python program to run automatically when the Raspberry Pi boots up
- how to program in Python: #1 to wait for input from the Makey Makey, #2 to take a photo with the webcam and #3 how to send the photo as an attachment via email to Flickr.com
- I already knew how to tell Flickr to accept such emails and post them to Twitter, with the email text as the tweet, and the attachment as the photo.
I learned by:
- for each element I needed to learn, googling and exploring heaps of web pages and tutorial videos
- copying and pasting Python code I did not understand and tinkering with it
- writing little Python programs that do just one of the tasks, one at a time, before stitching them together
The learning process was glorious... on one day I spent well over 12 hours with no proper break. Flow, flow, flow!
The End Result
If we build, it will the cats come?
The Actual Python Code
from pygame.locals import *
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
my_subject = "Miao Miao! Hello, this is Timo or Baps telling the world
that we're enjoying some water! Look and see:"
USERNAME = "#######"
PASSWORD = "#######"
grab_cam = subprocess.Popen(
"sudo fswebcam -r 320*240 -S 15 -d /dev/video0
def sendMail(send_to, subject, text, files=):
msg = MIMEMultipart()
msg['From'] = USERNAME
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for file in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open("/home/pi/HappyTimo.jpg","rb").read() )
part.add_header('Content-Disposition', 'attachment; filename="%s"'
server = smtplib.SMTP('smtp.gmail.com',587)
server.sendmail(USERNAME, send_to, msg.as_string())
screen = pygame.display.set_mode((640,480))
while not done:
happened = pygame.event.wait()
if happened.type == QUIT:
elif happened.type == NOEVENT:
elif (happened.type == KEYDOWN):
if happened.key == K_SPACE: