我试图运行一行代码来打开和关闭我的盲人使用步进电机使用python。在
在成功连接到mqtt服务器之后,我想在payload打开时调用blind_up函数,在payload关闭时调用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()