Created
March 25, 2019 06:55
-
-
Save oiehot/6f864737f64603958d56157f292cf98e to your computer and use it in GitHub Desktop.
py_mqtt_1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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