从网页控制Raspberry pi的GPIO引脚

问题描述

我正在使用 Python Web 应用程序控制 RaspBerry Pi GPIO 引脚。它在我使用flask-test-server的测试中工作正常,但它不能在另一个网络或互联网上工作。树莓派在局域网中,通过路由器的 DMZ 和 80 端口和互联网中的 Apache2 可见。引脚状态在我的测试中工作正常,但它在另一个网络中不起作用,尽管它从另一个网络访问我的服务器但没有改变 GPIO 引脚的状态。所以我知道“如果 request.method == 'POST'” 在“ @app.route('/profile',methods=['GET','POST']) "在我的代码中无法正常工作,但我不知道为什么 ???!!!!

html 文件

<!doctype html>
<html lang="en">
    <head>
        <title>controller</title>
        <Meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/controller.css')}}">
    <!--===============================================================================================-->
    </head>
    <body>
        <form method="post">
            <span class="switch">
                <span class="switch-border1">
                    <span class="switch-border2">
                        <input id="switch1" name="checkBox"  value="1" type="checkBox"/>
                        <label for="switch1"></label>
                        <span class="switch-top"></span>
                        <span class="switch-shadow"></span>
                        <span class="switch-handle"></span>
                        <span class="switch-handle-left"></span>
                        <span class="switch-handle-right"></span>
                        <span class="switch-handle-top"></span>
                        <span class="switch-handle-bottom"></span>
                        <span class="switch-handle-base"></span>
                        <span class="switch-led switch-led-green">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                        <span class="switch-led switch-led-red">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                    </span>
                </span>
            </span>
            <span class="switch">
                <span class="switch-border1">
                    <span class="switch-border2">
                        <input id="switch2" name="checkBox" value="2" type="checkBox"/>
                        <label for="switch2"></label>
                        <span class="switch-top"></span>
                        <span class="switch-shadow"></span>
                        <span class="switch-handle"></span>
                        <span class="switch-handle-left"></span>
                        <span class="switch-handle-right"></span>
                        <span class="switch-handle-top"></span>
                        <span class="switch-handle-bottom"></span>
                        <span class="switch-handle-base"></span>
                        <span class="switch-led switch-led-green">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                        <span class="switch-led switch-led-red">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                    </span>
                </span>
            </span>
            <span class="switch">
                <span class="switch-border1">
                    <span class="switch-border2">
                        <input id="switch3" name="checkBox" value="3" type="checkBox"/>
                        <label for="switch3"></label>
                        <span class="switch-top"></span>
                        <span class="switch-shadow"></span>
                        <span class="switch-handle"></span>
                        <span class="switch-handle-left"></span>
                        <span class="switch-handle-right"></span>
                        <span class="switch-handle-top"></span>
                        <span class="switch-handle-bottom"></span>
                        <span class="switch-handle-base"></span>
                        <span class="switch-led switch-led-green">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                        <span class="switch-led switch-led-red">
                            <span class="switch-led-border">
                                <span class="switch-led-light">
                                    <span class="switch-led-glow"></span>
                                </span>
                            </span>
                        </span>
                    </span>
                </span>
            </span>
            <input type="submit" value="Apply" style="
                background-color: #4CAF50; /* Green */
                border: none;
                color: white;
                padding: 15px 32px;
                text-align: center;
                text-decoration: none;
                display: inline-block;
                font-size: 16px;
                margin: 4px 2px;
                cursor: pointer;
                background-color: white; 
                color: black; 
                border: 2px solid #f44336;" />
        </form>
    </body>
</html>

和带有烧瓶的 py 文件

from flask import (
    Flask,g,redirect,render_template,request,session,url_for
)

import RPi.GPIO as GPIO

class User:
    def __init__(self,identi,username,password):
        self.id = identi
        self.username = username
        self.password = password

    def __repr__(self):
        return f'<User: {self.username}>'


users = []
mahbod = User(1024,username='xxxxx',password='xxxxx')
mahta = User(1025,username='',password='xxxxxx')
users.append(mahta)
users.append(mahbod)

app = Flask(__name__)
app.secret_key = 'somesecretkeythatonlyishouldkNow'


@app.before_request
def before_request():
    g.user = None

    if 'user_id' in session:
        user = [x for x in users if x.id == session['user_id']][0]
        g.user = user


@app.route('/login','POST'])
@app.route('/','POST'])
def login():
    if request.method == 'POST':
        session.pop('user_id',None)
        username = request.form['username']
        pas = request.form['pass']
        for x in users:
            if x.username == username and x.password == pas:
                session['user_id'] = x.id
                return redirect(url_for('profile'))
    return render_template('login.html')


@app.route('/profile','POST'])
def profile():
    if not g.user:
        return redirect(url_for('login'))

    if request.method == 'POST':
        checkBox = request.form.getlist('checkBox')
        try:
            # set GPIO numbering mode and define output pins
            GPIO.setmode(GPIO.BCM)
            GPIO.setwarnings(False)
            GPIO.setup(20,GPIO.OUT)
            GPIO.setup(26,GPIO.OUT)
            GPIO.setup(21,GPIO.OUT)

            if len(checkBox) == 0:
                GPIO.output(21,False)
                GPIO.output(20,False)
                GPIO.output(26,False)
            else:
                #for x in checkBox:
                if "1" in checkBox:
                    try:
                        GPIO.output(21,True)
                        print("switch_1 on")
                    except:
                        pass
                else: 
                    try:
                        GPIO.output(21,False)
                        print("switch_1 off")
                    except:
                        pass
                    
                if "2" in checkBox:
                    try:
                        GPIO.output(20,True)
                        print("switch_2 on")
                    except:
                        pass
                    
                else:
                    try:
                        GPIO.output(20,False)
                        print("switch_2 off")
                    except:
                        pass
                    
                if "3" in checkBox:
                    try:
                        GPIO.output(27,True)
                        print("switch_4 on")
                    except:
                        pass
                    
                else:
                    try:
                        GPIO.output(27,False)
                        print("switch_4 off")
                    except:
                        pass
                    
        except:
            return redirect(url_for('not_work'))

    return render_template('profile.html')
 

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

if __name__ == "__main__":
    app.run(debug=True)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)