강좌/RaspberryPI 활용

Raspberry pi 와 Arduino Pro Mini - RF 433 통신하기

acidpop 2017. 2. 3. 18:17
반응형

라즈베리파이(송신) ---> 아두이노 프로 미니로(수신) RF 통신을 이용하여 LED 를 켜보자


1. 준비물


  - 라즈베리파이( 2 또는 3) 1개


  - 아두이노 프로 미니 1개

  - Usb to TTL(아두이노 프로 미니에 프로그램 업로드 하기 위해 사용) 1개

    https://goo.gl/TJTMGo  (Usb To TTL + 아두이노 프로 미니 묶음 판매)


  - RF 433 Emmiter 1개

  - RF 433 Receiver 1개

    https://goo.gl/4KMIdi    (5쌍 묶음 판매)


  - LED 1개




2. 회로 구성 하기.


  2.1 라즈베리파이 회로 구성








RF 송신모듈에 각각 위와 같이 연결합니다.


 Raspberry pi

 RF Emitter

5V

Vcc 

GPIO 21

Data 

GND

GND 




  2.2 아두이노 프로 미니 회로 구성하기







Arduino Pro Mini

 RF Receiver

Vcc

Vcc 

PIN 2

Data 

GND

GND 


3. 라즈베리 파이 RC-Switch 라이브러리 준비

 

cd /home/pi

git clone https://github.com/acidpop/433Utils

cd 433Utils

cd rc-switch

make



4. Source Coding

  4.1. 라즈베리파이

 

다음 경로로 이동

cd /home/pi/433Utils/RPi_utils 

 

codesend.cpp 파일을 열어

 

int PIN =0;

위 코드를

int PIN = 21;

로 변경

 

또는 아래와 같이 소스 작성 후 컴파일



rftest.cpp 


/* Code Send */

#include "RCSwitch.h"

#include 
#include 
 
int main(int argc, char *argv[]) {
 
    // This pin is not the first pin on the RPi GPIO header!
    // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
    // for more information.
    int PIN = 21;
 
    // Parse the firt parameter to this command as an integer
    int code = atoi(argv[1]);
 
    if (wiringPiSetup () == -1) return 1;
    printf("sending code[%i]\n", code);
    RCSwitch mySwitch = RCSwitch();
    mySwitch.enableTransmit(PIN);
 
    mySwitch.send(code, 24);
 
    return 0;
 
}


 

위 코드를 컴파일 시에는 다음과 같이 진행한다.

 

g++ rftest.cpp -DRPI ../rc-switch/RCSwitch.cpp rftest.cpp -lwiringPi

 


  4.2 아두이노 프로 미니

 

        RCSwitch 라이브러리를 포함시켜야 한다. 


#include "RCSwitch.h"
#include 
#include 
RCSwitch mySwitch = RCSwitch();
 
void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
}
 
void loop() {
  if (mySwitch.available()) {
 
    int value = mySwitch.getReceivedValue();
 
    if (value == 0) {
      Serial.print("Unknown encoding");
    } else {
 
     Serial.print("Received ");
      Serial.print( mySwitch.getReceivedValue() );
      Serial.print(" / ");
      Serial.print( mySwitch.getReceivedBitlength() );
      Serial.print("bit ");
      Serial.print("Protocol: ");
      Serial.println( mySwitch.getReceivedProtocol() );

      switch(value):{
        case 1001:{
           digitalWrite(LED_BUILTIN, HIGH);
        }break;
        case 1002:{
           digitalWrite(LED_BUILTIN, LOW);
        }break;
      }
    }
 
    mySwitch.resetAvailable();
 
  }
}


 

 

 

 

아두이노에 프로그램을 컴파일 -> 업로드 하고

 

툴 -> 시리얼 모니터를 선택하여 시리얼 모니터 창이 보이도록 한다.

 

 

라즈베리파이에서 컴파일 해둔 rftest 를 시험해보자.

 

sudo rftest 12345

 

 

RF 송신기로 12345 라는 값을 보내면

 

아두이노의 시리얼 모니터로 12345 라는 값을 받았다는 메시지가 보이면 성공이다.

 

RF 송신기로 1001 을 보내면 아두이노에 연결된 LED 가 켜지고

 

1002 를 보내면 연결된 LED 가 꺼진다.

 

 

 

위 예제를 이용하면 라즈베리 파이 1대와

 

아주 값싼 아두이노 프로 미니를 원격으로 컨트롤 할 수 있다.

 

릴레이를 컨트롤 하면 원격으로 조정 할수 있는 멀티탭도 만들수 있다.

 

테스트 해본 결과 집 내부 가장 끝 방에서 끝방까지 (콘크리트 벽 2, 나무 방문 2개) RF 안테나 없이 송수신이 되는것을 확인 하였다.

 

 

반응형