Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch now!Tune in to Hackster Holidays, Ep. 6 now!
Alex Glow
Published

Using a Thermal Printer with Raspberry Pi

Print a file to a thermal printer when you push a button on your RasPi. Get creative!

IntermediateProtip1 hour17,871
Using a Thermal Printer with Raspberry Pi

Things used in this project

Story

Read more

Code

print-ipfs.py

Python
Test printing with a button on GPIO pin 23 – using text, a local image file, a local text file, and a file pulled from the Interplanetary File System!
#!/bin/python

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
os.system("stty -F /dev/serial0 19200")

def Printtest(channel):
	print('Printing...')

# Test printing text & image
#	os.system("echo 'This is a test.' | lp")
#	os.system("lp -o fit-to-page /usr/share/raspberrypi-artwork/raspberry-pi-logo.png")

# Test printing text from file, 18 chars per inch (to fit IPFS ascii art)
#	os.system("lp -o cpi=18 /home/pi/Scripts/welcome.txt")

# Test printing from IPFS! See ipfs.io
	os.system("ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme | lp -o cpi=18")

GPIO.add_event_detect(23, GPIO.FALLING, callback = Printtest, bouncetime = 2000)

while 1:
	time.sleep(1)

printtest.py

Python
Test that you can print text and an image with a single button-press.
#!/bin/python

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP)
os.system("stty -F /dev/serial0 19200")

def Printtest(channel):
	os.system("echo 'This is a test.' | lp")
	os.system("lp -o fit-to-page /usr/share/raspberrypi-artwork/raspberry-pi-logo.png")

GPIO.add_event_detect(23, GPIO.FALLING, callback = Printtest, bouncetime = 2000)

while 1:
	time.sleep(1)

Credits

Alex Glow

Alex Glow

149 projects • 1593 followers
The Hackster team's resident Hardware Nerd. I love robots, music, EEG, wearables, and languages. FIRST Robotics kid.

Comments