龙芯俱乐部开源技术社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3039|回复: 3

龍芯派web控制gpio

[复制链接]

46

主题

115

帖子

4146

积分

论坛元老

Rank: 8Rank: 8

积分
4146
发表于 2018-10-1 04:33:18 | 显示全部楼层 |阅读模式

龍芯派運行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[pin]['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[changePin]['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[pin]['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[pin].name }}
   {% if pins[pin].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
會看到類似
[root@LoongsonPi pythoncode]# 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 - - [01/Oct/2018 06:10:56] "GET /55/off HTTP/1.1" 200 -
從局域網其它機器訪問龍芯派IP即可控制gpio的led

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

84

主题

465

帖子

44万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
440445
发表于 2018-10-1 21:41:56 | 显示全部楼层
这个好

0

主题

5

帖子

223

积分

中级会员

Rank: 3Rank: 3

积分
223
发表于 2018-10-10 10:18:35 | 显示全部楼层
羡慕有网卡的板子
武汉龙芯俱乐部(首义学院Linux研究组)

46

主题

115

帖子

4146

积分

论坛元老

Rank: 8Rank: 8

积分
4146
 楼主| 发表于 2018-10-10 19:30:26 | 显示全部楼层
有不帶網卡的板子嗎
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|龙芯俱乐部开源技术社区

GMT+8, 2024-11-28 09:40 , Processed in 0.140745 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表