Skip to content
Snippets Groups Projects
Commit ff42c792 authored by 도혁 함's avatar 도혁 함
Browse files

feat: sysfs방식으로 부저 제어

parent 171e9229
No related branches found
No related tags found
No related merge requests found
...@@ -6,8 +6,6 @@ ...@@ -6,8 +6,6 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#include <wiringPi.h>
#include <softTone.h>
#include <signal.h> #include <signal.h>
#define BUFFER_MAX 3 #define BUFFER_MAX 3
...@@ -31,52 +29,64 @@ int fireAlarmNotes[] = { 880, 880, 0, 880, 880, 0, 988, 988, 0, 880, 880, 0 }; ...@@ -31,52 +29,64 @@ int fireAlarmNotes[] = { 880, 880, 0, 880, 880, 0, 988, 988, 0, 880, 880, 0 };
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
int park = 0; // 주차 상태: 0 = 주차되지 않음, 1 = 주차됨 int park = 0; // 주차 상태: 0 = 주차되지 않음, 1 = 주차됨
int setFire = 1; // 화재 알림 상태: 1 = 화재 가능, 0 = 이미 화재 알림 보냄 int setFire = 1;
int sock; // 서버 소켓 int sock;
int redLed = 2; // 적색 LED의 초기 설정값 int redLed = 2;
int CLEAR = 1; int CLEAR = 1;
pthread_t ultra_thread, flame_thread, receive_thread; pthread_t ultra_thread, flame_thread, receive_thread;
// GPIO 제어 함수 선언
int GPIOExport(int pin); int GPIOExport(int pin);
int GPIOUnexport(int pin); int GPIOUnexport(int pin);
int GPIODirection(int pin, int dir); int GPIODirection(int pin, int dir);
int GPIORead(int pin); int GPIORead(int pin);
int GPIOWrite(int pin, int value); int GPIOWrite(int pin, int value);
// 인터럽트
void signal_handler(int no); void signal_handler(int no);
// 초음파 센서 측정 스레드 // PWM 기반 부저 소리 발생 함수
void buzzerTone(int pin, int frequency, int duration) {
int period = 1000000 / frequency; // 주기 (마이크로초)
int halfPeriod = period / 2;
for (int i = 0; i < (duration * 1000 / period); i++) {
GPIOWrite(pin, HIGH);
usleep(halfPeriod);
GPIOWrite(pin, LOW);
usleep(halfPeriod);
}
}
void *ultrasonic_sensor(void *arg) { void *ultrasonic_sensor(void *arg) {
clock_t start_t, end_t; clock_t start_t, end_t;
double time; double time;
while (1) { while (1) {
GPIOWrite(POUT, HIGH); // 초음파 트리거 핀 HIGH GPIOWrite(POUT, HIGH);
usleep(10); // 10 마이크로초 신호 유지 usleep(10);
GPIOWrite(POUT, LOW); // 초음파 트리거 핀 LOW GPIOWrite(POUT, LOW);
// 에코 핀 신호 대기 while (GPIORead(PIN) == LOW)
while (GPIORead(PIN) == LOW) start_t = clock(); start_t = clock();
while (GPIORead(PIN) == HIGH) end_t = clock(); while (GPIORead(PIN) == HIGH)
end_t = clock();
time = (double)(end_t - start_t) / CLOCKS_PER_SEC; // 시간 계산 time = (double)(end_t - start_t) / CLOCKS_PER_SEC;
double distance = time / 2 * 34000; // cm 단위 거리 계산 double distance = time / 2 * 34000;
if (redLed == 2) { if (redLed == 2) {
GPIOWrite(RED, HIGH); GPIOWrite(RED, HIGH);
redLed = 1; redLed = 1;
} }
if (park == 0 && distance <= 10 && redLed == 1) { // 10cm 이하 -> 주차 if (park == 0 && distance <= 10 && redLed == 1) {
GPIOWrite(GREEN, HIGH); GPIOWrite(GREEN, HIGH);
GPIOWrite(RED, LOW); GPIOWrite(RED, LOW);
park = 1; park = 1;
redLed = 0; redLed = 0;
printf("0-PARK\n"); printf("0-PARK\n");
send(sock, "0-PARK", strlen("0-PARK"), 0); send(sock, "0-PARK", strlen("0-PARK"), 0);
} else if (park == 1 && distance > 10 && redLed == 0) { // 10cm 초과 -> 주차 해제 } else if (park == 1 && distance > 10 && redLed == 0) {
GPIOWrite(GREEN, LOW); GPIOWrite(GREEN, LOW);
GPIOWrite(RED, HIGH); GPIOWrite(RED, HIGH);
park = 0; park = 0;
...@@ -85,47 +95,47 @@ void *ultrasonic_sensor(void *arg) { ...@@ -85,47 +95,47 @@ void *ultrasonic_sensor(void *arg) {
send(sock, "0-EXIT", strlen("0-EXIT"), 0); send(sock, "0-EXIT", strlen("0-EXIT"), 0);
} }
usleep(500000); // 0.5초 주기 usleep(500000);
} }
return NULL; return NULL;
} }
// 불꽃 센서 감지 스레드
void *flame_sensor(void *arg) { void *flame_sensor(void *arg) {
printf("초기 FLAME 값은 %d\n", GPIORead(FLAME));
printf("초기 FLAME 값: %d\n", GPIORead(FLAME));
wiringPiSetupGpio();
softToneCreate(BUZZER);
while (1) { while (1) {
if (GPIORead(FLAME) == 0 && setFire == 1) { // 불꽃 감지 if (GPIORead(FLAME) == 0 && setFire == 1) {
printf("0-FIRE\n"); printf("0-FIRE\n");
send(sock, "0-FIRE", strlen("0-FIRE"), 0); send(sock, "0-FIRE", strlen("0-FIRE"), 0);
setFire = 0; // 더 이상 화재 신호 전송하지 않음 setFire = 0;
CLEAR = 0; CLEAR = 0;
printf("FLAME 값: %d\n", GPIORead(FLAME));
} }
while(1){
if (CLEAR == 0){ while (CLEAR == 0) {
for (int i = 0; i < sizeof(fireAlarmNotes) / sizeof(int); i++) { for (int i = 0; i < sizeof(fireAlarmNotes) / sizeof(int); i++) {
softToneWrite(BUZZER, fireAlarmNotes[i]); if (CLEAR != 0) {
delay(200); break;
} }
if (fireAlarmNotes[i] > 0) {
buzzerTone(BUZZER, fireAlarmNotes[i], 200);
} else {
usleep(200000);
} }
}
else{ if (CLEAR != 0) {
softToneWrite(BUZZER,0);
break; break;
} }
} }
usleep(200000); // 0.2초 주기
GPIOWrite(BUZZER, LOW);
usleep(200000);
} }
return NULL; return NULL;
} }
// 서버 메시지 수신 스레드
void *receive_message(void *arg) { void *receive_message(void *arg) {
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
int valread; int valread;
...@@ -140,29 +150,28 @@ void *receive_message(void *arg) { ...@@ -140,29 +150,28 @@ void *receive_message(void *arg) {
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { if (sock < 0) {
perror("Socket creation error"); perror("Socket creation error");
sleep(5); // 5초 대기 후 재시도 sleep(5);
continue; continue;
} }
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed. 재연결 시도 중..."); perror("Connection failed. 재연결 시도 중...");
close(sock); close(sock);
sleep(5); // 5초 대기 후 재시도 sleep(5);
continue; continue;
} }
printf("서버에 재연결되었습니다.\n"); printf("서버에 재연결되었습니다.\n");
break; // 재연결 성공 시 루프 탈출 break;
} }
} else { } else {
buffer[valread] = '\0'; // 수신된 메시지 null-terminate buffer[valread] = '\0';
printf("서버로부터 받은 메시지: %s\n", buffer); printf("서버로부터 받은 메시지: %s\n", buffer);
buffer[strcspn(buffer, "\r\n")] = '\0'; buffer[strcspn(buffer, "\r\n")] = '\0';
if (strcmp(buffer, "0-CLEAR") == 0) { // 화재 해제 명령 if (strcmp(buffer, "CLEAR") == 0) {
setFire = 1; // 화재 감지 준비 상태로 전환 setFire = 1;
CLEAR = 1; // 0-CLEAR을 받았음 CLEAR = 1;
printf("setFire가 1로 설정되었습니다.\n"); printf("setFire 및 CLEAR가 %d, %d로 설정되었습니다.\n", setFire, CLEAR);
} }
} }
} }
...@@ -229,16 +238,18 @@ int main() { ...@@ -229,16 +238,18 @@ int main() {
GPIOUnexport(BUZZER); GPIOUnexport(BUZZER);
return 0; return 0;
} }
// ctrl+c로 인해 프로세스 종료시 잔류 신호 차단
void signal_handler(int no) { void signal_handler(int no) {
printf("System Interrupt.\n"); printf("System Interrupt.\n");
GPIOWrite(GREEN, LOW); GPIOWrite(GREEN, LOW);
GPIOWrite(RED, LOW); GPIOWrite(RED, LOW);
softToneWrite(BUZZER,0); GPIOWrite(BUZZER, LOW);
pinMode(BUZZER, INPUT); // 부저 입력모드 전환으로 인한 잔류 신호 차단 close(sock);
exit(0); exit(0);
} }
int GPIOExport(int pin) { int GPIOExport(int pin) {
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
ssize_t bytes_written; ssize_t bytes_written;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment