-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7SA_MQTT.py
154 lines (109 loc) · 2.68 KB
/
7SA_MQTT.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
146
147
148
149
150
151
152
153
154
# Initialisierung
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
GPIO.setmode(GPIO.BCM)
# AusgangsPins
levelinput = 26
schieben = 19
shift = 16
GPIO.setup(levelinput,GPIO.OUT,initial=False)
GPIO.setup(schieben,GPIO.OUT,initial=False)
GPIO.setup(shift,GPIO.OUT,initial=False)
# Zahlen
n_0 = "01111110"
n_1 = "00010010"
n_2 = "10111100"
n_3 = "10110110"
n_4 = "11010010"
n_5 = "11100110"
n_6 = "11101110"
n_7 = "00110010"
n_8 = "11111110"
n_9 = "11110110"
aus = "00000000"
dot = "00000001"
n_a = [n_0,n_1,n_2,n_3,n_4,n_5,n_6,n_7,n_8,n_9,aus,dot]
# schieben
def doSchieben():
GPIO.output(schieben,1)
time.sleep(0.001)
GPIO.output(schieben,0)
# Eine Zahl ins Schieberegister
def n_to_s(n_p):
for i in range(8):
GPIO.output(levelinput,int(n_p[i]))
GPIO.output(shift,1)
time.sleep(0.001)
GPIO.output(shift,0)
# Zahl anzeigen
def show_number(msg_zahl):
# ausschalten
n_to_s(n_a[10])
n_to_s(n_a[10])
doSchieben()
# anzeigen
zahl = str(msg_zahl)
if zahl == "p":
zahl = "11"
elif zahl == "a":
zahl = "10"
zahl = int(zahl)
if zahl in range(0, 12):
n_to_s(n_a[int(zahl)])
doSchieben()
# Countdown
def countdown(msg_zahl):
# ausschalten
n_to_s(n_a[10])
n_to_s(n_a[10])
doSchieben()
start = str(msg_zahl)
if int(start) > 99:
start = "99"
# Countdown starten
for i in range(int(start)+1):
zahl = int(start) - i
if len(str(zahl)) == 1:
zahl = "0" + str(zahl)
n_to_s(n_a[int(zahl[1])])
n_to_s(n_a[0])
elif len(str(start)) == 2:
zahl = str(zahl)
n_to_s(n_a[int(zahl[1])])
n_to_s(n_a[int(zahl[0])])
doSchieben()
time.sleep(0.99)
# blinken
for i in range(2):
n_to_s(n_a[10])
n_to_s(n_a[10])
doSchieben()
time.sleep(0.4)
n_to_s(n_a[0])
n_to_s(n_a[0])
doSchieben()
time.sleep(0.4)
# ausschalten
n_to_s(n_a[10])
n_to_s(n_a[10])
doSchieben()
# MQTT
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe("7-SA/#")
def on_message(client, userdata, msg):
msg_len = (len(str(msg.payload)))-3
message = ""
for i in range(msg_len):
message = message + str(msg.payload)[2+i]
if msg.topic == "7-SA/Zahl":
show_number(message)
elif msg.topic == "7-SA/Countdown":
countdown(message)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.x.x", 1883, 60)
client.loop_forever()
GPIO.cleanup()