当我将它与FLASK

时间:2015-09-09 13:45:31

标签: raspberry-pi

我尝试使用Raspberry Pi的PWM实现控制。我用以下代码成功控制了LED的强度:

# Pin Definitons:
pwmPin = 18 # Broadcom pin 18 (P1 pin 12)

# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(pwmPin, GPIO.OUT) # PWM pin set as output
pwm = GPIO.PWM(pwmPin, 50)  # Initialize PWM on pwmPin 100Hz frequency

# Initial state for LEDs:
pwm.start(10)

try:
    while 1:
    cycle = raw_input("Introduce Duty Cycle")
    dc = int(cycle)
    pwm.ChangeDutyCycle(dc)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
    pwm.stop() # stop PWM
    GPIO.cleanup() # cleanup all GPIO

然后我想通过网页控制LED亮度。为此,我使用了FLASK和之前的代码。当我从网页设置占空比时,LED亮度会相应改变,但会随机波动。 这是一种奇怪的行为,我不知道它为什么会发生。这里是在FLASK上实现的代码:

# Pin Definitons:
pwmPin = 18 # Broadcom pin 18 (P1 pin 12)

# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(pwmPin, GPIO.OUT) # PWM pin set as output
pwm = GPIO.PWM(pwmPin, 50)  # Initialize PWM on pwmPin 100Hz frequency

# Initial state for LEDs:
pwm.start(10)

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('motor.html')

@app.route('/motor', methods=['POST'])
def motor():
    dc = int(request.form['valor'])
    pwm.ChangeDutyCycle(dc)
    return redirect(url_for('index'))


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=81, debug=True) 
    pwm.stop() # stop PWM
    GPIO.cleanup() # cleanup all GPIO

0 个答案:

没有答案