Skip to content

Instantly share code, notes, and snippets.

@naoki-sawada
Last active April 29, 2018 00:57
Show Gist options
  • Select an option

  • Save naoki-sawada/2781f4d6479a88986853d8afe903fbbb to your computer and use it in GitHub Desktop.

Select an option

Save naoki-sawada/2781f4d6479a88986853d8afe903fbbb to your computer and use it in GitHub Desktop.
Mqtt demo code

Mqtt Example

About

This is the example code of mqtt.

import paho.mqtt.client as mqtt
import time
client = mqtt.Client()
client.connect('localhost', 1883, keepalive=60)
client.loop_start()
while True:
client.publish('/message', 'test')
time.sleep(1)
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, respons_code):
print('connected')
client.subscribe('/message')
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, keepalive=60)
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment