candale icon

TMP_Mqtt

candale | PRO | 02/18/16 02:23:04 PM UTC | 0 ⭐ | 240 👁️ | Never ⏰ | []
text |

980 B

|

None

|

0 👍

/

0 👎

import time
 import paho.mqtt.client as mqtt
  # The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
  client = mqtt.Client()
client.on_connect = on_connect
 client.connect("localhost", 1883, 60)
client.loop_start()
 counter = 0
while True:
    client.publish("lol/data", counter)
    counter += 1
    time.sleep(1)
==============================================================================
import paho.mqtt.client as mqtt
  def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("lol/data")
    client.subscribe("$SYS/broker/subscriptions/#")
  def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
  client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
 client.connect("localhost", 1883, 60)
client.loop_forever()

Comments