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.