Skip to content

Instantly share code, notes, and snippets.

@wadewegner
Last active January 17, 2022 09:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save wadewegner/8872919 to your computer and use it in GitHub Desktop.
Save wadewegner/8872919 to your computer and use it in GitHub Desktop.
Code used to turn the IoT Thermal Printer into a lunch menu & text message receiver
import pika, os, urlparse, sys
from Adafruit_Thermal import *
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
url_str = os.environ.get('CLOUDAMQP_URL','amqp://YOUR_CLOUDAMQP_URL')
url = urlparse.urlparse(url_str)
params = pika.ConnectionParameters(host=url.hostname, virtual_host=url.path[1:],
credentials=pika.PlainCredentials(url.username, url.password))
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='texts')
def callback(ch, method, properties, body):
printer.inverseOn()
printer.println(' ' + '{:<31}'.format("TXT MESSAGE"))
printer.inverseOff()
printer.println(body)
printer.feed(3)
print("complete")
print " [x] Received %r" % (body)
channel.basic_consume(callback,
queue='texts',
no_ack=True)
try:
channel.start_consuming()
except KeyboardInterrupt:
print "Break detected"
channel.stop_consuming()
connection.close()
sys.exit()
from __future__ import print_function
from Adafruit_Thermal import *
from xml.dom.minidom import parseString
import itertools
import json
import re
import urllib2
import time
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
today = time.strftime("%Y-%m-%d")
text = urllib2.urlopen('http://dcsd.nutrislice.com/menu/eldorado/lunch/').read()
menus = json.loads(re.search(r"bootstrapData\['menuMonthWeeks'\]\s*=\s*(.*);", text).group(1))
days = itertools.chain.from_iterable(menu['days'] for menu in menus)
day = next(itertools.dropwhile(lambda day: day['date'] != today, days), None)
if day:
printer.inverseOn()
printer.print('{:^32}'.format("Lunch menu for " + today))
printer.inverseOff()
menu_items = '\n'.join(item['food']['name'] for item in day['menu_items'])
printer.print(menu_items)
printer.feed(3)
print(menu_items)
else:
print('Day not found.')
# Called once per day (6:30am by default).
# Invokes weather forecast and sudoku-gfx scripts.
def daily():
GPIO.output(ledPin, GPIO.HIGH)
subprocess.call(["python", "forecast.py"])
subprocess.call(["python", "sudoku-gfx.py"])
subprocess.call(["python", "lunch.py"])
GPIO.output(ledPin, GPIO.LOW)
from flask import Flask, request, redirect
import twilio.twiml
import pika, os, urlparse
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def hello():
from_number = request.values.get('From', None)
body = request.values.get('Body', None)
url_str = os.environ.get('CLOUDAMQP_URL','amqp://YOUR_CLOUDAMQP_URL')
url = urlparse.urlparse(url_str)
params = pika.ConnectionParameters(host=url.hostname, virtual_host=url.path[1:],
credentials=pika.PlainCredentials(url.username, url.password))
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='texts')
message = from_number + ": " + body
channel.basic_publish(exchange='', routing_key='texts', body=message)
connection.close()
resp = twilio.twiml.Response()
resp.message("We're so happy! We got your message and it's currently printing. Tx, Wade")
return str(resp)
if __name__ == "__main__":
app.run(debug=True)
@jahidhaque
Copy link

hey mate, I am trying to do the same thing with a excelvan thermal printer and nodejs (https://www.npmjs.com/package/node-thermal-printer-driver). I can connect with printer, it cuts. Only problem is there is no text on that paper. I don't understand what I am doing wrong here. Can you comment, mate please?

@zakaria1089
Copy link

Hi I need help to do the txt msg using twilio to go to printer in my kitchen
Please call +1718.508.2930 or email me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment