Press enter to see results or esc to cancel.

Python MQTT Subscribe Kodu

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):

if rc == 0:

print ("MQTT OK.")
client.subscribe("sensor/balkon/sicaklik")
client.subscribe("sensor/balkon/nem")
else:
print (rc)

def on_message(client, userdata, message):

if message.topic == "sensor/balkon/sicaklik":

s1 = str(message.payload.decode("utf-8"))
print ("Sicaklik :",s1.split(".")[0])

if message.topic == "sensor/balkon/nem":
s2 = str(message.payload.decode("utf-8"))
print ("Nem :", s2.split(".")[0])

client = mqtt.Client("Python1")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="esp1", password="000000")

try:

client.connect("mqtt.murat.ws", 1883, 60)

except:
print ("Baglanti Hatasi.")

try:

client.loop_forever()

except KeyboardInterrupt:

client.loop_stop()
client.disconnect()