Little Box of Poems with a Raspberry Filling

Back in October 2012 I made my first Little Box of Poems – this is a self-contained box that prints out a random short poem when you press a big red shiny button. I like having poems instead of receipts in my wallet. And it makes a good educational project combining physics (wiring), DT (making the box), ICT (programming) and English (writing or finding poems).

It was made using a little £40 Adafruit-type thermal printer and an Arduino microcontroller. I found that I could only get about a dozen short poems in the Arduino before it started spewing out gibberish, I guess because it ran out of memory. I could have claimed this as some sort of machine-generated found-poetry art project, but I wanted proper poems like what the poets originally wrote.

random poems

As luck would have it, my original Arduino-based Little Box of Poems inspired Carrie Anne Philbin to make The Little Box of Geek. This is another box that prints random stuff when you press a button – in this case random nuggets of geeky wisdom. Crucially, Carrie Anne used a Raspberry Pi instead of an Arduino. This means you can add much more stuff into your box, and also opens up the possibility of getting stuff off the internet. Do have a look at her blog and videos posted under the Geek Gurl Diaries banner – her aim is to get girls more engaged with IT and ICT in school.

Here, roughly, is how I made a Raspberry Pi-based little box of poems.

You will need:

  • A Raspberry Pi with bog-standard Raspbian OS installed.
  • A thermal printer like this: http://proto-pic.co.uk/thermal-printer/
  • Till roll for said printer
  • A big red button. It has to be red.
  • Some breadboard jumper wires
  • At least 5 female > male breadboard jumper wires to connect to the GPIO (general purpose input-output) pins on the Raspberry Pi. These are the prongs on your Pi that let you do cool stuff with things in the real world like you can with an Arduino.
  • A small breadboard
  • A 10k Ω resistor
  • A power supply for the Raspberry Pi
  • A hefty 1.5A power supply for the printer that gives between 5 and 9 volts.
  • While you’re building it, you’ll need a screen, keyboard and mouse for your Raspberry Pi – OR a computer, phone or tablet with an SSH client on it (any Mac or Linux box will work admirably). These aren’t needed once you’ve set it up.
  • An old Fairy washing tablet box or similar
  • Some short poems, haikus, limericks etc.

Circuit diagram

This is a simple diagram of the wiring needed. I had to buy some breadboard female to male leads from Maplin in order to get access to the GPIO pins on the Raspberry Pi – these meant I had to take the lid off my lovely PiBow case. If you had a proper ribbon connector this wouldn’t be a problem. Do NOT try to use an old hard drive IDE ribbon cable to connect to the GPIO pins – they short out certain pins and will likely Fry Your Pi:

Testing shorts
proving an IDE cable has shorts and should not go anywhere near your Pi

1) Fill your printer with till-roll and hold the printer button down while you connect it to that hefty 1.5A 5-9v power supply. A test sheet should come out looking like something like this:

It prints!

2) Connect your printer to the Raspberry Pi. The printer runs at 5 volts, your Pi mostly at 3.3 volts, so you need to be really careful here. Do not connect the TX pin of your printer (which may have a green wire) to the Raspberry Pi.

There’s a really useful diagram of the Raspberry Pi GPIO pins here: http://elinux.org/RPi_Low-level_peripherals

Connect the printer’s GND pin (which may have a black wire) to the pin 6 GND on the Pi. Connect the printer’s RX pin (which may have a yellow wire) to pin 8 (GPIO 14 TXD) on the Pi. That’s all you need to do for the printer.

_DSC1716

3) Then get the button wired up. Connect one side of the push button to pin 12 GND on the Raspberry Pi. Connect the the other side of the button to pin 14 on the Pi (GPIO 23). You also need to connect the same side of the switch that’s connected to GPIO 23 via a 10k ohm resistor to pin 1 of the Raspberry Pi (which supplies 3.3 volts of electricity).

4) Now you need to get some code on your Pi. Here I followed part 1 of the excellent Geek Gurl Diaries video and blog post: http://geekgurldiaries.blogspot.co.uk/2012/12/little-box-of-geek-project.html. Many, many thanks to Carrie Anne Philbin’s hard work on this stuff, which saved me a HUGE amount of time. Her instructions are excellent. Plug your Pi into the internet before you start.

5) Next set up the serial port on the Pi, install Git and the thermal printer library for Python. We’re going to use Python because it’s the default supplied programming language on the Raspberry Pi, there’s a themal printer library for it we can use, and there’s loads of help out there if you get stuck. Yes I’m afraid Python is named after Monty Python, but you can alleviate the shame of this somewhat by always sarcastically pronouncing it the American way: PIE-THONN.

This bit requires a few bits of command line geekery on the Pi. Either open up a terminal window on the Pi, or connect from another computer or phone by SSH (see end of this post for details). Follow all the steps in http://geekgurldiaries.blogspot.co.uk/2012/12/little-box-of-geek-project.html – though in the bit about removing the last line of /etc/inittab I would suggest that you comment the line out instead of deleting it, by putting a # (hash) sign at the start of the line.

6) Get on over to Part 2 of the Geek Gurl Diaries tutorial now: http://geekgurldiaries.blogspot.co.uk/2012/12/part-2.html. Have a read and a good look, play around but we’re not going to follow all of this, though, because we’re printing random poems that we are going to hard-code. It might not be the Cheese Shop, but it will be your very own Python script. Ahem. So ignore all the stuff about ‘fortune’ and instead write a file called poem.py that looks something like this:


# (c) 2012 Giles Booth @blogmywiki
# www.suppertime.co.uk/blogmywiki
# please credit me if you modify and/or use this code
# not for commercial use

import random
import printer

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0")

# the poem titles, text and authors are in 3 separate lists
# you force a new line with \n and \ at the end of a line
# allows you to continue defining a string on a new line

poemtitle = ['In a station of the Metro', 'The Sick Rose', 'This is just to say', 'Surprise']

poemtext = ['The apparition of these faces in the crowd;\n\
Petals on a wet, black bough.', 'O Rose thou art sick.\n\
The invisible worm,\n\
That flies in the night\n\
In the howling storm:\n\n\
Has found out thy bed\n\
Of crimson joy:\n\
And his dark secret love\n\
Does thy life destroy.', 'I have eaten\n\
the plums\n\
that were in\n\
the icebox\n\n\
and which\n\
you were probably\n\
saving\n\
for breakfast\n\n\
Forgive me\n\
they were delicious\n\
so sweet\n\
and so cold', 'I lift the toilet seat\n\
as if it were the nest of a bird\n\
and i see cat tracks\n\
all around the edge of the bowl.']

poemauthor = ['Ezra Pound\n', 'William Blake\n', 'William Carlos Williams\n', 'Richard Brautigan\n']

# this chooses a random poem number between 0 and 3
# (in the poem lists the 1st poem is poem number 0)
poem = random.randrange(0,4)

p.bold_on()
p.inverse_on()
p.print_text(poemtitle[poem])
p.linefeed()
p.inverse_off()
p.bold_off()
p.justify("L")
p.print_text(poemtext[poem])
p.linefeed()
p.justify("R")
p.print_text(poemauthor[poem])
p.justify("L")
p.linefeed()

# THIS SHOULD PRINT A QR CODE BUT I HAVEN'T GOT IT WORKING YET
#import Image, ImageDraw
#i = Image.open("bmw-qr.png")
#data = list(i.getdata())
#w, h = i.size
#p.print_bitmap(data, w, h, True)
#p.linefeed()

#FONT B IS THE TINY FONT
p.font_b_on()
p.print_text("The Little Box of Poems\n(c)2012 @blogmywiki\nwww.suppertime.co.uk/blogmywiki")
p.font_b_off()
p.linefeed()
p.linefeed()
p.linefeed()

7) Follow the Geek Gurl Diaries bit on adding a button, making sure you modify the GPIO script to call your poem.py script when you press the big red shiny button. The line that reads:
os.system("/usr/games/fortune -s science | python ggd_printer4.py")
should be changed to read something like:
os.system("python poem.py")
When I did this I got an error saying ‘no module named RPi-GPIO’ (I may have skipped something earlier) so I had to do a bit of installing at the command line:

sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

I also had to run the GPIO script calling poem.py as root (typing sudo before the command) otherwise I got an error saying no access to /dev/mem. I’ve not made it run automatically at startup yet, but the Geek Gurl Diaries blog tells you how to do that.

No TV? No problem!

One of the problems with the Raspberry Pi is that you need a TV connected to it. In the school holidays the TV is in heavy demand from the kids, so as everything I need to do is on the command line anyway, I decided to plug the Pi into the house broadband router by ethernet and log into the Pi using SSH. You can do this, for example from the OS X terminal:

Injecting a Pi filling to my Box o'Poems

…or from an iPhone or iPad using an SSH client like Prompt (currently on sale at £1.49 in the iTunes store):

Controlling Raspberry Pi from iPhone SSH client 'Prompt' - currently on sale at £1.49

You need to know your Pi’s IP address in order to do this, though, so you may need a keyboard, mouse & screen to start with, or you can run a utility like IP Scan (OS X) to scan your network for devices. Then you should be able to log in to the Pi by typing, for example, at the OS X command line:
ssh pi@192.168.1.190
(your IP address will almost certainly be different) and enter raspberry as the password when prompted. Then you’re logged into your Pi from the comfort of your laptop, iPad, whatever. And the kids can watch the 2,000th episode of Tracy Beaker that day.

One other benefit of using SSH from a laptop or iPhone to a Pi/printer combo in the kitchen is that you can send orders to the staff:

Important use for thermal printer connected to a Raspberry Pi

To-do list

- Add more poems
- Get image printing to work so I can include a logo or QR code. I just got a black square with the QR code PNG I tried.
- Work out how to power printer and Pi off one power supply.

This entry was posted in computers, ipad, iPhone, poetry, Raspberry Pi, Raspbian and tagged , , , . Bookmark the permalink.

12 Responses to Little Box of Poems with a Raspberry Filling

  1. I’m inspired by the idea, it’s a really great project! I’d actually back something like this on Kickstarter, love it!

    Looking forward for the next update!

    GeekBoy

  2. Pingback: Raspberry Pi & Arduino powered box of Poems | GeekBoy.it

  3. Pingback: Little Box of Poems | Blog My Wiki!

  4. Michael Street says:

    I wish I knew what you were talking about! Impressive anyway

  5. Pingback: Weather forecast on a #RaspberryPi printer | Raspberry PiPod

  6. daniel boira says:

    Excellent!!!
    any help about how print with big font size?

  7. elizabeth says:

    Hi! This is my first raspberry pi project and everything seems to be working great except the button. I go this one: https://www.adafruit.com/product/1439 and I fear it might be the wrong kind? Is that possible? Thanks!

  8. Mark Scholes says:

    Cool project, just a minor correction, you install git, not github. Github is one of many places git projects are found.

    Question: Why should you never connect the TX wire to the RPi? I see a big warning, but no explanation

  9. Michael Lorsung says:

    Hey,
    Just wondering if you ever got this to work with bmp images? I am working on a project that is using these kinds of applications as its foundation and that would be super helpful if you had!

    Best-

    Michael

    • blogmywiki says:

      Hi Michael – as I recall BMP images had to be converted but I’m not sure I ever got them working on the Raspberry Pi – I did print converted bitmapped images using the Arduino however… it was a long time ago, my memory is a bit hazy I’m afraid… will try to do some digging.

Leave a Reply to Mark Scholes Cancel reply