I am controlling Raspberry Pi GPIO pins with a python web app. Its work fine in my tests with flask-test-server but its not working from another network or from internet. Raspberry Pi is in a local area network and is visible through the DMZ of the router with port 80 and Apache2 in the internet.I have created with an Apache2 server in raspberry pi and accessing a .py file to change the status of pins. Status of pins are work fine in my tests but its is not working in another network, although it accesses my server from the other network but is not changing the status of GPIO pin. so I know that " if request.method == 'POST'" in " @app.route('/profile', methods=['GET', 'POST']) " is not working properly in my code but I don't know why ???!!!!
html file:
<!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>
and py file with flask:
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', methods=['GET', 'POST'])
@app.route('/', methods=['GET', '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', methods=['GET', '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)