-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
68 lines (56 loc) · 1.77 KB
/
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
# python3.6
import sys
import random
from time import sleep
from paho.mqtt import client as mqtt_client
broker = ''
port = 1883
client_id = f'python-mqtt-{random.randint(0, 1000)}'
username = ''
password = ''
global value
value = 3
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
if msg.topic == "akku":
value = msg.payload.decode()
print('akku: ' + value)
f = open("transfer_akku.txt", "w")
f.write(value)
elif msg.topic == "batBezug":
value = msg.payload.decode()
print('batBezug: ' + value)
f = open("transfer_batBezug.txt", "w")
f.write(value)
elif msg.topic == "produktion":
value = msg.payload.decode()
print('produktion: ' + value)
f = open("transfer_produktion.txt", "w")
f.write(value)
elif msg.topic == "netzBezug":
value = msg.payload.decode()
print('netzBezug: ' + value)
f = open("transfer_netzBezug.txt", "w")
f.write(value)
client.subscribe("akku")
client.subscribe("batBezug")
client.subscribe("produktion")
client.subscribe("netzBezug")
client.on_message = on_message
def run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
if __name__ == '__main__':
run()