We show you how to connect a Raspberry Pi to a thermal printer and schedule it to print useful data on a routine basis, including 7-day weather reports.
BUILD TIME: 2 HOURS
DIFFICULTY RATING: Intermediate
Printing from the Raspberry Pi is definitely no new thing, however, it is an often overlooked output medium for maker projects. While traditional printers can be quite impractical for projects due to their size and ink cartridge/toner requirements, thermal printers overcome many of these issues.
THE BROAD OVERVIEW
This project started as an experiment to operate a cheap thermal receipt printer from a Raspberry Pi, and to get them working with a few basic scripts to print out useful information like weather, news, etc, on a recurring basis.
The outcome is a fully-functioning Raspberry Pi controlled printer that can be set up to print a weather forecast, the news headlines, or hashtags that trend on Twitter, thanks to freely available APIs. There is plenty of scope to have it print other things as well.
Our build uses a HOIN HOP-H58 Thermal Printer and Raspberry Pi 4, however, you could adapt the project to suit a different printer or RPi board.
We show you how to set up the APIs, and how to code the Pi using various Python scripts.
Thermal printers are a proven technology which have been deployed across the world in cash registers, ATMs, etc. They require specialised thermal paper impregnated with a solid-state mixture of dye and matrix. When the paper is heated by the printhead, the dye reacts with the matrix and permanently becomes coloured. This setup eliminates the need for any external ink or toner but limits the available colours to just the die and the base paper. This works well for printing text and icons but not good for intricate or coloured objects.
Thermal printers consist of three main components: the thermal head, platen, and spring. The thermal head contains heating elements that can be controlled with an electrical current, the heat from these elements then activates the paper’s thermochromic layer exposing the dye. Modern thermal printers can include over 1000 individual heating elements allowing for print resolutions up to 1200 DPI. The platen is a motorised rubber roller that is used to feed the thermal paper under the thermal head with a spring to tension the paper for best results.
This compact design allows thermal printers to print much faster than dot-matrix printers while also being smaller, lighter, and less power-hungry. The thermal paper rolls can also be rapidly replaced, reducing turn-around times. We do need to consider the limited longevity of thermally printed content with the printed material being more susceptible to heat, abrasion, friction, light, and water. In a domestic environment, we can safely assume our content will remain legible for 50 days. This is perfectly fine for our project as we won’t be printing any important information, however, if you were looking to deploy a similar setup in an industrial setting, it would be an important consideration.
Digital thermal printers feature an onboard processor that handles the conversion of our digital file to commands for the printhead and roller. While most thermal printers will come with a Windows driver, very few come with one that is compatible with the Raspberry Pi. While few official drivers exist, there are open-source drivers derived from the original drivers available for the most popular models.
We will be using the inexpensive HOIN HOP-H58 Thermal Printer, which can be found for purchase online. The printer accepts common 58mm thermal paper and can be connected via USB, or Bluetooth. This guide should work with other thermal printers, providing you are handy with Linux and can find working drivers.
We decided to create some Python scripts that we could use to demonstrate the usefulness of a thermal printer for maker projects. We decided on three programs that could fetch useful information from free online sources and print the output. The programs can provide us with a variety of interesting and useful information such as the weather forecast for our location, news headlines, and the top tweets for a certain search term.
These scripts serve as a fantastic starting place for your very own thermal printer project. The scripts can easily be modified to suit your personal needs and requirements with a little bit of Python knowledge.
We will be using the Raspberry Pi 4 Model B 2GB for this project, however, any modern Raspberry Pi with internet connectivity should work. We also paired our Raspberry Pi 4 with the Argon NEO passive case to ensure the Pi remained cool under 24/7 operation.
We decided to go with a passive case to reduce noise, however, a variety of actively cooled cases are also available. You can use any modern microSD card with at least 16GB capacity and class 10 speeds. Some cards come with firmware pre-installed, however, we will be installing ours from scratch to ensure we get the latest version.
|1 x Raspberry Pi 4 Model B (2GB or 4GB)
|1 x Raspberry Pi 4 Power Supply
|1 x 16GB+ microSD Card
|1 x Argon NEO Raspberry Pi 4 Case^
^ Our project uses the Argon NEO from Core Electronics. The alternatives from Jaycar and Altronics are different cases.
We will be installing Raspberry Pi OS with desktop to our microSD card using Raspberry Pi Imager. We can download Raspberry Pi Imager for Windows, macOS, or Ubuntu from https://www.raspberrypi.org/software/
Once we have installed Raspberry Pi Imager, select the Operating System, and the recommended Raspberry Pi OS (32-bit). Insert the microSD card into your host computer and select the device in Raspberry Pi Imager. Once you have selected the microSD card you can click write and the OS will be installed.
We need to configure Raspberry Pi OS to operate in headless mode so that we can control it remotely without the need to attach a monitor and keyboard. To do this, we will configure our WiFi details and enable SSH, which will allow us to remotely connect with our Raspberry Pi over our home network. If you are connecting your Raspberry Pi to the internet with ethernet you can skip the WiFi configuration.
To configure the WiFi settings, you will need to create a wpa_supplicant.conf file. Open Notepad or a similar text editor and copy the following file, replacing ssid and psk with your WiFi network name and password. Save this file as wpa_supplicant.conf by setting the file type to all files in Notepad. The wpa_supplicant.conf file can also be downloaded from our website and edited for use with your network.
ssid="<Name of your wireless LAN>"
psk="<Password for your wireless LAN>"
Copy the wpa_supplicant.conf file to the microSD card home directory. This file will allow the Raspberry Pi to connect to your home WiFi network, but we still need to enable ssh so that it can be accessed remotely. In the same directory, right-click and select New > Text Document. Name the file SSH and delete the .txt extension. If you can not see file extensions, enable the option under the Windows explorer view tab.
Eject the microSD card from the computer and insert it into your Raspberry Pi. If you purchased a case for your Raspberry Pi, now would be a good time to assemble and install it. When you connect the power supply, the device should boot and connect to your WiFi network. Next, find the IP address of the Raspberry Pi so that you can connect to it via SSH.
If you have a modern WiFi system, you may have a mobile app that will give you the IP addresses of connected devices. Otherwise, use a free application like Fing to scan your network for devices. Look for a device called Raspberry Pi. Once you have located the IP address for the Pi, you can connect to it.
We will be using the PuTTY SSH client to connect to the Raspberry Pi.You can download and install the latest version pf PuTTY from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html. Next, enter your Raspberry Pi's IP address in the Host Name field and select Open to establish a connection.
If you receive a PuTTY Security Alert, select Yes to establish the connection.
PuTTY will now open a command-line connection to the Raspberry Pi.
Log into the OS as admin with username pi and password raspberry (note that the password will not be visible in the console). This default login should be changed to reduce the security risk using the passwd command.
We are now ready to configure the thermal printer for use with the Raspberry Pi.
We will be using the Common UNIX Printing System (CUPS) to handle our print jobs and interface with the printer. CUPS will need to be configured to give the default user administrative rights so that we can successfully access the printer.
In the terminal, enter the following two commands to install and grant administrative rights:
sudo apt-get install cups
sudo usermod -a -G lpadmin pi
We can now install the drivers for the thermal printer.
Note: The following instructions are for the HOIN HOP-H58 printer You may need to adjust your install based on the printer you are using.
In our case, we will use the custom drivers created by Okkar Min, which have been modified to work correctly on the Raspberry Pi.
The first step is to power on our printer and connect it to the Raspberry Pi via USB. Once the printer is connected we can download and install the drivers with the following commands:
git clone https://github.com/
chmod +x ./install_H58_driver.sh
The thermal printer should now be configured to work on the Raspberry Pi with CUPS, which will allow us to send print jobs and configure the printer.
To simplify the setup, we have also created an automated script that will perform the steps above, along with installing the Python scripts we will be using and their necessary modules. To run the script, you will need the HOIN HOP-H58 thermal printer attached and powered on. Running the following commands will download and run the installer script.
git clone https://github.com/
sudo chmod +x ./installer.sh
The Python scripts require a program to export the result to a file CUPS could send to the printer. We decided to use the paps text to PostScript converter which takes a text file and converts it to the printer-friendly PostScript file. To install paps, use the following command (paps will already be installed if you used our installation script):
sudo apt-get install paps
The final step is to install the necessary Python modules so that our custom scripts can interact with the APIs they use to fetch the required information. We used pip3 to install the three packages we needed, which were pyowm, tweepy, and requests.
The following command will install them (these modules will already be installed if you used our installation script):
pip3 install pyowm tweepy requests
With all our prerequisites installed and the thermal printer configured, we are ready to create our custom Python scripts. We decided to create three scripts that would print the weather forecast for the upcoming days at a specific location, show the top news headlines, and showcase recent popular tweets for a certain topic. We chose these options because they offered a wide range of options and had existing Python API implementations that we could use.
The weather script uses the free Weather API from the OpenWeatherMap to fetch the 7-day forecast for a set location provided by latitude and longitude coordinates. To use the script, we need to create an OpenWeatherMap account here: https://home.openweathermap.org/users/sign_up
The account confirmation email will contain your unique API key (APPID). You will also need to find the coordinates for your chosen location using a service such as https://www.latlong.net/.
To run the weather script, download the weather.py file. If you used our automated installer, the scripts will already be installed in Raspberry-Pi-Thermal-Printer/code. The scripts can also be downloaded from our website.
To use the program, navigate to the program folder using:
We can now run our script using the python3 command. We will also need to send arguments to the program with our API key, and location. The only mandatory argument is --api which takes our OpenWeatherMap API key. We have three optional arguments for latitude, longitude, and the number of days to return. Latitude is provided as a float with North positive and South negative, for longitude East is positive and West is negative. You can choose to return one to seven days of a weather forecast.
To view all available arguments and their functions we can use:
python3 weather.py --help
The program will default to Sydney (-33.8688, 151.2093) as its location and 3 days as the forecast to return if no arguments are provided. With our printer powered on and connected to the Raspberry Pi, we can call the program using:
python3 weather.py --api API_KEY --lat -XX.XXXX --lon XX.XXXX --days X
This will fetch the weather forecast for the provided location and create a file that can be interpreted by our printer.
The program can be modified to provide more weather information or display the forecast differently. The documentation for the Once Call OpenWeatherAPI we used can be found here: https://openweathermap.org/api/one-call-api
The news headlines script uses the News API to fetch the top headlines from all major publications in Australia. We will need to sign up for a free developer account to get an API access key, by going to https://newsapi.org/register.
Once you have registered for an individual account, you can find your API key on the account page.
Note: This program is also packaged with our automated installer or alternatively can be found on our website.
The program only requires a single argument with our API key. There is also an optional argument to specify the number of headlines to return, which defaults to three.
To view all available arguments and their functions, we can use:
python3 news.py --help
The program will print the top Australian headlines from a variety of publications.
With the thermal printer connected and powered on, we can run the script using the following command:
python3 news.py --api API_KEY --headlines X
The News API is very powerful and can easily be configured to return headlines for a different country or from only certain institutions by modifying our script. The complete documentation for the News API can be found here: https://newsapi.org/docs/endpoints/top-headlines.
The trending tweets script uses the Tweepy Twitter API to fetch the trending tweets for a certain search term or hashtag.
To gain access to the Twitter API you will need a Twitter account and apply for standard hobbyist access at: https://developer.twitter.com/en/apply-for-access.
Once you have been granted access to the API, navigate to the Twitter Developer Dashboard and create a new standalone application. Once you have created the app, copy the API key, and API secret key.
The program requires two arguments with our API key and API secret to authenticate with Twitter and fetch the results. The program also includes two optional arguments for the search term and the number of tweets to return. These default to “Raspberry Pi” and three tweets.
To view all available arguments and their functions, we can use:
python3 tweets.py --help
The program will search Twitter in extended mode for popular tweets which meet the search criteria.
With the thermal printer connected and powered on, run the script using the following command:
python3 tweets.py --key API_KEY --secret API_SECRET --query XXX --tweets X
The Tweepy Twitter API is very customisable and could easily be modified to display different Tweet information or content. The complete Tweepy api documentation can be found here: https://docs.tweepy.org/en/v3.10.0/api.html
The steps we have just described only allow us to run our scripts once when we are remotely connected to our RPi via SSH and can send commands. We can set up the RPi to automatically run our scripts at a set time using cron. Cron is a tool used for scheduling commands and scripts on UNIX systems. All cron entries are made up of the same six components: minute, hour, day of the month, month of the year, day of the week, and the command to be executed.
We can use cron to create scheduled tasks for our scripts.
To create a new entry, edit crontab using the following command:
When asked to select an edit, choose nano by pressing enter. Navigate to the bottom of the file with the down key. You can now enter a cron task on a new line. For example, if you wanted to run the weather program each morning at 7:00 am, use the following entry:
0 0 7 1/1 * python3 /home/pi/Raspberry-Pi-Thermal-Printer/code/weather.py --api API_KEY
You can use an online tool like CronMaker to easily generate cron expressions to run the program at any time you prefer.
Simply select how often you want the script to run and CronMaker will generate the first 5 parts of the cron expression. Simply add the script at the end.
WHERE TO FROM HERE?
Due to limited time and scope for this project, we could only describe three api methods. When planning this project, we had brainstormed many other possibilities for real-world uses of this printer and data service configuration.
These included printing shopping lists, stock/crypto prices, weekly tides, inspirational quotes, fun facts, etc. You may find a completely different purpose for connecting a RPI to a thermal printer.
There's also something fun you can do with thermal printers without much additional work from this fundamental build. Change the media!
There are virtually limitless options for what a thermal printer can print on. Stickers or labels, but you're not limited to white. There's also a huge array of coloured options too!
So don't be afraid to push the envelope - there's plenty of fun opportunities with this fundamental idea! Compatibility will only depend on the printer you use.