Skip to content

Commit 4dfb7c8

Browse files
Add files via upload
1 parent f8337c9 commit 4dfb7c8

File tree

4 files changed

+239
-85
lines changed

4 files changed

+239
-85
lines changed

app.py

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
from flask import Flask, render_template, Response, request
1+
from flask import Flask, render_template, Response, request, jsonify
22
import cv2
3-
import easyocr
43
import time
54
import threading
6-
from your_motor_library import MotorController
7-
from your_audio_detection_library import AudioDetector
5+
import serial
6+
87

98
app = Flask(__name__)
109

1110
# Kameralar
12-
outside_garage_cam = cv2.VideoCapture(0) # 10 metre uzakta olan kamera
13-
inside_garage_cam = cv2.VideoCapture(1) # Garajın içindeki kamera
14-
entrance_cam = cv2.VideoCapture(2) # Ev girişindeki kamera
15-
16-
# Plaka tanıma ve motor kontrolü
17-
target_plate = "34AEA154"
18-
motor = MotorController()
19-
audio_detector = AudioDetector() # Kapı zil sesi algılama
20-
21-
def check_license_plate(frame, target_plate):
22-
reader = easyocr.Reader(['en'])
23-
results = reader.readtext(frame)
24-
for (bbox, text, prob) in results:
25-
if text == target_plate:
26-
return True
27-
return False
11+
outside_garage_cam = cv2.VideoCapture(0)
12+
inside_garage_cam = cv2.VideoCapture(1)
13+
entrance_cam = cv2.VideoCapture(2)
14+
15+
# Seri port bağlantısı (Arduino)
16+
ser = serial.Serial('/dev/ttyUSB0', 115200) # Arduino's serial port
17+
18+
result_message = ""
2819

29-
def gen_frames(cam): # video akışı için bir jeneratör fonksiyon
20+
# Kullanıcı şifreleri
21+
user_passwords = {
22+
"user1": "1234",
23+
"user2": "5678"
24+
# Add more users
25+
}
26+
27+
def gen_frames(cam): # A generator function for streaming video
3028
while True:
3129
success, frame = cam.read()
3230
if not success:
@@ -40,35 +38,33 @@ def gen_frames(cam): # video akışı için bir jeneratör fonksiyon
4038
def garage_control():
4139
while True:
4240
ret, frame = outside_garage_cam.read()
43-
if ret and check_license_plate(frame, target_plate):
44-
motor.open_garage()
45-
time.sleep(2) # Garajın açılma süresi
46-
inside_detection()
47-
time.sleep(1)
48-
49-
def inside_detection():
50-
while True:
51-
ret, frame = inside_garage_cam.read()
5241
if ret:
53-
# Aracın içeri girip girmediğini kontrol et
54-
# Burada basit bir hareket algılama yapılabilir
55-
# Daha gelişmiş algoritmalar kullanabilirsiniz
56-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
57-
motion_detected = cv2.absdiff(gray, cv2.GaussianBlur(gray, (21, 21), 0)).sum() > 100000 # Basit bir hareket algılama
58-
if motion_detected:
59-
time.sleep(10) # Aracın içeri girmesi için bekleme süresi
60-
motor.close_garage()
61-
break
42+
# Motion detection and target plate recognition code will be here
43+
# Send RF signal if target plate detected
44+
ser.write(b'OPEN_GARAGE\n')
45+
time.sleep(1)
6246

6347
def audio_detection():
6448
while True:
65-
if audio_detector.detect_doorbell():
66-
print("Zil Sesi Algılandı!")
67-
# Ev giriş kamera akışını başlat
68-
# Bu durumda kullanıcının bildirim alıp kamerayı görüntülemesi sağlanabilir
69-
# Bu kısımda Flask route'ları kullanarak istemci tarafına bilgi gönderebilirsiniz.
49+
if detect_doorbell():
50+
print("ringtone detected!")
51+
# Start camera streaming at home entrance
52+
# In this case, the user can be notified and view the camera
53+
# In this section, you can send information to the client side using Flask routes.
7054
time.sleep(1)
7155

56+
def detect_doorbell():
57+
# Here will be the ringtone detection algorithm
58+
return False
59+
60+
def read_from_arduino():
61+
global result_message
62+
while True:
63+
if ser.in_waiting > 0:
64+
line = ser.readline().decode('utf-8').strip()
65+
if line.startswith("RESULT:"):
66+
result_message = line.split("RESULT:")[1]
67+
7268
@app.route('/video_feed/<int:cam_id>')
7369
def video_feed(cam_id):
7470
if cam_id == 0:
@@ -82,10 +78,26 @@ def video_feed(cam_id):
8278

8379
@app.route('/open_door', methods=['POST'])
8480
def open_door():
85-
# Solenoid kontrol kodu buraya gelecek
86-
motor.open_door() # Örneğin: motor.open_door()
81+
ser.write(b'OPEN_DOOR\n') # Command to send signal to Arduino
8782
return "Door Opened", 200
8883

84+
@app.route('/validate_password', methods=['POST'])
85+
def validate_password():
86+
data = request.json
87+
user = data.get('user')
88+
password = data.get('password')
89+
90+
if user in user_passwords and user_passwords[user] == password:
91+
ser.write(b'VALID_PASSWORD\n')
92+
return "Password Validated", 200
93+
else:
94+
return "Invalid Password", 401
95+
96+
@app.route('/result')
97+
def result():
98+
global result_message
99+
return jsonify(result=result_message)
100+
89101
@app.route('/')
90102
def index():
91103
return render_template('index.html')
@@ -94,4 +106,5 @@ def index():
94106
# Arka planda çalışan görevler
95107
threading.Thread(target=garage_control).start()
96108
threading.Thread(target=audio_detection).start()
109+
threading.Thread(target=read_from_arduino).start()
97110
app.run(host='0.0.0.0', port=5000, debug=True)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#include <RCSwitch.h>
2+
#include <MFRC522.h>
3+
#include <Keypad.h>
4+
#include <SPI.h>
5+
6+
// RFID
7+
#define SS_PIN 10
8+
#define RST_PIN 9
9+
MFRC522 rfid(SS_PIN, RST_PIN);
10+
11+
// Keypad
12+
const byte ROWS = 4; // Four rows
13+
const byte COLS = 3; // Three columns
14+
char keys[ROWS][COLS] = {
15+
{'1', '2', '3'},
16+
{'4', '5', '6'},
17+
{'7', '8', '9'},
18+
{'*', '0', '#'}
19+
};
20+
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
21+
byte colPins[COLS] = {5, 4, 3}; // Connect to the column pinouts of the keypad
22+
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
23+
24+
// Kullanıcı RFID kartları ve şifreler
25+
String validCards[] = {"CARD1_UID", "CARD2_UID"};
26+
String userPasswords[] = {"1234", "5678"};
27+
String userNames[] = {"X", "Y"};
28+
29+
String enteredPassword = "";
30+
31+
void setup() {
32+
Serial.begin(115200);
33+
SPI.begin();
34+
rfid.PCD_Init();
35+
}
36+
37+
void loop() {
38+
// process incoming commands from serial port
39+
if (Serial.available() > 0) {
40+
String command = Serial.readStringUntil('\n');
41+
if (command == "OPEN_DOOR") {
42+
openDoor();
43+
}
44+
}
45+
46+
// RFID Read
47+
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
48+
String cardUID = "";
49+
for (byte i = 0; i < rfid.uid.size; i++) {
50+
cardUID += String(rfid.uid.uidByte[i], HEX);
51+
}
52+
if (isValidCard(cardUID)) {
53+
String user = getUserByCard(cardUID);
54+
openDoor();
55+
Serial.print("Hello ");
56+
Serial.println(user);
57+
sendResultToJetson("Hello " + user);
58+
}
59+
rfid.PICC_HaltA();
60+
}
61+
62+
// Keypad Read
63+
char key = keypad.getKey();
64+
if (key) {
65+
if (key == '#') {
66+
if (isValidPassword(enteredPassword)) {
67+
String user = getUserByPassword(enteredPassword);
68+
openDoor();
69+
Serial.print("Hello ");
70+
Serial.println(user);
71+
sendResultToJetson("Hello " + user);
72+
}
73+
enteredPassword = "";
74+
} else {
75+
enteredPassword += key;
76+
}
77+
}
78+
}
79+
80+
bool isValidCard(String uid) {
81+
for (String validCard : validCards) {
82+
if (validCard == uid) {
83+
return true;
84+
}
85+
}
86+
return false;
87+
}
88+
89+
bool isValidPassword(String password) {
90+
for (String userPassword : userPasswords) {
91+
if (userPassword == password) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
98+
String getUserByCard(String uid) {
99+
for (int i = 0; i < sizeof(validCards) / sizeof(validCards[0]); i++) {
100+
if (validCards[i] == uid) {
101+
return userNames[i];
102+
}
103+
}
104+
return "Unknown";
105+
}
106+
107+
String getUserByPassword(String password) {
108+
for (int i = 0; i < sizeof(userPasswords) / sizeof(userPasswords[0]); i++) {
109+
if (userPasswords[i] == password) {
110+
return userNames[i];
111+
}
112+
}
113+
return "Unknown";
114+
}
115+
116+
void openDoor() {
117+
// Solenoidi açma kodu burada olacak
118+
// Örneğin: digitalWrite(solenoidPin, HIGH);
119+
}
120+
121+
void sendResultToJetson(String message) {
122+
Serial.print("RESULT:");
123+
Serial.println(message);
124+
}

static/styles.css

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
body {
2+
text-align: center;
3+
font-family: Arial, sans-serif;
4+
}
5+
6+
h1 {
7+
text-align: center;
8+
}
9+
10+
.container {
11+
display: flex;
12+
justify-content: space-around;
13+
align-items: center;
14+
flex-wrap: wrap;
15+
}
16+
17+
.camera-feed {
18+
flex: 1;
19+
margin: 10px;
20+
}
21+
22+
.centered-button {
23+
text-align: center;
24+
width: 100%;
25+
margin-top: 20px;
26+
}
27+
28+
#open-door {
29+
font-size: 1.5em;
30+
padding: 10px 20px;
31+
}

templates/index.html

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,47 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>H3RU Home Control Automation</title>
7-
<style>
8-
body {
9-
text-align: center;
10-
}
11-
.container {
12-
display: flex;
13-
justify-content: space-around;
14-
align-items: center;
15-
flex-wrap: wrap;
16-
}
17-
.camera-feed {
18-
flex: 1;
19-
margin: 10px;
20-
}
21-
.centered-button {
22-
text-align: center;
23-
width: 100%;
24-
margin-top: 20px;
25-
}
26-
button {
27-
font-size: 20px;
28-
padding: 10px 20px;
29-
}
30-
</style>
6+
<title>H3RU Home Control System</title>
7+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
318
</head>
329
<body>
33-
<h1>Home Control Automation</h1>
10+
<h1>H3RU Home Control System</h1>
3411
<div class="container">
3512
<div class="camera-feed">
36-
<h2>Outside Garage Camera</h2>
37-
<img src="{{ url_for('video_feed', cam_id=0) }}">
13+
<h3>Outside Garage Camera</h3>
14+
<img src="{{ url_for('video_feed', cam_id=0) }}" alt="Outside Garage Camera" width="320" height="240">
3815
</div>
3916
<div class="camera-feed">
40-
<h2>Inside Garage Camera</h2>
41-
<img src="{{ url_for('video_feed', cam_id=1) }}">
17+
<h3>Inside Garage Camera</h3>
18+
<img src="{{ url_for('video_feed', cam_id=1) }}" alt="Inside Garage Camera" width="320" height="240">
4219
</div>
4320
<div class="camera-feed">
44-
<h2>Entrance Camera</h2>
45-
<img src="{{ url_for('video_feed', cam_id=2) }}">
21+
<h3>Entrance Camera</h3>
22+
<img src="{{ url_for('video_feed', cam_id=2) }}" alt="Entrance Camera" width="320" height="240">
4623
</div>
4724
</div>
4825
<div class="centered-button">
49-
<button onclick="openDoor()">Open Door</button>
26+
<button id="open-door">Open Door</button>
5027
</div>
51-
28+
<div id="result-message" style="margin-top: 20px;"></div>
5229
<script>
53-
function openDoor() {
30+
document.getElementById('open-door').addEventListener('click', function() {
5431
fetch('/open_door', { method: 'POST' })
55-
.then(response => {
56-
if(response.ok) {
57-
alert("Door opened");
58-
}
32+
.then(response => response.text())
33+
.then(data => {
34+
document.getElementById('result-message').innerText = data;
35+
});
36+
});
37+
38+
function fetchResult() {
39+
fetch('/result')
40+
.then(response => response.json())
41+
.then(data => {
42+
document.getElementById('result-message').innerText = data.result;
5943
});
6044
}
45+
46+
setInterval(fetchResult, 1000); // Her saniye sonuçları kontrol et
6147
</script>
6248
</body>
6349
</html>

0 commit comments

Comments
 (0)