Truyền Tín Hiệu Với Module Radio Frequence 433Mhz

download Truyền Tín Hiệu Với Module Radio Frequence 433Mhz

of 22

description

Truyền Tín Hiệu Với Module Radio Frequence 433Mhz

Transcript of Truyền Tín Hiệu Với Module Radio Frequence 433Mhz

Truyn tn hiu vi module radio frequence 433Mhz

Truyn tn hiu vi module radio frequence 433MhzNTP_PROgi voTh nm, 14 Thng 8, 2014 - 19:30Xin cho cc bn, bi vit hm nay ca mnh s gii thiu v cch truyn d liu khng dy bng modules RF (radio frequence) 433Mhz.PHN CNGBoard Arduino x2 ( mnh s dngArduino UNO R3v Arduino Leonardo).Module RF 433 Mhz ( gm 1transmitter modulev 1receiver module) hoc 315MHz.Breadboardx2.LED xanh, vng, .in tr (220 - 560 m)pin 9V x2.Dy dn.GII THIUMODULE RF 433Mhz:y l 1 module gi r, dng sng radio truyn tn hiu gia b pht (transmitter module) v b thu (receiver module).Mo: tng hiu qu v khong cch khi s dng module RF, cc bn c th ch 2 ci ngten bng dy ng cun thnh l xo ri gn vo ngten ca transmitter v receiver.nh module ca mnh:

-Transmitter module: l ci module nh hn , gm 3 chn: t tri qua phi: DATA (hoc ATAD), VCC, GND.Transmitter moduleArduino

DATAdigital Pin

VCC5V

GNDGND

-Receiver module: l ci to hn, gm 4 chn: t tri qua phi: GND, DATA, DATA, VCC. (chng ta ch dng 1 trong 2 chn DATA ny)Transmitter moduleArduino

DATAdigital Pin

VCC5V

GNDGND

hiu r hn v sng radio, cc bn c th tham kho ti bi vit sau:http://arduino.vn/bai-viet/277-song-vo-tuyen-la-gi-va-nhung-suc-manh-cua-no-khi-ket-hop-voi-arduinoChng ta s tm hiu cch truyn d liu vi RF module ngay by gi.LP MCH

Trong Transmitter module, mnh s dng pin 8 pht tn hiu.Trong Receiver module, mnh cng s dng pin 8 nhn tn hiu.LP TRNH s dng RF module 1 cch d dng hn, chng ta s download th vin VirtualWire ti link sau:http://www.airspayce.com/mikem/arduino/VirtualWire/VirtualWire-1.27.zipSau , cc bn m Arduino IDE, chn Sketch/ Import Library.../ Add Library... v chn vo file .zip va ti v ci t th vin. Transmitter module, mnh s nhn d liu t cng Serial, sau gi sang Receiver module.Transmitter:#include // khai bo th vin VirtualWirevoid setup(){ Serial.begin(9600);// giao tip Serial vi baudrate 9600 Serial.println("Ready........."); vw_set_ptt_inverted(true);// yu cu cho RF link modules vw_setup(1024);// ci t tc truyn tn hiu vw_set_tx_pin(8);// ci t chn digital pht tn hiu}void loop(){ char text[20] = "";// khai bo string dng array byte i = 0; while (Serial.available() == 0) { // nothing } while (Serial.available() > 0) { char ch = Serial.read(); text[i] = ch; i++; delay(5); } Serial.print("sent: "); Serial.println(text); vw_send((byte *)text, sizeof(text));// gi tn hiu i vw_wait_tx(); delay(100);}Receiver:#include // khai bo th vin VirtualWirebyte msg[VW_MAX_MESSAGE_LEN];// bin lu d liu nhn cbyte msgLen = VW_MAX_MESSAGE_LEN;void setup(){ Serial.begin(9600); Serial.println("READY.........."); vw_setup(1024); // ci t tc truyn tn hiu vw_set_rx_pin(8);// ci t chn digital nhn tn hiu vw_rx_start();// bt u nhn tn hiu}void loop(){ if (vw_get_message(msg, &msgLen)) // nu c tn hiu c truyn n { Serial.print("got: "); for (int i = 0; i < msgLen; i++) { Serial.write(msg[i]);// in k t ra mn hnh } Serial.println(); }}Ch : Khi upload 2 on code trn ln 2 boards Arduino, cc bn cn bt 2 ca s Arduino IDE ring bit (tc l phi 2 ln double click vo biu tng Arduino IDE), v mi board s c 1 cng COM ring, nn cc bn ch vo Tools / Serial Port... chn ng cng COM cho board.

Sau khi upload code ln 2 boards Arduino, cc bn m 2 ca s Serial ca 2 boards, ri nhp vi k t vo ca s Serial ca Transmitter, KQ s tng t th ny:

GII THCH- Transmitter s nhn d liu t Bn phm qua cng Serial, sau Transmitter s gi d liu ny n Receiver bng sng radio, khi Receiver nhn c tn hiu, n s gii m v in ra ca s Serial.- Theo mc nh, th vin VirtualWire chn pin 12 pht tn hiu, pin 11 nhn tn hiu. Cc bn c th ty chn 1 chn digital bt k gi v nhn tn hiu vi dng lnh:vw_set_tx_pin(pin) i vi transmitter vvw_set_rx_pin(pin) i vi Receiver.- Cc keywords:vw_set_ptt_inverted(true): cn phi c i vi RF link modules, c th khng cn thit i vi cc loi modules khc.vw_setup(speed): thit lp tc truyn d liu gia Transmitter v Receiver, n v: bits/s. Ch : tc ca Transmitter v Receiver phi bng nhau.vw_set_tx_pin(pin): thit lp chn pht tn hiu.vw_set_rx_pin(pin): thit lp chn nhn tn hiu.vw_rx_start(): bt u nhn tn hiu.vw_send((byte *)data, sizeof(data)): hm gi tn hiu i, gm 2 tham s l:(byte *)data:bin con tr (byte *)data, trong : data l bin cha d liu cn c truyn i, bin data cn c khai bo l bin mng. C th v d trn l bin text[20].sizeof(data):kch thc ca d liu c truyn i, tnh theo byte. Ch : nu bn khai bo string kiu con tr, VD: const char *text = "hello"; lc ny bn s phi dng strlen(text) thay v sizeof(text).vw_get_message(msg, &msgLen): hm nhn tn hiu, s tr v gi trtruekhi c tn hiu c gi ti, gm 2 tham s l:msg:bin con tr msg, trong : msg l bin cha d liu va nhn c, bin msg cn c khai bo l bin mng. v d trn l bin msg[VW_MAX_MASSAGE_LEN].&msgLen:a ch ca bin msgLen, bin msgLen ch kch thc ca d liu nhn c.vw_wait_tx(): i trc khi truyn tn hiu tip theo.VW_MAX_MASSAGE_LEN: th vin VirtualWire define sn kch thc ti a ca d liu l 30 bytes.By gi, mnh s s dng sng radio iu khin cc thit b in t.LP MCHTransmitter

Receiver

LP TRNHMnh s gi lnh t Transmitter n Receiver iu khin bt/tt cc LED.Lnh s c c php: "number STATE"VD: 1 ON ===> led sng, 2 OFF ===> led xanh tt, 3 ON ===> led vng sng, A ON ===> bt tt c led, A OFF ===> tt tt c led,...Transmitter (tng t nh v d trn):#include void setup(){ Serial.begin(9600); Serial.println("Ready........."); vw_set_ptt_inverted(true); vw_setup(1024); vw_set_tx_pin(8);}void loop(){ char text[20] = ""; byte i = 0; while (Serial.available() == 0) { // nothing } while (Serial.available() > 0) { char ch = Serial.read(); text[i] = ch; i++; delay(5); } Serial.print("sent: "); Serial.println(text); vw_send((byte *)text, sizeof(text)); vw_wait_tx(); delay(100); }Receiver:#include byte msg[VW_MAX_MESSAGE_LEN];byte msgLen = VW_MAX_MESSAGE_LEN;const int RED = 7; // led const int GREEN = 6; // led xanhconst int YELLOW = 5; // led vngString text = ""; // bin String lu d liu void setup(){ Serial.begin(9600); Serial.println("READY.........."); pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(YELLOW, OUTPUT); vw_setup(1024); vw_set_rx_pin(8); vw_rx_start();}/* select */void select(byte number, byte name){

// lnh c dng "number STATE", VD: 1 ON, 2 OFF, 3 ON... // i bt u ti 2 b qua "number" v "khoang trang" // bt u kim tra c "STATE" lun for (int i = 2; i < msgLen; i++)

{ text += char(msg[i]); } Serial.print("got: "); // in kt qu ra ca s Serial Serial.print(number); Serial.print("-"); Serial.println(text); /* nu "STATE" = ON ==> bt, nu "STATE" = OFF ==> tt*/ if (text == "ON") { digitalWrite(name, 1); } else if (text == "OFF") { digitalWrite(name, 0); }}/* select All*/void selectAll(){ for (int i = 2; i < msgLen; i++) { text += char(msg[i]); } Serial.print("got: ALL"); Serial.print("-"); Serial.println(text); if (text == "ON") { digitalWrite(RED, 1); digitalWrite(GREEN, 1); digitalWrite(YELLOW, 1); } else if (text == "OFF") { digitalWrite(RED, 0); digitalWrite(GREEN, 0); digitalWrite(YELLOW, 0); }}void loop(){ if (vw_get_message(msg, &msgLen)) { if (msg[0] == '1') { select(1, RED); } else if (msg[0] == '2') { select(2, GREEN); } else if (msg[0] == '3') { select(3, YELLOW);

} else if (msg[0] == 'A') { selectAll(); } } text = ""; // reset bin text}Sau khi upload thnh cng v test th, chng ta s c ca s Serial nh sau:

y l 1 s hnh nh v project ca mnh

LI KTKt hp vi rle, cc bn hon ton c th iu khin t xa cc thit b in trong gia nh thng qua my vi tnh.Chc cc bn thnh cng!Mi thc mc v sai st cc bn c comment nh!^^11066 lt xem