脉冲是您在代码中控制伺服位置的方式:
wiringpi.pwmWrite(18,pulse)
请参阅此示例:
https ://learn.adafruit.com/adafruits-raspberry-pi-lesson-8-using-a-servo-motor?view=all
根据您的伺服器,100 的脉冲值将伺服器一直向左移动(在本例中关闭),200 一直向右移动(在本例中打开)。您需要通过阅读数据表或通过实验找到这些值。一旦你有了这些,这里是你如何设置伺服的位置:
min_servo_val = 100
max_servo_val = 200
wiringpi.pwmWrite(18, min_servo_val) # Move all the way left
time.sleep(1) # Wait a second
wiringpi.pwmWrite(18, max_servo_val) # Move all the way right
现在您可以编写一个函数,将温度转换为 min_servo_val 和 max_servo_val 之间的伺服位置,或者使用简单的 if 语句。这是将温度转换为脉冲(伺服位置)的函数示例:
def get_servo_position(temp):
min_servo_val = 100 # Pulse value at which window is all the way closed closed
max_servo_val = 200 # Pulse value at which window is all the way open
full_closed_temp = 20.0 # Temperature at which window is completely closed
full_open_temp = 26.0 # Temperature at which window is completely open
if temp <= full_closed_temp:
return min_servo_val
elif temp >= full_open_temp:
return max_servo_val
else:
return ((temp - full_closed_temp) / (full_open_temp - full_closed_temp)) * (max_servo_val - min_servo_val) + min_servo_val
现在你可以这样做:
print get_servo_position(19) # 100 all the way closed
print get_servo_position(22) # 133.3, or 33% of the way between min_servo_val and max_servo_val
print get_servo_position(25) # 183.3, or 83% of the way between min_servo_val and max_servo_val
print get_servo_position(27) # 200 all the way open
现在您需要一个循环,每十分钟检查一次温度并调整伺服位置。像这样的东西:
while True:
humidity, temp = Adafruit_DHT.read_retry(sensor, pin) # Get temperature.
position = get_servo_position(temp) # Get the servo position
wiringpi.pwmWrite(18,position) # Move to the position
time.sleep(10*60) # Wait 10 minutes
把它们放在一起,你的脚本应该看起来像这样:
import sys
import Adafruit_DHT
import time
import wiringpi
dht_pin = 17 # GPIO conencted to DHT
servo_pin = 18 # GPIO connected to servo
dht_sensor = Adafruit_DHT.DHT11 # Put your sensor here, or set it from command line args
min_servo_val = 100 # Pulse value at which window is all the way closed closed
max_servo_val = 200 # Pulse value at which window is all the way open
full_closed_temp = 20.0 # Temperature at which window is completely closed
full_open_temp = 26.0 # Temperature at which window is completely open
def get_servo_position(temp):
if temp <= full_closed_temp:
return min_servo_val
elif temp >= full_open_temp:
return max_servo_val
else:
return ((temp - full_closed_temp) / (full_open_temp - full_closed_temp)) * (max_servo_val - min_servo_val) + min_servo_val
def main_loop():
while True:
humidity, temp = Adafruit_DHT.read_retry(dht_sensor, dht_pin) # Get temperature.
position = get_servo_position(temp) # Get the servo position
wiringpi.pwmWrite(18,position) # Move to the position
time.sleep(10*60) # Wait 10 minutes
if __name__ == '__main__':
# If you need to get command line arguments, do it below, otherwise just set the pins and other settings at the top of this script.
# For example...
# dht_pin = sys.argv[1]
# servo_pin = sys.argv[2]
# Set up servo
wiringpi.wiringPiSetupGpio()
wiringpi.pinMode(18,wiringpi.GPIO.PWM_OUTPUT)
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)
wiringpi.pwmSetClock(192)
wiringpi.pwmSetRange(2000)
# Enter main loop
main_loop()
请注意,我省略了命令行 arg 解析。如果您需要这些,您可以在之后立即添加它们if __name__ == '__main__':
最后,让脚本在启动时运行是一个很好的话题:
在 Raspberry Pi 启动时启动 shell 脚本