-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_mqtt.py
46 lines (37 loc) · 1.33 KB
/
sub_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
import paho.mqtt.client as mqtt
import random
import streamlit as st
data_list = []
time_list = []
def pub():
with st.empty():
def on_connect(client_1, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
def on_message(client_1, userdata, msg):
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
if len(data_list) >= 100:
data_list.pop(0)
data_list.append(float(msg.payload.decode()))
st.line_chart(data_list)
client_id = f'python-mqtt-{random.randint(0, 100)}'
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, client_id)
client.username = "root"
client.password = "Whc@20010311"
client.on_connect = on_connect
client.connect("113.125.84.107", port=1883)
topic = "/public/pz_esp32/1"
client.subscribe(topic)
client.on_message = on_message
client.loop_forever()
try:
while True:
pass
except KeyboardInterrupt:
print("Interrupted by user, exiting...")
client.disconnect()
# streamlit run s
if __name__ == '__main__':
pub()