Created
April 6, 2024 05:27
-
-
Save theycallmeloki/84b0d4d357f6bcc70977aae9798a2acb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from escpos.printer import Usb | |
app = Flask(__name__) | |
# Run with sudo | |
# Configure your USB printer | |
# Analog Devices, Inc. H58 Printer settings | |
VENDOR_ID = 0x0456 | |
PRODUCT_ID = 0x0808 | |
INTERFACE = 0 | |
IN_EP = 0x81 # Endpoint for receiving data (typically not used for sending commands) | |
OUT_EP = 0x03 # Endpoint for sending data to the printer | |
# Initialize printer | |
printer = Usb(VENDOR_ID, PRODUCT_ID, in_ep=IN_EP, out_ep=OUT_EP) | |
@app.route('/print', methods=['POST']) | |
def print_message(): | |
"""Receives a POST request with JSON containing the message to be printed.""" | |
data = request.get_json() | |
message = data.get('message') | |
if message is None: | |
return jsonify({"error": "No message provided"}), 400 | |
# Send text to the printer | |
try: | |
printer.text(message + "\n") | |
printer.cut() # Cut the paper after printing | |
return jsonify({"success": "Message printed"}), 200 | |
except Exception as e: | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=5000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment