Happy Steve

Innovation 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?

Filtering by Tag: python

Raspberry Pi + Makey Makey = Cats that Tweet

The Lofty Aim

Allow our cats to tweet the mundane events of their lives. 

IMG_8673 (Medium).JPG
The Plan

The Plan

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? 

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. 

Makey Makey

Makey Makey

Raspberry Pi

Raspberry Pi

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?


Gallery Block
This is an example. To display your Flickr images, double-click here to add an account or select an existing connected account. Learn more

The Actual Python Code

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)


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() )


        part.add_header('Content-Disposition', 'attachment; filename="%s"'

                        % os.path.basename(file))


    server = smtplib.SMTP('smtp.gmail.com',587)





    server.sendmail(USERNAME, send_to, msg.as_string())



screen = pygame.display.set_mode((640,480))

pygame.display.set_caption('Pygame Caption')


done= False

while not done:


    happened = pygame.event.wait()

    if happened.type == QUIT:


    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



            sendMail( ["########@photos.flickr.com"],


                "tags: cat",

                ["HappyTimo.jpg"] )






Next Step

  • maybe get a Ninja Block to expand sensor capabilities
  • expand the repertoire of trigger events and the wording of tweets
  • could the cats blog? could they Facebook? could they run for office?