I'm going to make a robot pet
I am building a robot pet.
Read MoreInnovation and Learning
Start with clarity of intent.
Now build it out with an evocative vision. Improvise progress by tinkering: with lots of trial and lots of error. The not knowing is the best bit: the mysteries the surprises, and from time to time the windfalls!
Hello there, I'm Steve Collis!
Click on "contact", won't you, and wave right back at me?
I am building a robot pet.
Read MoreAllow our cats to tweet the mundane events of their lives.
The Plan
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?
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.
Makey Makey
Raspberry Pi
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!
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.
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.
I had a stack of stuff to learn to make this work. It was much easier than I expected.
I had to figure out:
I learned by:
The learning process was glorious... on one day I spent well over 12 hours with no proper break. Flow, flow, flow!
Yes!
import time
import os
import pygame
from pygame.locals import *
import smtplib
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
import sys
import subprocess
my_subject = "Miao Miao! Hello, this is Timo or Baps telling the world
that we're enjoying some water! Look and see:"
USERNAME = "#######"
PASSWORD = "#######"
def takepicture():
grab_cam = subprocess.Popen(
"sudo fswebcam -r 320*240 -S 15 -d /dev/video0
/home/pi/HappyTimo.jpg", shell=True)
grab_cam.wait()
def sendMail(send_to, subject, text, files=[]):
assert type(files)==list
assert type(send_to)==list
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() )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(file))
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, send_to, msg.as_string())
server.quit()
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Pygame Caption')
pygame.mouse.set_visible(0)
done= False
while not done:
pygame.event.clear()
happened = pygame.event.wait()
if happened.type == QUIT:
done=True
elif happened.type == NOEVENT:
print "confusing"
elif (happened.type == KEYDOWN):
print happened
print happened.key
if happened.key == K_SPACE:
print happened
print happened.key
takepicture()
time.sleep(10)
sendMail( ["########@photos.flickr.com"],
my_subject,
"tags: cat",
["HappyTimo.jpg"] )
time.sleep(5)
pygame.event.clear()
else:
done=True
sys.exit()