Thermal Printer
In this project, we’ll be using the Omega to control a thermal printer via a web interface. Simply type text in a box, and click Print to print it out in real life!
Overview
Skill Level: Intermediate
Time Required: 20 minutes
This tutorial will use the Omega to control a thermal printer - most often seen at cash registers and restaurant checkouts. We’ll be using connectors and cables that come with the printer to provide it with power and communicate serially.
Optionally, we’ll 3D print a base to clean up the cabling and give the printer some polish.
Ingredients
- Onion Omega2 or Omega2+
- Any Onion Dock that exposes the Omega’s GPIOs: Expansion Dock, Power Dock, Arduino Dock 2, Breadboard Dock
- Thermal Printer
- comes with a 2-pin JST power cable and a 5-pin TTL cable
- DC barrel jack adapter
- 5V 2A DC Power Supply
- 3D printed base
- Male-to-Male Jumper Wires
Step-by-Step
Follow these steps to turn your Omega into a web-based printer!
1. Prepare
You’ll have to have an Omega2 ready to go, complete the First Time Setup Guide to connect your Omega to WiFi and update to the latest firmware.
2. Wire Up the Thermal Printer
We’ll be doing the following to connect the Omega to the printer:
Let’s dive in:
- First make sure the Omega is off and seated in the Expansion Dock.
- Then, plug in the 2-pin power cable into the left side of the bottom of the printer above.
- Route the black wire to the
GND
pin on the Expansion Dock headers. - Next we’ll connect the serial wires:
- First plug one end of the 5-pin TTL cable into the socket at the bottom of the printer.
- Using a jumper (preferably green to keep it consistent) connect the green wire pin on the TTL connector to the UART1
TX
pin on the Omega Expansion header. - Same goes for the yellow wire pin on the TTL connector, except this one goes to the UART1
RX
pin on the Expansion Header. - Lastly, do the same for the black wire to the
GND
pin on the Expansion Dock header - we used a breadboard intermediary in the diagram to show how the connection is supposed to go.
- Finally, connect the red wire from the JST connector to a
5V
pin on the Expansion Dock headers.
Note that we used a breadboard to connect the two
GND
pins from the printer to a singleGND
pin on the Omega. It would have been equally ok to connect the two printerGND
pins to twoGND
pins on the Omega.
2. Download the Project Code
The code for this project is all done and can be found in Onion’s iot-thermal-printer repo on GitHub.
First, connect to the Omega’s Command line and install git
:
opkg update
opkg install git git-http ca-bundle
And then use git
to download the project code to your Omega:
cd /root
git clone https://github.com/OnionIoT/iot-thermal-printer.git
After cloning the repo, enter the repo directory and copy the contents of the www
to the /www
directory on the Omega:
cd iot-thermal-printer
cp -r www/ /
By virtue of
uhttpd
, the HTTP server running on the Omega, all of the files in the/www
directory will be served up as a website.
Running the Printer
- Connect your Omega to your WiFi network, or connect your computer to the Omega’s WiFi network.
- In a web browser, navigate to
omega-ABCD.local/printer.html
, whereABCD
is the last 4 digits on the sticker on the Omega.- On some Android and PC devices, the
omega-ABCD.local
address doesn’t always work. Follow our guide on finding your Omega’s IP Address and use the IP address instead ofomega-ABCD.local
when connecting the web interface. It will be something along the lines of192.168.1.109/printer.html
- On some Android and PC devices, the
- Type in text in the box in the middle of the webpage.
- Click print to print it!
The physical output:
Code Highlight
This project uses the cgi-bin
method to run scripts on the Omega via a web interface. In the following line, we send the data from the text box to the script in the /cgi-bin
directory using asynchronous JavaScript (AJAX):
$.ajax({
type: "POST",
url: "/cgi-bin/print.sh",
data: $('#printContent').val().split('\n').join('\r'), // <-- We need to replace \n with \r
contentType: 'text/plain'
})
The print.sh
script works like a simple API endpoint that takes data and does something with it; in this case, sending it to the printer via serial:
#!/bin/sh
echo "Content-type: application/json"
echo ""
if [ "$REQUEST_METHOD" = "POST" ]; then
read -n $CONTENT_LENGTH line
echo $line > /dev/ttyS1
# feed paper
echo '' > /dev/ttyS1
fi
echo '{"success":"ok"}'
exit 0
This is just one of many methods to create your own endpoints and services easily and quickly on the Omega!
Going Further
With a little bit of wire splicing and soldering, we can make this project much more compact! Check out the next part for more.