Today the 'Little Box of Geek' Raspberry Pi and thermal printer project featured on the front page of the Raspbbery Pi Foundation official website.
http://www.raspberrypi.org/archives/2953
It's been a very exciting and busy day with many people getting in touch via twitter, facebook and YouTube to offer their support for my work.
If you want to get in touch, have ideas for episodes or suggestions for improvements check out:
Subscribe to the Geek Gurl Diaries YouTube Channel
Facebook Page
Twitter accounnt @GeekGurlDiaries or @MissPhilbin
Check out the Geek Gurl Diaries Website
Many thanks to everyone at Raspberry Pi for their kind words and support, and welcome to all my new viewers and followers, I hope I do not disappoint!
Sunday, 30 December 2012
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()
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()
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()
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
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()
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:
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.
Thursday, 27 December 2012
Little Box of Geek Project - Part 1
You will need:
- A Raspberry Pi, power supply and peripherals (including up to date image on SD card)
- A thermal printer, a 7.5v/2a power supply, and roll of paper
- A cardboard box
- A button
- Cables
- Breadboard
- A resistor
- A solder-less DC connector
- A small crosshead screwdriver
- A cable striper
- Scissors
- A pen
- Some help from an adult for the fiddly bits.
Prepare the thermal printer by connecting the cables. First plug in the data cables (green, yellow and black) to the printer. Next connect the power cable (red, black).
Cut off the end of the power connector and replace it with the solder less dc connector so that it can be plugged into a dc power supply (must be between 5 and 9v, 2a). Make sure that the red wire is put into the positive (+) and the black wire into the negative (-). Screw in tightly.
Test that the printer works with your power supply by plugging it in. If the green light flashes and when you press the paper feed button, the paper comes out, the printer is ready to be plugged into the Raspberry Pi GPIO pins.
Using male to female jumper cables attach the RX of the printer (yellow) to pin 8 and the ground of the printer (black) to pin 6 on the Pi. GPIO layout Note: Make sure you read this first :)
Connect your pi to a keyboard, mouse, monitor etc. You should have already setup your raspberry pi, if you have not done this yet check out GGD episode 5.
We need to first setup the Raspberry Pi serial port and change some settings.
Install the required files by typing the following into a terminal window.
sudo apt-get install python-serial
sudo apt-get install python-imaging-tk
sudo apt-get install python-imaging-tk
Next give the serial port permission to dialout using:
sudo usermod -a -G dialout pi
We also need to edit this file:
sudo nano /boot/cmdline.txt
By deleting:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
and replacing it with:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait
Finally remove the last line of /etc/inittab and restart your raspberry pi using:
sudo shutdown -r now
(With thanks to natemcbean.com tutorial)
Next we need to download the python files needed to print from github. First you will need to install git-core onto your pi using:
sudo apt-get install git-core
create a directory on your pi to download your files from git hub to using:
cd~/git
to download the printer repository use:
git clone git://github.com/luopio/py-thermal-printer.git
Finally we need to edit the following file:
sudo nano printer.py
To add this line at the top:
#!/usr/bin/env python
and to check that it is pointing to the correct port:
/dev/ttyAMA0
Run Printer.py and a test page should print.
What are you waiting for? Hot foot it to Part 2 for more fun!
What are you waiting for? Hot foot it to Part 2 for more fun!
Subscribe to:
Posts (Atom)