Compare commits
15 Commits
adafruit-t
...
35fc990bc0
| Author | SHA1 | Date | |
|---|---|---|---|
| 35fc990bc0 | |||
| 47e89cc295 | |||
| b2593a5a97 | |||
|
|
c9a42a7b8b | ||
|
|
4ed11cb078 | ||
|
|
55b763dfe8 | ||
|
|
e6a046d4e6 | ||
|
|
8153bfd9ca | ||
|
|
5ab316e9da | ||
|
|
3fde3fd8ae | ||
|
|
c38faa940f | ||
|
|
af267a20f7 | ||
|
|
b58748e671 | ||
|
|
fbc6a09e60 | ||
|
|
e7a1e8a61f |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -24,6 +24,9 @@ wheels/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
bin/
|
||||
share/
|
||||
pyvenv.cfg
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
|
||||
38
README.md
38
README.md
@@ -3,6 +3,9 @@
|
||||
|
||||
> Print out shit from the cloud.
|
||||
|
||||

|
||||
[CC-BY oomlout](https://commons.wikimedia.org/wiki/File:TPRI-01_(14335248671).jpg)
|
||||
|
||||
## About
|
||||
|
||||
This project started when I got a Thermal Printer from a friend. I don't really know if you can do anything more, but I guess it's fun.
|
||||
@@ -11,39 +14,57 @@ This project started when I got a Thermal Printer from a friend. I don't really
|
||||
## Requirements
|
||||
|
||||
To make this project work, you will need :
|
||||
- [A Thermal Printer](https://www.adafruit.com/product/597)
|
||||
- A Raspberry Pi
|
||||
- A Thermal Printer :
|
||||
- I used Adafruit's thermal printer at first, but it broke down. You can get it [here](https://www.adafruit.com/product/597).
|
||||
- I then used a EPSON TM-T2OIII. It's expensive, but it's also high quality, and has better CUPS and specific library support.
|
||||
- A Raspberry Pi or any computer with USB or Serial.
|
||||
- Some electric wires.
|
||||
- Some knowledge of the command line
|
||||
- 3h of your time
|
||||
- Some knowledge of the command line,
|
||||
- Some knowledge of Python.
|
||||
- 3h of your time, 5h if things need debugging.
|
||||
- `git`, `virtualenv`,`pip` and `python` >= 3.8.6.
|
||||
|
||||
## Installation
|
||||
|
||||
Start by following the guide [here](https://learn.adafruit.com/networked-thermal-printer-using-cups-and-raspberry-pi) to install the CUPS software needed to print images. If you want, you can install it via the command line, [following this guide](https://help.ubuntu.com/lts/serverguide/cups.html).
|
||||
For the EPSON TM-T20III, you can get the CUPS driver from [here](https://download.epson-biz.com/modules/pos/index.php?page=soft&scat=32). It's not specific to the printer I've been using, so you can try with other printers.
|
||||
|
||||
With that, you can try out your printer and print normal text, images or pdf documents for example. LittlePrynter itself does not require any other software than the ones installed with it, i.e the ones listed in the `requirements.txt` file.
|
||||
|
||||
|
||||
|
||||
For the Adafruit printer, start by following the guide [here](https://learn.adafruit.com/networked-thermal-printer-using-cups-and-raspberry-pi) to install the CUPS software needed to print images. If you want, you can install it via the command line, [following this guide](https://help.ubuntu.com/lts/serverguide/cups.html).
|
||||
|
||||
You can also get some information from [here](https://learn.adafruit.com/mini-thermal-receipt-printer) and [here](https://learn.adafruit.com/instant-camera-using-raspberry-pi-and-thermal-printer) if you're stuck.
|
||||
|
||||
Then, setup the project :
|
||||
```
|
||||
git clone https://git.n07070.xyz/n07070/LittlePrynter
|
||||
virtualenv LittlePrynter
|
||||
cd LittlePrynter
|
||||
source bin/activate
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
Now, edit `the users.json` and add a user. **Don't forget to remove the test user.**
|
||||
|
||||
> tip : when you're done, you can get out of the virtualenv either by closing your terminal, or by running `deactivate`.
|
||||
|
||||
You should see a folder named `configuration`. Enter it, and duplicate the file named `config.toml.sample`, and rename the copy to `config.toml`. Now, edit this file by following the comments in the file itself.
|
||||
|
||||
You can now start the web server with
|
||||
|
||||
```
|
||||
export FLASK_APP=littleprynter.py
|
||||
export FLASK_APP=main.py
|
||||
flask run
|
||||
```
|
||||
|
||||
This command should start a web server with which you can test your configuration. If you plan on exposing your printer to the Internet, and give it an IP / URL, _please, please, please_, don't run it this way. Look at Flask's documentation and read about running a production server. It's a little more work, but it will prevent your computer/server being hacked in too easily.
|
||||
|
||||
Voilà !
|
||||
|
||||
## More
|
||||
|
||||
If you liked this project, feel free to support my work !
|
||||
|
||||
[https://n07070.xyz/post/about-me/about-me/](Donations welcome :-])
|
||||
[Donations welcome ! :)](https://n07070.xyz/about-me)
|
||||
|
||||
## Links
|
||||
|
||||
@@ -51,7 +72,6 @@ If you liked this project, feel free to support my work !
|
||||
- [Github repo with CUPS drivers for the Adafruit Thermal Printer ( zj-58 )](https://github.com/klirichek/zj-58)
|
||||
- [A link to buy one in Europe](https://rlx.sk/sk/various-boards/1829-mini-thermal-receipt-printer-adafruit-597.html)
|
||||
- [Another link to buy one, direct from factory](https://www.cashinotech.com/csn-a2-58mm-mini-panel-thermal-receipt-printer_p11.html)
|
||||
-
|
||||
|
||||
|
||||
## Licence
|
||||
|
||||
14
configuration/config.toml
Normal file
14
configuration/config.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
# Configuration file the LittlePrynter
|
||||
|
||||
# Printer settings
|
||||
[printer]
|
||||
vendor_id = 0x04b8
|
||||
device_id = 0x0e28
|
||||
|
||||
# Users = Password
|
||||
[users]
|
||||
admin = "admin"
|
||||
|
||||
# Secrets for flask
|
||||
[secrets]
|
||||
flask_secret_key = "ChÕÓhÑLa)Ù<î=F;ÑÊÝ5fV÷}º35+FâXg£Ì;Ñ58ëȤ^ìãtzº<$úÌ¢ü>A¡ÿ:--_Uc²æmÇ)ëÛòöéZ¥÷Z¯¦`,ç¶B°>Å:Zÿ¼Á@ªu-º´¬XäwYÃ`c>YÜYÏJ${_f{MJ[ÙkNzg_WZv"
|
||||
15
configuration/config.toml.sample
Normal file
15
configuration/config.toml.sample
Normal file
@@ -0,0 +1,15 @@
|
||||
# Configuration file the LittlePrynter
|
||||
|
||||
# Printer settings
|
||||
[printer]
|
||||
vendor_id = "0x04b8"
|
||||
device_id = "0x0e28"
|
||||
|
||||
# Users = Password
|
||||
[users]
|
||||
admin = "admin"
|
||||
|
||||
# Secrets for flask
|
||||
# See "How to generate good secret keys" in https://flask.palletsprojects.com/en/1.1.x/quickstart/
|
||||
[secrets]
|
||||
flask_secret_key = "random string to do things"
|
||||
BIN
configuration/hello_world.png
Normal file
BIN
configuration/hello_world.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 62 KiB |
BIN
dither_it_cover.png
Normal file
BIN
dither_it_cover.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
BIN
docs/Epson TM-T20 ESC-POS Quick Reference Guide .pdf
Normal file
BIN
docs/Epson TM-T20 ESC-POS Quick Reference Guide .pdf
Normal file
Binary file not shown.
BIN
docs/Epson-TM-T20-Series-datasheet.pdf
Normal file
BIN
docs/Epson-TM-T20-Series-datasheet.pdf
Normal file
Binary file not shown.
BIN
docs/tm-t20iii_trg_en_reva.pdf
Normal file
BIN
docs/tm-t20iii_trg_en_reva.pdf
Normal file
Binary file not shown.
BIN
docs/tmx-cups-src-ThermalReceipt-3.0.0.0.tar.gz
Normal file
BIN
docs/tmx-cups-src-ThermalReceipt-3.0.0.0.tar.gz
Normal file
Binary file not shown.
@@ -1,13 +1,13 @@
|
||||
Adafruit-Thermal==1.1.0
|
||||
Click==7.0
|
||||
Flask==1.0.2
|
||||
Flask-Limiter==1.0.1
|
||||
itsdangerous==0.24
|
||||
Jinja2==2.10
|
||||
limits==1.3
|
||||
MarkupSafe==1.0
|
||||
Pillow==5.3.0
|
||||
pyserial==3.4
|
||||
six==1.11.0
|
||||
Unidecode==1.0.22
|
||||
Werkzeug==0.14.1
|
||||
Adafruit-Thermal>=1.1.0
|
||||
Click>=7.0
|
||||
Flask>=1.0.2
|
||||
Flask-Limiter>=1.0.1
|
||||
itsdangerous>=0.24
|
||||
Jinja2>=2.10
|
||||
limits>=1.3
|
||||
MarkupSafe>=1.0
|
||||
Pillow>=5.3.0
|
||||
pyserial>=3.4
|
||||
six>=1.11.0
|
||||
Unidecode>=1.0.22
|
||||
Werkzeug>=0.14.1
|
||||
|
||||
72
src/main.py
Normal file
72
src/main.py
Normal file
@@ -0,0 +1,72 @@
|
||||
# Welcome to the LittlePrynter's source code.
|
||||
# This program expose a web interface, with user authentification, that makes it possible to print messages from the web.
|
||||
# It also exposes a API, making it possible to print and interface with much of the printer's abilities.
|
||||
|
||||
# We first define the connection to the printer itself,
|
||||
# Then we build the API around Flask,
|
||||
# Then we build the web interface, using the simple Jinja2 templating.
|
||||
|
||||
# Following are the librairies we import,
|
||||
from flask import Flask, request # Used for the web framework
|
||||
from printer import Printer # The wrapper for the printer class
|
||||
import toml # Used for the config file parsing
|
||||
import pprint
|
||||
import time
|
||||
|
||||
# Load the configuration file
|
||||
try:
|
||||
configuration_file = toml.load("configuration/config.toml")
|
||||
except TypeError :
|
||||
print("Unable to load the config file: invalid type or is a list containing invalid types")
|
||||
exit(-1)
|
||||
except toml.TomlDecodeError:
|
||||
print("An error occured while decoding the file")
|
||||
exit(-1)
|
||||
except Exception as e:
|
||||
print("Error while loading file : " + str(e))
|
||||
|
||||
pprint.pprint(configuration_file)
|
||||
|
||||
# We define the app module used by Flask
|
||||
app = Flask(__name__)
|
||||
app.secret_key = configuration_file["secrets"]["flask_secret_key"]
|
||||
|
||||
|
||||
# Printer connection
|
||||
# Uses the class defined in the printer.py file
|
||||
|
||||
# Define the USB connections here.
|
||||
vendor_id = configuration_file["printer"]["vendor_id"]
|
||||
device_id = configuration_file["printer"]["device_id"]
|
||||
|
||||
printer = Printer(app,0x04b8, 0x0e28)
|
||||
printer.init_printer()
|
||||
|
||||
|
||||
|
||||
# API routes
|
||||
# The api has the following methods
|
||||
# api/print/{sms,img,letter,qr,barcode}
|
||||
# api/auth/{login,logout}
|
||||
# api/status/{paper,ping,stats}
|
||||
|
||||
# If you just call the api route, you get a help back.
|
||||
@app.route('/')
|
||||
def index():
|
||||
return "Welcome ! Please type your message in the URL bar in such way : /api/print/sms?txt=<message here>"
|
||||
|
||||
|
||||
@app.route('/api')
|
||||
def api_help():
|
||||
return "Welcome to the API's help page"
|
||||
|
||||
@app.route('/api/print')
|
||||
def api_index():
|
||||
return "Welcome to the printing software's API. Please see the index page to get started."
|
||||
|
||||
@app.route('/api/print/sms', methods=['GET'])
|
||||
def api_print_sms():
|
||||
texte = request.args.get("txt",type=str)
|
||||
app.logger.debug("Printing : " + texte)
|
||||
time.sleep(10)
|
||||
return printer.print_sms(texte)
|
||||
117
src/printer.py
Normal file
117
src/printer.py
Normal file
@@ -0,0 +1,117 @@
|
||||
# Importing the module to mage the connection to the printer.
|
||||
from escpos.printer import Usb, USBNotFoundError
|
||||
from time import sleep, gmtime, strftime
|
||||
import os.path
|
||||
|
||||
|
||||
class Printer(object):
|
||||
"""
|
||||
# The connection is based on the ESC/POS library
|
||||
|
||||
## Connection to the USB printer
|
||||
|
||||
## Making sure the printer is alive
|
||||
|
||||
## Making sure it has paper
|
||||
|
||||
## Define default print settings
|
||||
|
||||
## Print starting message, log time of first print, cut.
|
||||
|
||||
## Annonce readyness : return a positive pong message.
|
||||
"""
|
||||
|
||||
# Is the printer ready to accept a new print ?
|
||||
ready = False
|
||||
|
||||
def __init__(self, app, device_id, vendor_id):
|
||||
super(Printer, self).__init__()
|
||||
self.app = app
|
||||
self.ready = False
|
||||
self.printer = None
|
||||
self.device_id = device_id
|
||||
self.vendor_id = vendor_id
|
||||
self.usb_args = {}
|
||||
self.usb_args['idVendor'] = self.device_id
|
||||
self.usb_args['idProduct'] = self.vendor_id
|
||||
|
||||
def init_printer(self):
|
||||
|
||||
# Is the printer online ? Is the communication with the printer successfull ?
|
||||
waiting_elapsed = 30
|
||||
self.app.logger.debug('Waiting for printer to get online...')
|
||||
|
||||
while not self.ready:
|
||||
try:
|
||||
# This also calls open(), which we need to close()
|
||||
# or else the device will appear as busy.
|
||||
p = Usb(self.device_id, self.vendor_id, 0, profile="TM-P80")
|
||||
except USBNotFoundError as e:
|
||||
self.app.logger.error("The USB device is not plugged in, trying again : " + str(e))
|
||||
pass
|
||||
|
||||
try:
|
||||
if p.is_online():
|
||||
self.ready = True
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
sleep(1)
|
||||
waiting_elapsed -= 1
|
||||
if waiting_elapsed < 1:
|
||||
self.app.logger.error('Printer took more than 30 seconds to get online, aborting...')
|
||||
waiting_elapsed = 30 # Reset the waiting time for the next print.
|
||||
return False
|
||||
|
||||
|
||||
# Let's check paper status.
|
||||
ps = p.paper_status()
|
||||
if ps == 0:
|
||||
self.app.logger.error('Printer has no more paper, aborting...')
|
||||
return False
|
||||
elif ps == 1:
|
||||
self.app.logger.warning('Printer needs paper to be changed very soon ! ')
|
||||
sleep(5)
|
||||
elif ps == 2:
|
||||
self.app.logger.debug('Printer has paper, good to go')
|
||||
|
||||
# Setting up the printing options.
|
||||
p.set(align='center', font='a', bold=False, underline=0, width=1, height=1, density=9, invert=False, smooth=False, flip=False, double_width=False, double_height=False, custom_size=False)
|
||||
# Beware : if we print this every time the printer becomes ready, it means
|
||||
# we are printing before and after every print !
|
||||
|
||||
self.printer = p;
|
||||
self.ready = True;
|
||||
self.printer.close();
|
||||
return True
|
||||
|
||||
def print_sms(self, msg):
|
||||
clean_msg = str(msg)
|
||||
|
||||
if len(clean_msg) > 256 or len(clean_msg) < 10 :
|
||||
self.app.logger.warning("Could not print message of this length :" + str(len(clean_msg)))
|
||||
return "Could not print message of this length :" + str(len(clean_msg)) + ", needs to be 10 < x < 256"
|
||||
else :
|
||||
self.printer.open(self.usb_args);
|
||||
self.printer.textln(strftime("%Y-%m-%d %H:%M:%S", gmtime()))
|
||||
self.printer.textln(clean_msg)
|
||||
self.printer.cut()
|
||||
self.printer.close()
|
||||
return "Message printed : " + clean_msg
|
||||
|
||||
def print_img(self, path):
|
||||
|
||||
if os.path.isfile(str(path)):
|
||||
pass
|
||||
else:
|
||||
self.app.logger.warning("File does not exist : " + str(path))
|
||||
|
||||
try:
|
||||
self.printer.open(self.usb_args)
|
||||
self.printer.image(path)
|
||||
self.printer.close()
|
||||
except Exception as e:
|
||||
self.printer.close()
|
||||
return e
|
||||
else:
|
||||
self.app.logging.debug("Printed an image : " + str(path))
|
||||
12
src/web.py
Normal file
12
src/web.py
Normal file
@@ -0,0 +1,12 @@
|
||||
class web(object):
|
||||
"""docstring for web."""
|
||||
|
||||
def __init__(self, arg):
|
||||
super(web, self).__init__()
|
||||
self.arg = arg
|
||||
|
||||
def login_page():
|
||||
return "login page"
|
||||
|
||||
def logout_page():
|
||||
return "logout"
|
||||
Reference in New Issue
Block a user