我正在尝试运行代码行来使用python的步进电机打开和关闭我的盲人。
成功连接到mqtt服务器后,如果有效负载为ON,我想调出一个blind_up函数,当有效负载为OFF时,我想调用blind_down。
感谢您的帮助。
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
pinListUp = [4, 17, 27, 22]
pinListDown = [22, 27, 17, 4]
def blind_up():
for pin in pinListUp:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
seq = [ [1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1] ]
for i in range(512):
for halfstep in range(8):
for pin in range(4):
GPIO.output(pinListUp[pin], seq[halfstep][pin])
time.sleep(0.001)
def blind_down():
for pin in pinListDown:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, 0)
seq = [ [1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1],
[1,0,0,1] ]
for i in range(512):
for halfstep in range(8):
for pin in range(4):
GPIO.output(pinListDown[pin], seq[halfstep][pin])
time.sleep(0.001)
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("switch/bedroom/blind")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if msg.payload == "ON":
blind_up
if msg.payload == "OFF":
blind_down
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.21", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
GPIO.cleanup()
client.loop_forever()
答案 0 :(得分:2)
您只需要在函数调用中添加括号:
if msg.payload == "ON":
blind_up() # fix this line
if msg.payload == "OFF":
blind_down() # and this line