Skip to content

Instantly share code, notes, and snippets.

@oiehot oiehot/py_mqtt_1.py
Created Mar 25, 2019

Embed
What would you like to do?
py_mqtt_1.py
# https://mosquitto.org
# https://www.vultr.com/docs/how-to-install-mosquitto-mqtt-broker-server-on-ubuntu-16-04
# https://pypi.org/project/paho-mqtt/
# http://www.steves-internet-guide.com/into-mqtt-python-client/
# $ mosquitto_pub -t "house/main-light" -m "message from mosquitto_pub client" -u "oiehot" -P "*******"
import time
import paho.mqtt.client as mqtt
ID = 'oiehot'
PW = '*******'
MQTT_SERVER = '****************'
MQTT_SERVER_PORT = 1883
MAIN_LIGHT = 'house/main-light'
def on_connect(client, userdata, flags, rc):
print('Connected with result code: ' + str(rc))
client.subscribe(MAIN_LIGHT) # TODO: '$SYS/#' ?
def on_message(client, userdata, msg):
print(msg.topic+' '+str(msg.payload))
def on_log(client, userdata, level, buf):
print("log: ",buf)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_log = on_log
client.username_pw_set(username=ID,password=PW)
client.connect(MQTT_SERVER, MQTT_SERVER_PORT, 60)
# client.loop_start()
# client.publish(MAIN_LIGHT,'OFF')
# time.sleep(4)
# client.loop_stop()
client.loop_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.