Skip to content

Instantly share code, notes, and snippets.

@oiehot
Created March 25, 2019 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oiehot/6f864737f64603958d56157f292cf98e to your computer and use it in GitHub Desktop.
Save oiehot/6f864737f64603958d56157f292cf98e to your computer and use it in GitHub Desktop.
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