본문 바로가기
아두이노 응용

[프로젝트] 간단한 단거리 무선 통신(433/315Mhz)으로 구현하는 실내외 원격 온습도계 디스플레이 프로젝트

by 작동미학 2015. 2. 9.
반응형

원격 아두이노에서 온습도(DHT22)를 받아, 또다른 아두이노에 전송(433/315Mhz)해 화면(OLED)에 표시하는 장치 구현

 

▶ 이 가이드를 따라하면

- 두 개의 아두이노로 무선으로 근거리(100m미만)를 통해 숫자를 주고 받아 화면에 표시하는 기능을 구현한다

- 온/습도를 구하고, 433/315Mhz, OLED 제어 세가지를 융합한 실제 작동 세트를 구현한다.

 

▶ 먼저 읽으면 좋은 글

- 라이브러리 설치 방법 : http://bbangpan.tistory.com/1

- DHT22 : http://bbangpan.tistory.com/3

- 433Mhz 네트워크 : http://bbangpan.tistory.com/9

- OLED : http://bbangpan.tistory.com/4

- Aliexpress에서 부품 저렴하게 구매하는 방법 : http://bbangpan.tistory.com/5

 

▶ 부품 설명 및 회로 구성

이 프로젝트는 지금까지 bbangpan.com에 게시된 3가지 기본 강좌(OLED, 433/315Mhz 무선, DHT22)를 합하여 작은 프로젝트를 진행한 것이다. 간략하게는 하나의 아두이노에 DHT22(온습도 센서)와 433/315Mhz 송신기를 갖추고, 다른 하나에는 OLED와 433/315Mhz 수신기를 갖춘 후 원격에서 측정된 온습도 값을 화면에 출력하는 기기이다.

두 장치를 같이 보면 다음과 같다. 왼쪽은 배터리로 구동되는 DHT22/RF 송신기를 갖춘 아두이노(납땜용 쉴드와 배터리가 두꺼워보이지만 일반 아두이노 UNO이다)이고, 오른쪽이 RF 수신기와 OLED를 갖춘 아두이노이다. 왼쪽의 온도를 무선전송받아 오른쪽에서 표시하는 장치이다. DHT22 사용은 http://bbangpan.tistory.com/3 강좌를 참조한다.

<무선 통신 온습도 표시 장치 송신기/수신기 예제>

송신기는 http://bbangpan.tistory.com/3 강좌와 http://bbangpan.tistory.com/9 강좌의 합본으로 탄생한다. 와이어링(wiring)이 복잡해 보이지만, 5V/GND만 빵판을 이용해서 연결해준 것 뿐이고, 나머지는 각 강좌의 회선 연결과 유사하다. 전원은 장치 편의상 들고 다니기 위해 AA배터리 6개짜리 팩을 사용했고, 탑재된 아두이노 쉴드는 납땜을 위한 프로토타이핑으로 신경쓰지 않아도 된다.

<송신회로 구성/DHT22, 433Mhz 송신기 연결/연결 핀은 소스 참조>

<수신회로 구성/OLED, 433Mhz 수신기 연결/연결 핀은 소스 참조>

실제 구동해보면 OLED장치에 온습도가 주기적으로 표기되는 것을 확인할 수 있다. 베란다나 집 밖의 온도변화 등을 실내에서 확인할 수 있는 간단한 장치가 된다.

참고로 빵판은 배선을 손쉽게 하기 위한 판이고, 아래와 같은 소형도 존재한다. 연결된 라인중 하나의 소켓에 선을 꼽으면 나머지 점들과도 같이 납땜한 것과 동일한 효과를 낸다. 여기서는 5V, GND를 두 개 이상 모듈에 연결하기 위해 사용하였다. 아래 그림에서 연결 예제를 참조하자. OLED 모듈을 빵판에 꼽고 각각의 VCC/GND/SCL/SDA를 점퍼선을 이용해 아두이노에 연결한 그림이다.

<빨간색 선이 가상의 연결을 나타냄, 최대 5개씩 연결가능한 소형빵판 예시>

▶ 소스 코드 입력 및 구동

라이브러리는 VirtualWire: http://www.airspayce.com/mikem/arduino/VirtualWire/index.html 와 U8glib: https://code.google.com/p/u8glib/ 을 사용한다.

Arduino 기본 IDE 강좌( http://bbangpan.tistory.com/1 )를 참조로 해당 라이브러리 zip을 다운받아 아두이노설치폴더/libraries/ 에 해당 라이브러리 폴더가 위치하도록 한다.

아래는 송수신 모듈을 각기 표기하였다. 데이터핀의 경우는 내부 코드를 조절하여 변경 가능하다(디폴트로는 기본 핀이 선택되며 무선송신 모듈은 필자 편의상, 아래 소스에서는 D4로 변형되어 있다. 필요에 따라 조절한다)

    

송신 모듈 소스 원본 GitHub 링크 참조 : https://github.com/bbangpan/bbangpan.com/blob/master/neibc_433_315mhz_trans_dht22/neibc_433_315mhz_trans_dht22.ino

http://www.bbangpan.com -----------------------------------

/* Originally posted on www.bbangpan.com

 

Program Description: 433/315Mhz transmitter(FS1000A or XY-FS) and DHT22 test code

Needed library : VirtualWire http://www.airspayce.com/mikem/arduino/VirtualWire/index.html , DHT

DataPin : 4 to 433/315Mhz transmitter DATA(near GND), 9 to DHT22

Tested on : Arduino 1.0.6, Arduino UNO R3, VirtualWire, DHT

 

Inspired by http://www.HobbyComponents.com

 

Copyright (c) 2015 www.bbangpan.com. All rights reserved.

This program can be used for any non-commercial purpose freely. */

 

#include <VirtualWire.h>

#include "DHT.h"

 

#define DHTPIN 9 // what pin we're connected to

#define DHTTYPE DHT22

 

/* Digital IO pin that will be used for sending data to the transmitter */

const int TX_DIO_Pin = 4;

const int SENDINTERVAL = 5000;

 

DHT dht(DHTPIN, DHTTYPE);

 

void setup()

{

Serial.begin(9600);

 

dht.begin();

pinMode(13, OUTPUT);

vw_set_tx_pin(TX_DIO_Pin);

vw_setup(2000); // transmit speed : 2000 bits per sec

}

 

/* Main program */

void loop()

{

int Data;

byte TxBuffer[4];

 

float h = dht.readHumidity();

float t = dht.readTemperature();

 

if (isnan(t) || isnan(h)) {

Serial.println("Failed to read from DHT");

} else {

Serial.print("Humidity: ");

Serial.print(h);

Serial.print(" %\t");

Serial.print("Temperature: ");

Serial.print(t);

Serial.println(" *C");

 

Data = (int)(t*10);

TxBuffer[0] = Data >> 8;

TxBuffer[1] = Data;

Data = (int)(h*10);

TxBuffer[2] = Data >> 8;

TxBuffer[3] = Data;

 

digitalWrite(13, HIGH);

/* Send the data (2 bytes) */

vw_send((byte *)TxBuffer, 4);

/* Wait until the data has been sent */

vw_wait_tx();

 

/* Turn off the LED on pin 13 to indicate that we have

now sent the data */

digitalWrite(13, LOW);

}

delay(SENDINTERVAL);

}

-------------------------------------------------------

 

수신 모듈 소스 원본 GitHub 링크 참조 : https://github.com/bbangpan/bbangpan.com/blob/master/neibc_433_315mhz_recv_dht22_oled/neibc_433_315mhz_recv_dht22_oled.ino

http://www.bbangpan.com -----------------------------------

/* Originally posted on www.bbangpan.com

 

Program Description: 433/315Mhz receiver(XY-MK-5V) test code (for dht22 transmit, oled display)

Needed library : VirtualWire http://www.airspayce.com/mikem/arduino/VirtualWire/index.html, U8glib

DataPin : 11 to 433/315Mhz receiver DATA

Tested on : Arduino 1.0.6, Arduino UNO R3, VirtualWire, U8glib

 

Inspired by http://www.buildcircuit.com/how-to-use-rf-module-with-arduino/

 

Copyright (c) 2015 www.bbangpan.com. All rights reserved.

This program can be used for any non-commercial purpose freely. */

 

 

#include <VirtualWire.h>

#include "U8glib.h"

 

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);

 

const int RX_DIO_Pin = 11; // default 11

float t,h;

 

void draw(float tval, float hval) {

u8g.setFont(u8g_font_unifont);

 

u8g.setPrintPos(1,10);

u8g.print("Temp: ");

u8g.print(tval);

u8g.print(" C");

u8g.setPrintPos(1,23);

u8g.print("Hum: ");

u8g.print(hval);

u8g.print(" %");

}

 

void setup()

{

t=0;

h=0;

 

pinMode(13, OUTPUT);

Serial.begin(9600);

 

vw_set_rx_pin(RX_DIO_Pin);

vw_setup(2000);

vw_rx_start();

 

u8g.firstPage();

do {

draw(0,0);

} while( u8g.nextPage() );

}

 

void loop()

{

uint8_t Buffer_Size = 4;

int Data;

uint8_t RxBuffer[Buffer_Size];

 

/* Accept the transmitted data */

if (vw_get_message(RxBuffer, &Buffer_Size)) // Non-blocking

{

/* If so, then turn on the LED connected to DIO 13

to indicate this */

digitalWrite(13, HIGH);

 

/* Store the received high and low byte data */

Data = RxBuffer[0] << 8 | RxBuffer[1];

t = (float)Data / 10.0;

Data = RxBuffer[2] << 8 | RxBuffer[3];

h = (float)Data / 10.0;

 

Serial.print("Temparature : ");

Serial.print(t);

Serial.print(" Humidity : ");

Serial.println(h);

/* Turn off the LED on pin 13 to indicate that the

data has now been received */

digitalWrite(13, LOW);

 

u8g.firstPage();

do {

draw(t, h);

} while( u8g.nextPage() );

 

delay(500);

}

}

-------------------------------------------------------

이처럼 다양하게 활용하여 무선 모듈을 통해 데이터를 원격으로 주고 받고 각종 센서 값을 받아 OLED에 출력이 가능하다.

 

▶ 구매 가이드

 

RF 송수신기 : http://bbangpan.tistory.com/9

OLED(디스플레이) : http://bbangpan.tistory.com/4

DHT22(온습도계) : http://bbangpan.tistory.com/3

 

강의 키워드

아두이노 무선 통신 온도계, 아두이도 OLED 응용, 아두이노 DHT22 응용, 아두이노 무선 통신 응용, 원격 온습도계

반응형