Friday, 28 December 2012

Little Box of Geek Project - Part 2



Using the printer base as a template, draw around the printer using pen onto the cardboard box where you want your printer to be. Using a screwdriver or pencil, pierce the box so that you can comfortably get scissors inside to cut out the hole for your printer to sit in.

Do the same again for your button. (I found that a one pound coin was the same size as my button so I used it as a template to draw around.) Wrap your box in colour paper or print some labels to make it look more interesting.

We need to create a program to print on the Raspberry Pi:


First test that the printer works by writing a simple 'Hello World' style statement. Save as printer1.py (in the same directory as the printer.py file we used in Part 1 of this tutorial because it is importing from this file) and execute the program by typing python printer1.py in a terminal window) making sure that your printer is attached to your pi:
import printer

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0")
p.print_text("\nHello Geek Gurl Diaries viewers!\n")
p.linefeed()
p.linefeed()
p.linefeed()


If that works, great! But what happens if we write a really long string? Will it go over multiple lines, or will the text be cut off? We should test it by modifying our code and saving it as printer2.py and executing it.
import printer

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0"
p.print_text("\nHello Geek Gurl Diaries viewers!What happens if we try to print a longer sentence or paragraph? Will it go over multiple lines or will the text just be cut off? It's good to test these things you know!\n"
p.linefeed() 
p.linefeed()
p.linefeed()


It seems clear that we need to use a text wrap module to ensure that all the words fit onto each line and are not split down over multiple lines on the printout. Adapt the code in printer2.py to include the textwrap module and save it as printer3.py:
import printer, textwrap 

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0"
wrapped_text = textwrap.fill("\nHello Geek Gurl Diaries viewers!What happens if we try to print a longer sentence or paragraph? Will it go over multiple lines or will the text just be cut off? It's good to test these things you know!\n"
p.print_text(wrapped_text) 
p.linefeed()
p.linefeed() 
p.linefeed()

I wanted my box to print geeky statements at anytime. I decided to use the fortune program. First Install Fortune and then install fortunes to get options to select categories of fortunes:
sudo apt-get install fortune
sudo apt-get install fortunes

By looking at the manual for fortune it is possible to use only short statements which are more suitable for my thermal printer using -s and to get specifically scientific fortunes I can use 'science'
man fortune

In a terminal window I can check to see what statements the fortune program will give me by typing:
fortune -s science

Now let's modify our program to move away from using a fixed string and instead use a Unix shell to direct the output of the fortune program to the input of our printer program. We can also add some functionality to wrap the text over 32 characters (thats how many the printer prints per line!) so that our printout looks even better. Save as printer4.py and execute:
import printer, textwrap, sys 

p=printer.ThermalPrinter(serialport="/dev/ttyAMA0"
unwrapped_text = sys.stdin.read() 
wrapped_text = textwrap.fill(unwrapped_text, 32
p.print_text(wrapped_text)
p.linefeed() 
p.linefeed() 
p.linefeed()

Test out this idea by running a terminal and typing:
fortune -s science | python printer4.py

Adding a button:


Now that our printer is printing what we want it to we need to add the button. Using a breadboard, a 10k resistor, and 6 jumper cables attach the button to the breadboard to the raspberry pi GPIO pins (I learned everything I needed to know from Adafruit Raspberry Pi GPIO Setup and Adafruit Sounds and Buttons Tutorial):





We need to write a script to detect when the button is pressed. Using a text editor type the following and save as GPIO_test.py (remember indentation is important in python!) :
#!/usr/bin/env python
from time import sleep
import os 
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

while True:
if ( GPIO.input(23) == False ):
print("Printing")
os.system("/usr/games/fortune -s science | python ggd_printer4.py")
print("Printed")


sleep(1)

Finally, we want the printer program to run whenever the button is pushed and the Pi is on. Therefore we need to modify the /etc/rc.local file to reflect this by using a terminal window:
sudo nano /etc/rc.local 

Add this before exit0 making sure it points to the correct location of your GPIO_test file:
cd /home/pi/printer/ && python gpio_test.py &

Once you have tested that it all works, transfer your pi, printer, and breadboard into the prepared cardboard box and make sure everything is plugged in and sits well. Power it all up, give is a few minutes, then press the button and receive a piece of Geek wisdom.

I hope you have found this tutorial useful and enjoyable to do. My box was an idea born from seeing @blogmywiki's Little Box of Poems which he created using an Arduino. It would not have been possible without the support of Giles and @asbradbury (the best Pi Code Monkey)

Please support my work getting more teenagers (especially girls) into tech, by watching, liking and sharing my videos.

Thanks,

Carrie Anne.

9 comments:

  1. How are you powering the Pi and the printer inside the box of geek? Is there a hidden cable round the back or have you somehow got it going off batteries?

    I'm thinking this would be an awesome project for my daughter to do and take in to school as a show and tell :)

    ReplyDelete
    Replies
    1. There are two power adapters connected to the mains, one for the printer and one for the pi, they enter the box at the back which you can not see in the video. Powering it from a battery sounds like an idea, cept I'm not sure it would work for the printer as it has very specific power needs.

      Text to speech sounds extremely possible. Would love to see what you do.

      I think this is a great project for teenagers to show and tell their friends, at school or even at a Raspberry Jam.

      Thank you for your comments and good luck!

      Delete
  2. Also, do you reckon you could replace the thermal printer with a speaker and use text-to-speech software to have the pi say things? That'd be awesome too! How did you find a thermal printer to work with the pi? It's not something I would have ever thought to pick up if I hadn't seen you do it here!

    ReplyDelete
  3. Hi GG, My little boy is looking for a New Year plan and we're thinking about maybe a Pi and some projects for the year ahead. Thank you for the inspiration - loved the video and the idea. I think it will be the youngsters leading the oldsters. From Paul (Aged 46 and a half) and Patrick (7 and three quarters).

    ReplyDelete
    Replies
    1. There are some more pi based projects coming on GGD (which boys will like as much as girls) so keep watching this space!

      Thanks for your comment and good luck :)

      Delete
  4. A few troublespots...
    I've got the pi working with the button when I type sudo python GPIO_test.py - but without the sudo I am told that I have no access to /dev/mem - I tried to modify the nano /etc/rc.local to cd/home/pi/py-thermal-printer && sudo python GPIO_test.py but when I unplugged the pi and plugged back in, it thought I was logging in as sudo...

    Did you have this issue?

    Thanks!

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Very nice presentation.
    I would like to do something similar, which is to
    build a portable price label printer . how do I tell the pi to wait for input to print. eg:- person picks up the assembled device( pi+printer+lcd+keyboard) and the LCD displays "item name" they enter it > then are prompted for price and barcode , they enter it and then push a button to print label.
    how do you write a script which waits for information then carries out the instructions. help please

    ReplyDelete
  7. Hi there! Thank you for your work on this tutorial. I am doing something similar so I followed your guide. Basically, I am feeding an RSS feed. I am having a bit of a problem where the printer likes to randomly print some garbled characters over a few lines. This is where text should be printing (not images) and there doesn't seem to be any rhyme or reason to it. Wondering if you have ever ran into a similar issue?

    ReplyDelete