Files
Gainsight/Scripts/Todo_reorg/mqtt_test_.py

45 lines
901 B
Python

'''
This was the code provided by this tutorial:
https://techtutorialsx.com/2017/04/14/python-publishing-messages-to-mqtt-topic/
'''
import paho.mqtt.client as mqttClient
import time
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected
Connected = True
else:
print("Connection failed")
Connected = False
broker_address = "192.168.200.55"
port = 1883
user = "hass"
password = "Over9+look*"
client = mqttClient.Client("Python")
client.username_pw_set(user, password=password)
client.on_connect = on_connect
client.connect(broker_address, port=port)
client.loop_start()
while Connected != True:
time.sleep(0.1)
try:
while True:
value = input("Enter the message:")
client.publish("python/test", value)
except KeyboardInterrupt:
client.disconnect()
client.loop_stop()