-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotion_Detection.c
More file actions
248 lines (203 loc) · 6.58 KB
/
Motion_Detection.c
File metadata and controls
248 lines (203 loc) · 6.58 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <pthread.h>
#include <unistd.h> // usleep() 마이크로 단위 sleep
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
// 임시 파일
#define STATUS_FILE "/home/pi/control.txt"
#define STREAM_FILE "/home/pi/streaming.txt"
// 핀
#define PIR_PIN 24 // GPIO 24 모션감지 센서
#define BUTTON_PIN 23 // GPIO 23 초인종
#define BUZZER_PIN 17 // GPIO 17 부저
pid_t python_pid = -1; //python 프로세스 PID 저장
// 공유 변수
volatile int doorbellState = 0; // 초인종 버튼이 눌러지면
volatile int personRecognition = 0; // 사람이 감지되면
// 초인종 firebase 알람 트리거
void uploadToFirebase_Bell() {
int result = system("python3 /home/pi/SDB/firebaseUpload_Bell.py");
if (result == 0) {
printf("Bell Firebase 업로드 성공\n");
} else {
printf("Bell Firebase 실패");
}
}
// 센서 firebase 알람 트리거
void uploadToFirebase_Sensor() {
int result = system("python3 /home/pi/SDB/firebaseUpload_Sensor.py");
if (result == 0) {
printf("Sensor Firebase 업로드 성공\n");
} else {
printf("Sensor Firebase 실패");
}
}
///////////////////////////////////////////////////////////// C to python
// 상태 파일에 신호 쓰기
void writeSignalToFile(const char *signal) {
FILE *file = fopen(STATUS_FILE, "w");
if (file == NULL) {
perror("파일 열기 실패");
return;
}
fprintf(file, "%s", signal);
fclose(file);
printf("신호 파일에 기록: %s\n", signal);
}
///////////////////////////////////////////////////////////// 여기까지 C to python
// 모션 감지 스레드
void *motionDetectionThread(void *arg) {
int personDetected = 0; // 사람 감지 여부
int motionDetected = 0; // 센서 출력 상태
unsigned long lastMotionTime = 0; // 마지막으로 움직임을 감지한 시간
while (1) {
motionDetected = digitalRead(PIR_PIN);
//printf("타이머 : %lu\n", millis() - lastMotionTime);
if (motionDetected) {
lastMotionTime = millis(); // 움직임 감지 시간 업데이트
if (!personDetected) {
personRecognition = 1;
personDetected = 1;
printf("사람 감지, 녹화 프로그램 시작\n");
writeSignalToFile("1");
uploadToFirebase_Sensor();
}
}
// 8초 동안 움직임이 없으면 상태를 초기화
else if (!motionDetected && (millis() - lastMotionTime > 8000) && personRecognition) {
personRecognition = 0;
personDetected = 0;
printf("사람 없음, 녹화 프로그램 종료\n");
writeSignalToFile("0");
}
delay(200); // 짧은 대기
}
return NULL;
}
// 초인종 스레드
void *buttonPressThread(void *arg) {
// 버튼이 꾹 눌러질 때 처리
int buttonState = HIGH; // 버튼 초기 상태
int lastButtonState = HIGH; // 버튼 나중 상태
while (1) {
buttonState = digitalRead(BUTTON_PIN);
// 버튼 감지
if (buttonState == LOW && lastButtonState == HIGH) {
doorbellState = 1; // Doorbell was pressed
printf("초인종이 눌러짐, 알람 전송\n");
uploadToFirebase_Bell();
printf("화상통화 프로세스 시작\n");
}
// 버튼 상태 업데이트
lastButtonState = buttonState;
delay(50); // 디바운싱 처리 (짧게..?)
}
return NULL;
}
void playTone(int frequency, int duration) {
int period = 1000000 / frequency;
int halfPeriod = period / 2;
for (int i = 0; i < (duration * 1000) / period; i++) {
digitalWrite(BUZZER_PIN, HIGH);
usleep(halfPeriod);
digitalWrite(BUZZER_PIN, LOW);
usleep(halfPeriod);
}
}
void *buzzerThread(void *arg) {
while (1) {
if (doorbellState) {
printf("Buzzer: Ding-dong\n");
playTone(523, 400);
usleep(100000);
playTone(392, 400);
digitalWrite(BUZZER_PIN, LOW);
doorbellState = 0;
}
usleep(100000);
}
return NULL;
}
int main() {
FILE *file = fopen(STATUS_FILE, "w");
if (file == NULL) {
perror("파일 열기 실패");
return 1;
}
fprintf(file, "%s", 0);
fclose(file);
file = fopen(STREAM_FILE, "w");
if (file == NULL) {
perror("파일 열기 실패");
return 1;
}
fprintf(file, "%s", 0);
fclose(file);
if (wiringPiSetupGpio() == -1) {
printf("Failed to initialize WiringPi with GPIO mode!\n");
return 1;
}
pinMode(PIR_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
pullUpDnControl(BUTTON_PIN, PUD_UP);
// 서보 모터 프로세스 코드 시작
pid_t motor_pid, python_pid;
python_pid = fork();
if(python_pid < 0) {
perror("fork 실패");
exit(EXIT_FAILURE);
} else if (python_pid == 0){
printf("python 프로세스 실행");
fflush(stdout);
execlp("python3", "python3", "/home/pi/SDB/flask_app.py", NULL);
perror("execlp 실패");
exit(EXIT_FAILURE);
}
motor_pid = fork();
if(motor_pid < 0) {
perror("fork 실패");
exit(EXIT_FAILURE);
} else if (motor_pid == 0){
printf("서보모터 프로세스 실행");
fflush(stdout);
execlp("./motor", "./motor", NULL);
perror("execlp 실패");
exit(EXIT_FAILURE);
}
pthread_t motionThread, buttonThread, buzzerThreadId;
if (pthread_create(&motionThread, NULL, motionDetectionThread, NULL) != 0) {
printf("모션 스레드 생성 실패\n");
return 1;
}
if (pthread_create(&buttonThread, NULL, buttonPressThread, NULL) != 0) {
printf("버튼 스레드 생성 실패\n");
return 1;
}
if (pthread_create(&buzzerThreadId, NULL, buzzerThread, NULL) != 0) {
printf("버저 스레드 생성 실패\n");
return 1;
}
// 메인 스레드
while (1) {
if (personRecognition) {
printf("사람 감지 O\n");
} else {
printf("사람 감지 X\n");
}
delay(1000);
}
// 스레드
pthread_join(motionThread, NULL);
pthread_join(buttonThread, NULL);
pthread_join(buzzerThreadId, NULL);
wait(NULL);
printf("자식 프로세스 종료됨");
return 0;
}