-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
36 lines (28 loc) · 867 Bytes
/
server.py
File metadata and controls
36 lines (28 loc) · 867 Bytes
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
import paho.mqtt.client as mqtt
import json
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected.")
client.subscribe("Group_05/sensors/#")
else:
print("Failed to connect. Error code:", rc)
def on_message(client, userdata, msg):
# Payload is in msg. We convert it back to a Python dictionary.
recv_dict = json.loads(msg.payload)
# Recreate the data
print("Received data: ", recv_dict)
# Can send for data analytics here
def setup(hostname: str) -> mqtt.Client:
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
# client.connect(hostname)
client.connect("localhost", 1883, 60)
# client.loop_start()
client.loop_forever()
return client
# Test main
def main():
setup("192.168.0.1")
if __name__ == '__main__':
main()