-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.py
executable file
·52 lines (40 loc) · 1.55 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: <utf-8> -*-
#Author: Callum Pritchard, Joachim Hummel
#Project Name: Scroll-Phat
#Project Description: Getting data from MQTT and displaying it on the scroll-phat
#Version Number: 0.5
#Date: 10/5/17
#Release State: Beta testing
#Changes: Changed the replace to a range to output as to avoide replacing the letter B
#needed commands
#pip3 install paho-mqtt
#curl https://get.pimoroni.com/scrollphat | bash
import paho.mqtt.client as mqtt
import time
import scrollphat
global output
output = ''
def on_connect(client, userdata, flags, rc):
print("Connected with result code " +str(rc))
client.subscribe('#')
def on_message(client, userdata, msg): #triggers on an update
scrollphat.clear()
global output
if str(msg.topic) == 'msg/sensor1': #name of the publisher
print(str(msg.payload)[2:len(str(msg.payload))-1])
output = str(msg.payload)[2:len(str(msg.payload))-1] + ' '
#since the loop cannot be here, the text needs to looped in itself so it can be displayed
#until a new value is sent via mqtt
scrollphat.set_rotate(True)
scrollphat.set_brightness(2)
client = mqtt.Client()
client.connect("mqtt.unixweb.de",1883,60) #connects to the broker
client.on_connect = on_connect
client.on_message = on_message
while True:
client.loop()
scrollphat.write_string(output, 11) #writes to display starting at position 11 on x axis
for i in range(len(output) + 3):
scrollphat.scroll() #initiates the scrolling
time.sleep(0.1)