Sitemap / Advertise

Information



Tags



Share

How to use a tiny thermal printer with Raspberry Pi

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

In this tutorial, I will show you how to print text and QR codes by using a tiny thermal printer with Raspberry Pi.

Code

There are various types of thermal printers with different abilities, but all of them are controlled by similar methods and functions. In this project, I used an embedded (tiny) thermal printer supporting TTL and USB.

You can get more information about the thermal printer I used here.

⭐ First of all, open the Raspberry Pi configuration settings to enable Serial Port.

article_image
Figure - 153.1

⭐ Connect the tiny thermal printer to Raspberry Pi via the USB cable. Then, enter the following command to check the occupied serial ports - folders.

ls /dev/*

article_image
Figure - 153.2

⭐ If the printer is not showing or working on "/dev/ttyUSB0", enter the following command to find which channel it occupies in the usb folder. You should see a folder name like "lp0".

ls /dev/usb*

⭐ Then, to check whether the thermal printer is working precisely or not, enter the commands below.

⭐ Do not forget that you need to set the permission of "/dev/usb/lp0" to 666 to be able to send commands to the printer.

sudo chmod 666 /dev/usb/lp0

echo -e "This is a test message.\\n\\n" > /dev/usb/lp0

article_image
Figure - 153.3

⭐ After that, you can send ESC/POS commands to the thermal printer to change settings such as font size, inverted, and position. Although there are libraries to use ESC/POS commands, sending them directly to the printer is way faster and reduces faint letters and lines. You can download the general instruction set of ESC/POS commands for the thermal printer here.

article_image
Figure - 153.4

⭐ However, printing QR codes with ESC/POS commands alone is perplexing, so you need to download the escpos module to be able to print QR codes easily.

sudo pip3 install python-escpos

article_image
Figure - 153.5

After taking the steps above, the thermal printer should print the given text and the QR codes without a problem when you execute the code.

The depicted code is a part of my electronics project named Web-enabled ML Mask Detection Robot Fines for No Mask w/ Penalty Receipt.


...

    # Via the Thermal Printer, print the fine receipt when detecting people without a mask.
    def print_fine_receipt(self, case_code, fine, due):
        print("\nStatus => Printer Working!")
        # Define character design and font sizes for each line.
        command_thermal_printer = []
        command_thermal_printer.append("sudo chmod 666 /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1B\x45\x01' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x42\x01' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x21\x37' > /dev/usb/lp0")
        command_thermal_printer.append("echo '&&&' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x42\x10' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x21\x24\x0a' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'COVID-19' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x21\x12' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Management' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Violation' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Notice' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x21\x01' > /dev/usb/lp0")
        command_thermal_printer.append("echo '\\nReceipt No:' > /dev/usb/lp0")
        command_thermal_printer.append("echo '" + case_code + "\\n' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Issue Date: " + datetime.datetime.now().strftime('%m-%d-%Y') + "' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Time: " + datetime.datetime.now().strftime('%H:%M:%S') + "' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Amount: " + fine + "' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'Due Date: In " + due + " Days\\n' > /dev/usb/lp0")
        command_thermal_printer.append("printf '\x1D\x21\x09' > /dev/usb/lp0")
        command_thermal_printer.append("echo '(!) You comitted' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'a code' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'violation by not' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'wearing a mask' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'in public.\\n' > /dev/usb/lp0")
        command_thermal_printer.append("echo '(!) To inspect the' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'picture showing' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'you not wearing' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'a mask in public' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'and pay the penalty' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'enter your' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'receipt number' > /dev/usb/lp0")
        command_thermal_printer.append("echo 'to this page:\\n' > /dev/usb/lp0")
        
        # Print each line via the serial port.
        for i in range(len(command_thermal_printer)):
            call([command_thermal_printer[i]], shell=True)
            sleep(0.5)
        # Print QR Code w/ Settings
        thermal_printer = printer.File("/dev/usb/lp0")
        thermal_printer.qr("http://" + self.server + "/Mask_Detection_Robot_Dashboard/Payments/?q=" + case_code, size=4, model=2)
        thermal_printer.cut()
        print("\nStatus => Printer Printed the Receipt!\n\n")

...


Result:

You can inspect my electronics project in which I used the same instructions to print text and QR codes from here.