Moved to Python3, and moved to the new Adafruit Library

This commit is contained in:
N07070 2020-01-16 16:23:27 +01:00
parent 329bd63fd7
commit 6c0dc5a278
2 changed files with 56 additions and 27 deletions

View File

@ -19,7 +19,9 @@ To make this project work, you will need :
## Installation ## 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. 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. 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 : Then, setup the project :
``` ```
@ -43,6 +45,15 @@ If you liked this project, feel free to support my work !
[https://n07070.xyz/post/about-me/about-me/](Donations welcome :-]) [https://n07070.xyz/post/about-me/about-me/](Donations welcome :-])
## Links
- [A blog post about some CUPS configuration](http://scruss.com/blog/2015/07/12/thermal-printer-driver-for-cups-linux-and-raspberry-pi-zj-58/)
- [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 ## Licence
``` ```

View File

@ -3,12 +3,37 @@
import os, random, sys, json import os, random, sys, json
from datetime import datetime from datetime import datetime
from subprocess import call from subprocess import call
from Adafruit_Thermal import *
import serial
from flask import Flask, render_template, jsonify, make_response, request, redirect, url_for, abort, session, flash from flask import Flask, render_template, jsonify, make_response, request, redirect, url_for, abort, session, flash
from flask_limiter import Limiter from flask_limiter import Limiter
from pprint import pprint from pprint import pprint
from flask_limiter.util import get_remote_address from flask_limiter.util import get_remote_address
# Update to using the new Python 3 lib
import board
import busio
import adafruit_thermal_printer
ThermalPrinter = adafruit_thermal_printer.get_printer_class(2.69)
RX = board.RX
TX = board.TX
uart = serial.Serial("/dev/ttyAMA0", baudrate=19200, timeout=3000)
printer = ThermalPrinter(uart, auto_warm_up=False)
# Initialize the printer. Note this will take a few seconds for the printer
# to warm up and be ready to accept commands (hence calling it explicitly vs.
# automatically in the initializer with the default auto_warm_up=True).
printer.warm_up()
if printer.has_paper():
print('Printer has paper!')
else:
print('Printer might be out of paper, or RX is disconnected!')
app = Flask(__name__) app = Flask(__name__)
@ -77,38 +102,31 @@ def logout():
flash('Tu est déconnecté', 'info') flash('Tu est déconnecté', 'info')
return redirect(url_for('login')) return redirect(url_for('login'))
@app.route('/print/image') # @app.route('/print/image')
@limiter.limit("5 per minute", error_message=error_handler_limiter) # @limiter.limit("5 per minute", error_message=error_handler_limiter)
def print_image(): # def print_image():
if session.get('logged_in'): # if session.get('logged_in'):
img = random.choice(os.listdir("static/images/")) #change dir name to whatever # img = random.choice(os.listdir("static/images/")) #change dir name to whatever
# call(["lp", "-o fit-to-page", "static/images/" + img]) # # call(["lp", "-o fit-to-page", "static/images/" + img])
printer = Adafruit_Thermal(SERIAL_PORT, BAUDRATE, timeout=5) # printer = Adafruit_Thermal(SERIAL_PORT, BAUDRATE, timeout=5)
printer.begin() # printer.begin()
printer.feed(1) # printer.feed(1)
printer.printImage("static/images/" + img, True) # printer.printImage("static/images/" + img, True)
printer.feed(2) # printer.feed(2)
return redirect(url_for('display_index_page')) # return redirect(url_for('display_index_page'))
else: # else:
return redirect(url_for('login')) # return redirect(url_for('login'))
@app.route('/print/text', methods=['POST']) @app.route('/print/text', methods=['POST'])
@limiter.limit("3: per minute", error_message=error_handler_limiter) @limiter.limit("3: per minute", error_message=error_handler_limiter)
def print_text(): def print_text():
if session.get('logged_in'): if session.get('logged_in'):
if len(request.form['message']) < 200: if len(request.form['message']) < 200:
printer = Adafruit_Thermal(SERIAL_PORT, BAUDRATE, timeout=5) printer.set_defaults()
printer.begin() printer.print((request.form['message']))
printer.justify('L') printer.print("From" + session['user'])
printer.println((request.form['message']).encode())
printer.setSize('S')
printer.println(">> " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + " " + session.get('user'))
printer.justify('C')
printer.println("------------------------------")
printer.feed(2) printer.feed(2)
printer.sleep() # Tell printer to sleep
printer.wake() # Call wake() before printing again, even if reset
printer.setDefault() # Restore printer to defaults
return redirect(url_for('display_index_page')) return redirect(url_for('display_index_page'))
else: else:
flash('Le text est trop long, 200 caractères au maximum stp !') flash('Le text est trop long, 200 caractères au maximum stp !')