cyberhero 发表于 2018-10-1 04:33:18

龍芯派web控制gpio


龍芯派運行python flask web服務器,訪問頁面可讀取和控制gpio狀態
pip install flask
pip install python-periphery
pip install Werkzeug
pip install jinja2
pip install click
pip install itsdangerous
創建app.py
from periphery import GPIO
from flask import Flask, render_template, request
app = Flask(__name__)
# Create a dictionary called pins to store the pin number, name, and pin state:
pins = {
    55 : {'name' : 'GPIO 55', 'state' : False}
    }
# Set each pin as an output and make it low:
for pin in pins:
    gpio_out = GPIO(pin, "out")
    gpio_out.write(False)
@app.route("/")
def main():
   # For each pin, read the pin state and store it in the pins dictionary:
    for pin in pins:
      pins['state']=gpio_out.read()
   # Put the pin dictionary into the template data dictionary:
    templateData = {
      'pins' : pins
      }
   # Pass the template data into the template main.html and return it to the user
    return render_template('main.html', **templateData)
# The function below is executed when someone requests a URL with the pin number and action in it:
@app.route("/<changePin>/<action>")
def action(changePin, action):
   # Convert the pin from the URL into an integer:
    changePin = int(changePin)
   # Get the device name for the pin being changed:
    deviceName = pins['name']
   # If the action part of the URL is "on," execute the code indented below:
    if action == "on":
      # Set the pin high:
      gpio_out.write(True)
      # Save the status message to be passed into the template:
      message = "Turned " + deviceName + " on."
    if action == "off":
      gpio_out.write(False)
      message = "Turned " + deviceName + " off."
   # For each pin, read the pin state and store it in the pins dictionary:
    for pin in pins:
      pins['state']=gpio_out.read()
   # Along with the pin dictionary, put the message into the template data dictionary:
    templateData = {
      'pins' : pins
    }
    return render_template('main.html', **templateData)
if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)
保存退出。
建立templates目錄,在其下創建main.html
<!DOCTYPE html>
<head>
   <title>Loongson Web Server</title>
</head>

<body>
   <h1>Loongson Web Server</h1>
   {% for pin in pins %}
   <h2>{{ pins.name }}
   {% if pins.state == true %}
      is currently <strong>on</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/off" class="btn btn-block btn-lg btn-default" role="button">Turn off</a></div></div>
   {% else %}
      is currently <strong>off</strong></h2><div class="row"><div class="col-md-2">
      <a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
   {% endif %}
   {% endfor %}
</body>
</html>
保存退出。運行
chmod 755 app.py
python app.py
會看到類似
# python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 135-748-612
192.168.1.126 - - "GET /55/off HTTP/1.1" 200 -
從局域網其它機器訪問龍芯派IP即可控制gpio的led

sn2015ol 发表于 2018-10-1 21:41:56

这个好

tsuibin 发表于 2018-10-10 10:18:35

羡慕有网卡的板子

cyberhero 发表于 2018-10-10 19:30:26

有不帶網卡的板子嗎
页: [1]
查看完整版本: 龍芯派web控制gpio