简体   繁体   中英

RE: How Can I Get Flask to Use GPIO Pins to Change the LED Brightness on Linux with a BeagleBone Black?

I am using Python, Adafruit_BBIO for GPIO and PWM, Flask, and a BeagleBone Black. With all these tools and info, I have been following along in a book, "Getting Started with BeagleBone" (Richardson 2014).

With this in mind, here is my software from the text:

from flask import Flask, render_template
app = Flask(__name__)
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.PWM as PWM

PWM.start("P8_13", 0.0)

@app.route("/")
def hello():
    if GPIO.input("P8_11"):
        doorStatus = "open"
    else:
        doorStatus = "closed"
    templateData = {
        'doorStatus': doorStatus,
    }
    return render_template('main-door.html', **templateData)

@app.route('/ledLevel/<level>')
def pin_state(level):
    PWM.set_duty_cycle("P8_13", float(level))
    return "LED level set to " + "."

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True)

So...I have:

Flask: v0.12.2 Python: v2.7.13

I am using the 4.9.x kernel from a Debian Distro, Stretch.

Here is my print out of the info. for the error when running the software:

![Image of Issue][1]

The terminal goes to the debugger online. I check the online debugger out and the above link is what is produced.

Seth

PS NameError: Global Name P8_11 is not defined.

from flask import Flask, render_template
app = Flask(__name__)
import Adafruit_BBIO.GPIO as GPIO
import Adafruit_BBIO.PWM as PWM

GPIO.setup("P8_19", GPIO.OUT)
PWM.start("P8_11", 0.0)

@app.route("/")
def hello():
    if GPIO.input("P8_19"):
        doorStatus = "open"
    else:
        doorStatus = "closed"
    templateData = {
        'doorStatus': doorStatus,
    }
    return render_template('main-door.html', **templateData)

@app.route('/ledLevel/<level>')
def pin_state(level):
    PWM.set_duty_cycle("P8_11", float(level))
    return "LED level set to " + "."

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000, debug=True) 

This updated software should work if checked. I did not listen enough to the debugger online.

Seth

PS If you need extra support for Adafruit_BBIO, please check on https://github.com/adafruit/adafruit-beaglebone-io-python .

There was a bug in the PWM pins on Debian Stretch for Adafruit_BBIO.

Seth

PS Backup to kernel 4.4.x instead of using kernel 4.9.x.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM