-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_side.py
119 lines (97 loc) · 3.58 KB
/
rpi_side.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import ftplib #Library for FTP
import os #Library for Operating System
import socket #Library for Network Communication
import time #Library for Time
import cv2 #Library for Computer Vision
import RPi.GPIO as GPIO #Library for Raspberry Pi GPIO
GPIO.setmode(GPIO.BCM) #setting GPIO Mode
GPIO.setwarnings(False) # Suppress GPIO warnings
#Define pin numbers for the motor
PIN1 = 24
PIN2 = 23
#Set up the GPIO pins for output
GPIO.setup(PIN1, GPIO.OUT)
GPIO.setup(PIN2, GPIO.OUT)
def send_frames():
# FTP connection setup
ftp = ftplib.FTP('')
ftp.login('username', 'password')
# FTP settings for better performance and reliability
ftp.set_pasv(True)
ftp.set_debuglevel(2)
ftp.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
ftp.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 300)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 60)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5)
# FTP commands to send frames
for file in os.listdir('frames'):
with open(os.path.join('frames', file), 'rb') as f:
ftp.storbinary('STOR {}'.format(file), f, blocksize=65536)
os.remove(os.path.join('frames', file)) # Delete the file after sending
ftp.quit()
def capture_new_frames():
cap = cv2.VideoCapture(0) # Use the first available camera (index 0)
# Calculate the time interval between capturing each frame
num_frames = 6
time_interval = 3.0 / num_frames
# Capture frames and save them to 'frames' directory
for i in range(num_frames):
ret, frame = cap.read()
if ret:
frame_file = os.path.join('frames', f'frame{i:03d}.jpg')
cv2.imwrite(frame_file, frame)
time.sleep(time_interval) #Wait for the specified time interval
else:
print("Error capturing frame")
cap.release()
def check_file():
# FTP connection setup
ftp = ftplib.FTP('')
ftp.login('username', 'password')
# FTP settings for better performance and reliability
ftp.set_pasv(True)
ftp.set_debuglevel(2)
ftp.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
ftp.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 300)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 60)
ftp.sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5)
# Check for specific files on the FTP server
files = ftp.nlst()
if 'on.txt' in files:
open_door()
print("Door is opening")
time.sleep(1)
ftp.delete("on.txt") # Delete the file after processing
elif 'off.txt' in files:
close_door()
print("Door is closing")
time.sleep(1)
ftp.delete("off.txt") # Delete the file after processing
ftp.quit()
def open_door():
# Control the motor to open the door
GPIO.output(PIN1, GPIO.HIGH)
GPIO.output(PIN2, GPIO.LOW)
time.sleep(1.6)
stop_door()
time.sleep(10)
close_door() # Close the door after 10 seconds
def close_door():
# Control the motor to close the door
GPIO.output(PIN1, GPIO.LOW)
GPIO.output(PIN2, GPIO.HIGH)
time.sleep(1.6)
stop_door()
def stop_door():
# Stop the motor
GPIO.output(PIN1, GPIO.LOW)
GPIO.output(PIN2, GPIO.LOW)
while True:
check_file()
capture_new_frames()
send_frames()
time.sleep(5) # Wait for 5 seconds before repeat
# Clean up GPIO channels
GPIO.cleanup()