Created
March 25, 2018 14:48
-
-
Save tak-km/6f93333441ab569076f8b96c5eac79d4 to your computer and use it in GitHub Desktop.
This file contains 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
#! /usr/bin/env python | |
# coding: utf-8 | |
# coding=utf-8 | |
# -*- coding: utf-8 -*- | |
# vim: fileencoding=utf-8 | |
import sys | |
from PIL import ImageFont | |
from PIL import Image | |
from PIL import ImageDraw | |
import io | |
import subprocess | |
import paho.mqtt.client as mqtt | |
import json | |
TOKEN = "YOURTOKEN" | |
HOSTNAME = "mqtt.beebotte.com" | |
PORT = 8883 | |
TOPIC = "YOURCHANNEL/voice" | |
CACERT = "mqtt.beebotte.com.pem" | |
fontFile = 'NotoSansCJKjp-Medium.otf' | |
font = ImageFont.truetype(fontFile, 40, encoding='utf-8') | |
def on_connect(client, userdata, flags, respons_code): | |
print('status {0}'.format(respons_code)) | |
client.subscribe(TOPIC) | |
def on_message(client, userdata, msg): | |
print(msg.topic + " " + str(msg.payload)) | |
data = json.loads(msg.payload.decode("utf-8"))["data"] | |
print(data) | |
text = data | |
w, h = font.getsize(text) | |
img = Image.new('RGB', (400, 50), (255,255,255)) | |
draw = ImageDraw.Draw(img) | |
draw.text((0, 0), text, fill=(0,0,0), font=font) | |
buf = io.BytesIO() | |
img.save(buf, 'PNG') | |
# print out | |
p = subprocess.Popen('lpr', stdin=subprocess.PIPE) | |
p.communicate(buf.getvalue()) | |
client = mqtt.Client() | |
client.username_pw_set("token:%s"%TOKEN) | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.tls_set(CACERT) | |
client.connect(HOSTNAME, port=PORT, keepalive=60) | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment