개발자/라즈베리파이4

라즈베리파이4 스마트홈 불꽃 센서 코드 2

지구빵집 2023. 5. 9. 15:21
반응형

 

 

이전 포스팅에서 사물인터넷 스마트홈 시스템에 대해 살펴보았다. 이전 장에서는 스마트홈 센서를 검토해 보자. 길어지면 다음 포스팅으로 넘기지만 연결도와 테스트 소스코드(C언어)를 살펴보면서 진행하기로 한다.

 

 

라즈베리파이 스마트 홈 실습 과정 전체 포스팅 리스트 

 

라즈베리파이4 스마트홈 제품 특징 1

라즈베리파이4 스마트홈 불꽃 센서 코드 2

라즈베리파이4 스마트홈 가스센서 코드 3

라즈베리파이4 스마트홈 소리센서 코드 4

라즈베리파이4 스마트홈 조도센서 코드 5

라즈베리파이4 스마트홈 온도 습도센서 코드 6

라즈베리파이4 스마트홈 인체감지 모션센서 코드 7

라즈베리파이4 스마트홈 액츄에이터 디지털 도어록 실습 8

라즈베리파이4 스마트홈 가스타이머 실습 9

라즈베리파이4 스마트홈 전동볼밸브 실습 10

라즈베리파이4 스마트홈 RFID 실습 11

라즈베리파이4 스마트홈 RGB LED 실습 12

라즈베리파이4 스마트홈 카메라 실습 13

라즈베리파이4 스마트홈 조명 LED bar 실습 14

라즈베리파이4 스마트홈 서버 프로그램 실습 15

라즈베리파이4 스마트홈 안드로이드 앱 설명 16

 

 

우선 아래 스마트홈 센서 구성 이미지를 보자. 좌측부터 불꽃 센서, 가스 센서, 소리 센서, 조도 센서, 온도 습도 센서, 인체감지 센서가 보일 것이다. 그럼 순서대로 하나씩 테스트를 해보자.

 

 

스마트 홈 센서

 

모든 센서에 공통인 사항을 불꽃 센서 설명에서 설명한다. SSH 접속을 위해 Putty 프로그램이나 mobaXterm을 설치하여 사용한다. 요즘 mobaXtrem을 많이 사용한다. 

 

불꽃 센서 Flame Sensor

 

불꽃 감지 센서는 적외선 LED를 통해 불꽃에서 감지되는 적외선 파장을 감지하여 이를 아날로그 혹은 디지털 신호로 변환하는 센서다. 불꽃의 파장(주파수) 760nm ~ 1100nm 대역을 감지하는 센서로 소방활동 로봇 제작 및 화재 진압에 중요한 화재경보기 제작에 활용이 가능하며 불꽃센서의 동작 환경은 25~85도(섭씨)에서 작동하며 불꽃을 감지하는 거리에 따라 아날로그 값이나 불꽃의 존재 유무를 알려주는 디지털 값을 출력한다. 

 

 

불꽃 센서 핀번호

 

주의: 라이터, 부탄가스, 가스레인지 등으로 시험 시 화재나 신체상의 위험을 주의해야 한다. 부탁이다.  

 

 

스마트홈 시스템 불꽃 센서

 

 

불꽃 센서 사양

 

동작전압 3.3V to 5V
감지 범위 20cm (4.8V) ~ 100cm (1V)
감지 대역(파장) 760nm ~ 1100nm
응답 시간 : 15usec
인터페이스 : 아날로그/디지탈
크기 : 22x30mm

 

센서 컬리브레이션

 

다른 센서와 마찬가지로 불꽃 센서도 보정을 거친 후 사용한다. 방법은 전원을 연결하면 센서모듈 중앙의 두 개의 LED 중 한 개 전원 LED에 불이 들어온다. 이때 다른 LED 도 켜져 있다면 항상 신호가 들어오는 것이므로 가변저항을 돌려 약간만 시계, 반 시계 방향으로 돌려도 신호 출력 LED 가 꺼지는 상태까지 보정해 주면 된다. 다른 센서도 마찬가지인데 어느 정도에서 컬리브레이션을 마치면 되는가? ON 상태를 알리는 LED가 꺼졌다 들어왔다 하는 지점에서 꺼지는 상태로 두면 된다. 

 

 

 

스마트 홈 폴더로 가기 위해 아래 명령어를 사용한다.

 

pi@raspberrypi:~ $ cd smarthome/
pi@raspberrypi:~/smarthome $

 

불꽃 센서를 테스트 소스 코드 파일 home-flame.c

 

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>

#define HOMEFLAME 23  //gpio13

volatile unsigned char homeflamedetect = 0;

// myInterrupt:  called every time an event occurs
void homeflameInterrupt(void) {    
   homeflamedetect = 0x01;  
}

int main(void) {
  // sets up the wiringPi library
  if (wiringPiSetup () < 0) {
      fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
      return 1;
  }

  // set Pin 17/0 generate an interrupt on high-to-low transitions
  // and attach myInterrupt() to the interrupt
  if ( wiringPiISR (HOMEFLAME, INT_EDGE_FALLING, &homeflameInterrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }

  // display counter value every second.
  while ( 1 ) {
 
    delay( 200 ); // wait 1 second
	if(homeflamedetect == 0x01)
	{
		printf("home flame detected...\n");
		homeflamedetect = 0;
	}
	else
		printf("not flame\n");
  }

  return 0;
}

 

위와 같이 입력하였다면 컴파일 명령은 다음과 같다. 라즈제리파이 GPIO 라이브러리는 wiringPi 라이브러리를 사용하므로 컴파일할 때 인클루드 한다.

 

$gcc -o home-flame home-flame.c -lwiringPi

 

실행은 $./home-flame

 

아래 컴파일과 실행 명령어 실행 코드를 참고한다.

 

pi@raspberrypi:~/smarthome $ gcc -o home-flame home-flame.c -lwiringPi
pi@raspberrypi:~/smarthome $ ./home-flame
not flame
not flame
not flame
home flame detected...
home flame detected... not flame
home flame detected... not flame
not flame
not flame
not flame
not flame
not flame

 

위 코드에도 일부는 들어 있지만 불꽃 센서와 가스 센서를 인터럽트를 사용하는 코드는 아래를 참고한다.

 

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>


// Use GPIO Pin 7, which is Pin 11 for wiringPi library

#define HOMEFLAME 23  //gpio13
#define HOMEGAS 24  //gpio19

// the event counter 
volatile int eventCounter = 0; 

volatile unsigned char homeflamedetect = 0;
volatile unsigned char homegasdetect = 0;

// -------------------------------------------------------------------------
// myInterrupt:  called every time an event occurs
void homeflameInterrupt(void) {
   eventCounter++;
   
   homeflamedetect = 0x01;  
}

void homegasInterrupt(void) {
   eventCounter++;
   homegasdetect = 0x01;
}

// -------------------------------------------------------------------------
// main
int main(void) {
  // sets up the wiringPi library
  if (wiringPiSetup () < 0) {
      fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
      return 1;
  }

  // set Pin 17/0 generate an interrupt on high-to-low transitions
  // and attach myInterrupt() to the interrupt
  if ( wiringPiISR (HOMEFLAME, INT_EDGE_FALLING, &homeflameInterrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }
  
  if ( wiringPiISR (HOMEGAS, INT_EDGE_FALLING, &homegasInterrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }

  // display counter value every second.
  while ( 1 ) {
    //printf( "%d\n", eventCounter );
    //eventCounter = 0;
    //delay( 1000 ); // wait 1 second
	if(homeflamedetect == 0x01)
	{
		printf("home flame detected...\n");
		homeflamedetect = 0;
	}
	if(homegasdetect == 0x01)
	{
		printf("gas detected...\n");
		homegasdetect = 0;
	}
  }

  return 0;
}

 

 

이상 끝. 이렇게 하나씩 해야 할까 보다. SSyang!

 

 

 

반응형