کدی که گفتم :
سمت فرستنده :
کد:
#include <RH_NRF24.h>
RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
void setup()
{
Serial.begin(9600);
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
}
void loop()
{
if (Serial.available() > 0) {
char c = Serial.read();
Serial.println(c);
if (c == '1') { b
Serial.println("Sending to nrf24_server");
uint8_t data[] = "1";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
}
else if (c == '2') {
Serial.println("Sending to nrf24_server");
uint8_t data[] = "2";
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
}
else {
Serial.println("unknown character");
}
}
}
کد سمت گیرنده :
کد:
#include <SPI.h>
#include <RH_NRF24.h>
RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
void setup()
{
Serial.begin(9600);
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
if (nrf24.available())
{
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
String str = (char*)buf;
if (str == "1") {
Serial.println("got request:1 ");
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
if (str == "2") {
Serial.println("got request:2 ");
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
}
}
else
{
Serial.println("recv failed");
}
}
}
کتابخونه ای که استفاده کردم اتچ شده