x Raspberry Kiosk - 03 - Printing a receipt with the Raspberry Pi

Raspberry Kiosk

Archive   

A web-kiosk based on the Raspberry pi. Journey of a software engineer into a DIY project

Want more raspberry juice?
Ask me!

Support this blog!


Home

03 - Printing a receipt with the Raspberry Pi


Date: December 26, 2012 at 3:10pm
Tags: #thermal printer #raspberry #printer #receipt #tutorial

By reading this tutorial you will learn to use your cute raspberry pi to do this :

We will learn how to print a simple “Hello World” using the Adafruit Mini Thermal Receipt Printer.

This is the result of a sunday night of hacking together with a friend. It represent another step forward in the realisation of the christmas project of this blog, an open source web kiosk. I want the kiosk to be able to print, don’t ask me why. Now the printer works.

image


The ingredients you need to complete this tutorial are : 

I suggest you to buy the starter kit that contains everything you need (but the rpi).

This tutorial is inspired to two articles written by Tobias Floery (1, 2), who we should all thank for his contribution. However his tutorials are not really “noob-proof”, so that’s why I’m writing this down.  Lets divide and conquer the tutorial in three parts :

  1. Connect the printer to the power supply
  2. Connect the printer to the rpi
  3. Print Hello World

Before you start, make sure you have the two cables that comes with the printer.

image

Part 1: Connect the printer to the power supply

First of all don’t forget to turn off the raspberry pi, please: if you are connected to it via ssh you can type 

sudo shutdown -h now

and unplug the power source. Then connect the cables to the back of the printer as shown in the picture below. 

image

In this first phase we can ignore the green/yellow/red and focus on connecting the DC IN (black/red cable) to the power. For this purpose you need to plug the 5V 2A power supply to the 2.1 mm DC jack adapter, both provided with the kit. Afterwards you can connect the jack adapter to the red/black cable as shown in figure below:

image

Now you can plug it to the power adapter, and if everything went fine you can see how the green led on the printer starts to blink. Now open the printer, insert the roll of paper inside the printer (image below) and close it.

image

Press the only button on the printer and see if the paper is rolled out. Does it work? Do it again, and again, just for the thrill of it.

Part 2: Connect the printer to Raspi

The second step is connecting the printer to the rpi through the GPIO pins (read : General Purpose Input/Output). It’s very simple, don’t panic. The GPIO port is located on the board, and it comes with 16 pins, mapped below.

image

To connect the printer we make use of the pin #6 (Ground) and the pin #8 (GPIO14). If you are wondering how this figure can be rotated to match with the rpi, note that the pin #2 is the one located in the corner of the board. 
Now, take the green/yellow/black cable. Since we wanted to be able to use the wires that come with the starter kit without any further soldiering, and it was 3AM at this point, we did something simple and radical: we got rid of the green cable. Cut it out, you don’t need it since the printer is not transmitting any data to the rpi through the TX port ;) .
Now plug the cables to the pin #3 and number #8 of the GPIO, as shown in the detail below.
image

If you didn’t do it before, now simply connect it to the TTL socket in the back of the printer : the black cables goes into GND, and the yellow into the RX. Green dude, rest in peace.

image

Done, we are now ready for the software part of this tutorial. I told you that it wasn’t that hard. Now lets try to make it work.

Part 3: Print Hello World

To interface the rpi with the printer we will use the driver class adapted from arduino and available on github. Its a c++ application based on the Qt framework, hence we need to install some dependencies to make it work. Let’s start installing Qt4 on the rpi by getting some prerequisites (you might already installed some of them): 

sudo apt-get install gcc
 xterm
 git-core
 subversion


And finally the application framework :

sudo apt-get install qt4-dev-tools qtcreator
 


Now we need a component written for Qt that lets the framework communicate with the serial port:  qextserialport. To install it statically in the system clone it and compile as following :


git clone https://code.google.com/p/qextserialport/ 

cd quextserialport

qmake 

sudo make install

cd ../
rm -r quextserialport 


 then download and compile the Qt project for the printer from github . (This will be your working directory from now on).

git clone https://github.com/thefloe1/Raspi-Thermal-Printer.git
cd Raspi-Thermal-Printer 

qmake

make

Sweet! Now we have everything we need to make it work. For the next part, we need to open the Qt framework application user interface, therefore launch X (if you dint’ do it before) :

startx

Now open the File Manager (under the menu Accessories) and navigate to the Raspi-Thermal-Printerfolder downloaded previously, and then to the examples/printertest/ subfolder, which contains the working example. Right click the printest.proTools  and select QRcreator to open the project file with the Qt environment.

Then we need to configure the environment by selecting the menu Tools -> Options then selecting the Build & Run element on the left, and finally the Tools Chains tab. Press the Add button, and chose GCC from the dropdown menu.  Select it in the option window and make sure to insert the following parameters:

compiler path -> /usr/bin/arm-linux-gnueabihf-gcc-4.6 
Debugger : /usr/bin/gdb

mkspec : default

image

Now change tab and click the Qt versions tab, press add and select /usr/bin/qmake-qt4 . Apply changes by pressing the ok button.

Another configuration step is telling Qt to deploy locally and not to a remote linux machine. Select the menu item Help -> About plugins and uncheck Device support -> Remote linux.

Now we need to change some system settings:  close Qt Creator and open the terminal. We need to free the tty port of the GPIO (pin #8). Type 

sudo pico /boot/cmdline.txt 

and replace the content with

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Close the file and save ( Control + x then hit y). Last modification is to set the slot free on boot by editing inittab :

sudo pico /etc/inittab 

 and commenting (#) the line 

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Now reboot raspberry to make the changes effective.

sudo reboot

Ok, I know, it was a little bit boring so far, but now you are ready to print! Let’s try something out. 

Re-open the file Raspi-Thermal-Printer/examples/printertest/printetst.pro with Qt Creator (right click). Find the file printertest.cpp located under Sources in the left column and play with it. This is how you print using different styles. If you want you can edit the file by replacing the p->write part with a simple Hello World: 

p->write("\n Hello, (physical) world!\n");

Save the file. Now press build the project by pressing the big hammer icon located in the bottom left cornet of Qt Creator. 

image

This will create a new folder with the compiled application. Navigate to it (it depends on where the folder is located on your disk). In my case  :

cd ~/Raspi-Thermal-Printer/examples/printertest-build-desktop-Qt_4_8_2__System__Release

Now, call your mum and your best friends over skype, and prepare them to the big event. Open the terminal, and to finally print, type : 

./printertest 

Clap your hands, congratulate yourself, and have huge fun.  In the next posts we will learn how to make more complex stuff and we will write some more code to make printing a little more intuitive.

I hope I dind’t make too many mistakes. If you had any doubts or just a simple “thank you” feed back, please leave a comment full of love at the bottom of this page, so I know that someone actually is reading this stuff. It keeps me motivated to write ;)  

image

Subscribe to this blog via rss.

If you liked this post and you want some more, you can subscribe or ...

Donate Bitcoins

or simply spread the word to makers and hackers like you

Vote on HN

Notes

  1. radiojam11 reblogged this from pikiosk
  2. pikiosk posted this