-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchdata.py
More file actions
31 lines (22 loc) · 756 Bytes
/
Copy pathfetchdata.py
File metadata and controls
31 lines (22 loc) · 756 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
import paho.mqtt.client as mqtt
import time
# Broker connection
mqtt_broker = "test.mosquitto.org"
mqtt_port = 1883
mqtt_topic = "ken/data"
in_val = None
def on_connect(client, userdata, flags, rc):
print("Connected to MQTT broker with result code " + str(rc))
client.subscribe(mqtt_topic)
def on_message(client, userdata, msg):
global info_text
print(f"Received message on topic {msg.topic}: {msg.payload.decode()}")
def mqtt_function(mqtt_broker,mqtt_port,mqtt_topic):
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(mqtt_broker, mqtt_port, 60)
client.loop_forever()
while True:
mqtt_function(mqtt_broker,mqtt_port,mqtt_topic)
time.sleep(1)