-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_panel.py
145 lines (120 loc) · 3.38 KB
/
mqtt_panel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python
#
# mqtt_ledpanel.py
#
# Show a sliding text on RGB led panel
# 2014-2017 (c) Sergio Tanzilli - [email protected] - www.tanzolab.it
#
# Multiple panel capability added by Andrea Montefusco 2017,
# Requires ledpanel.ko 2.0
import paho.mqtt.client as mqtt
import json
import time
import sys
import os
from datetime import datetime
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import StringIO
import probe
#Return the PIL color array from a web color (ie #55FF00)
def web2pil(webcolor):
try:
r=int(webcolor[1:3],16)*5/255
g=int(webcolor[3:5],16)*5/255
b=int(webcolor[5:7],16)*5/255
return (r,g,b)
except:
return (1,1,1)
#Return the interface MAC address
def getmac(interface):
try:
mac = open('/sys/class/net/'+interface+'/address').readline()
except:
mac = "00:00:00:00:00:00"
return mac[0:17]
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
global topic
print("Connected with result code "+str(rc))
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global text,font,color,bgcolor,probe,x
print("on_message: %s %s" % (msg.topic,msg.payload))
try:
j = json.loads(msg.payload)
if j["cmd"]=="print":
print j["font"]
print j["color"]
print j["bgcolor"]
print j["text"]
path = os.path.dirname(os.path.realpath(__file__))
font=ImageFont.truetype("%s/fonts/%s" % (path,j["font"]), probe.panel_h)
color=j["color"]
bgcolor=j["bgcolor"]
text=j["text"]
x = probe.panel_w
panel_clear(web2pil(j["bgcolor"]),probe.panel_w,probe.panel_h)
if j["cmd"]=="clear":
pass
except:
print "Message format error"
#Clear the led panel with bgcolor
def panel_clear(bgcolor,w,h):
im = Image.new("RGB", (w,h), bgcolor)
draw = ImageDraw.Draw(im)
out_file = open("/sys/class/ledpanel/rgb_buffer","w")
output = StringIO.StringIO()
draw.rectangle((0,0,w,h),outline=bgcolor,fill=bgcolor)
output.truncate(0)
im.save(output, format='PPM')
buf=output.getvalue()
out_file.seek(0)
out_file.write(buf[13:])
path = os.path.dirname(os.path.realpath(__file__))
print("Running from " + path)
print "Panel size: %d x %d\n" % (probe.panel_w, probe.panel_h)
broker="www.tanzolab.it"
port=1883
topic="tanzolab/mqtt_panel/%s/cmd" % (getmac("wlan0"))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, port, 60)
client.loop_start()
#client.loop_forever()
font = ImageFont.truetype(path + '/fonts/' 'Ubuntu-B.ttf', probe.panel_h)
text=getmac("wlan0")
color="#0000FF";
bgcolor="#000000";
loops=0
im = Image.new("RGB", (probe.panel_w, probe.panel_h), web2pil(bgcolor))
draw = ImageDraw.Draw(im)
draw.fontmode="1" #No antialias
out_file = open("/sys/class/ledpanel/rgb_buffer","w")
output = StringIO.StringIO()
x = probe.panel_w
while True:
width, height = font.getsize(text)
x=x-1
if x < -(width):
if loops==0:
x = probe.panel_w
continue
else:
if loops==1:
break
else:
loops=loops-1
x = probe.panel_w
continue
draw.rectangle((0, 0, probe.panel_w - 1, probe.panel_h), outline=web2pil(bgcolor), fill=web2pil(bgcolor))
draw.text((x,-2), text, web2pil(color),font)
output.truncate(0)
im.save(output, format='PPM')
buf=output.getvalue()
out_file.seek(0)
out_file.write(buf[13:])
out_file.close()