我使用MQTTLens使用Python脚本发布文本。我有一个ESP8266板连接到MQttLens我可以通过发布1&但是我想从python脚本控制LED,就像我在终端1输入它是ON而0是OFF。
import paho.mqtt.client as mqttClient
import time
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
Connected = False #global variable for the state of the connection
broker_address= "broker.mqtt-dashboard.com"
port = 1883
user = "xxxx"
password = "xxxx"
client = mqttClient.Client("Python") #create new instance
client.username_pw_set(user, password=password) #set username and password
client.on_connect= on_connect #attach function to callback
client.connect(broker_address, port=port) #connect to broker
client.loop_start() #start the loop
while Connected != True: #Wait for connection
time.sleep(0.1)
try:
while True:
value = raw_input('Enter the message:')
client.publish("xxxxx",value)
except KeyboardInterrupt:
client.disconnect()
client.loop_stop()