-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiot_lock.py
More file actions
47 lines (35 loc) Β· 1.26 KB
/
Copy pathiot_lock.py
File metadata and controls
47 lines (35 loc) Β· 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flask import Flask, request, jsonify
app = Flask(__name__)
# Simulated lock state
lock_state = {"status": "LOCKED", "pin": "1234"} # Default PIN is 1234
@app.route("/")
def home():
return f"""
<h1>π IoT Door Lock</h1>
<p>Current State: <b>{lock_state['status']}</b></p>
<form action="/unlock" method="post">
<input type="password" name="pin" placeholder="Enter PIN" required>
<button type="submit">Unlock</button>
</form>
<a href="/lock"><button>Lock</button></a>
"""
@app.route("/unlock", methods=["POST"])
def unlock():
user_pin = request.form.get("pin")
if user_pin == lock_state["pin"]:
lock_state["status"] = "UNLOCKED"
print("π Door UNLOCKED")
return f"<h2>β
Door is now UNLOCKED</h2><a href='/'>Go Back</a>"
else:
return f"<h2>β Wrong PIN</h2><a href='/'>Try Again</a>"
@app.route("/lock", methods=["GET"])
def lock():
lock_state["status"] = "LOCKED"
print("π Door LOCKED")
return f"<h2>π Door is now LOCKED</h2><a href='/'>Go Back</a>"
@app.route("/status", methods=["GET"])
def status():
return jsonify(lock_state)
if __name__ == "__main__":
print("π IoT Door Lock Simulation Running at http://127.0.0.1:5000/")
app.run(debug=True)