用超再生模块传输数据
淘宝上大把的315与433MHZ超再生模块一组才三五块,价钱可说是非常的平易近人上次有人在论坛提到修改红外线库来控制这模块,这次就介绍个更方便用的库:RadioHead
首先是库的官网:http://www.airspayce.com/mikem/arduino/RadioHead/
下载连结:http://www.airspayce.com/mikem/arduino/RadioHead/RadioHead-1.41.zip
硬件连接方式很简单,发送端默认D12脚,接收端默认D11脚,加上电源与接地就各三条线
想试试效果可以先用库提供的范例,下一篇再做说明
发送端:
// ask_transmitter.pde
#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
const char *msg = "hello";
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
}
接收端:
// ask_receiver.pde
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver;
void setup()
{
Serial.begin(9600); // Debugging only
if (!driver.init())
Serial.println("init failed");
}
void loop()
{
uint8_t buf;
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
driver.printBuffer("Got:", buf, buflen);
}
}
参数设置在这行:RH_ASK driver;
这个库默认的传输速度:2000bps,接收端D11,发送端D12
例如RH_ASK driver(3000,9,10); 表示传输速度3Kbps,接收端D9,发射端D10
可发送的内容是任何uint8_t的阵列,或者说是byte或char阵列如发送范例使用的一样
接收端基本上照著发送端设置,接收到的讯息可以直接从buf[]中取回
另外,真的不要期待这模块的速度,而且还存在同频率的模块间干扰问题 同频率干扰?不能像nrf设置收发ID么? 從庫文件看是沒有設定收發位址的功能,多組使用應該會干擾
買了一組315模塊做遙控車,發現反應很慢就丟著了 这库很全呀,非常好
页:
[1]