this is sketch of nrf node :
#define MY_DEBUG
#define MY_RADIO_NRF24
#define MY_RF24_CHANNEL 0
#define MY_REPEATER_FEATURE
#define MY_NODE_ID 6
#define MY_RF24_PA_LEVEL RF24_PA_MAX
#define MY_TRANSPORT_WAIT_READY_MS 10000
#include <SPI.h>
#include <MySensors.h>
#include <VirtualWire.h>
int rx = 3;
int tx = 4;
bool stateA = false;
bool stateB = false;
#define CHILD_IDA 1
#define CHILD_IDB 2
MyMessage msgA(CHILD_IDA, V_STATUS );
MyMessage msgB(CHILD_IDB, V_STATUS );
void setup()
{
vw_set_rx_pin(rx);
vw_set_tx_pin(tx);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_rx_start();
send(msgA.set(false));
send(msgB.set(false));
}
void presentation()
{
sendSketchInfo("RF315", "1.0");
present(CHILD_IDA, S_LIGHT);
present(CHILD_IDB, S_LIGHT);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '1')) {
send(msgA.set(true));
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '3') ) {
send(msgA.set(false));
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '5') ) {
send(msgB.set(true));
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '7') ) {
send(msgB.set(false));
}
else {
for (int i = 0; i < 10; i++)
buf [i] = ' ' ;
}
}
}
void receive(const MyMessage &message) {
if (message.type == V_STATUS) {
switch (message.sensor) {
case 1:
stateA = message.getBool();
if ( stateA == true) {
send(msgA.set(false));
const char *msg = "100";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}
if (stateA == false) {
send(msgA.set(true));
const char *msg = "102";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}
break;
case 2:
stateB = message.getBool();
if ( stateB == true) {
send(msgB.set(false));
const char *msg = "104";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}
if (stateB == false) {
send(msgB.set(true));
const char *msg = "106";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
}
break;
}
}
}
and this is for receiver:
#include <VirtualWire.h>
int rx = 3;
int tx = 4;
int relay1 = 5;
int relay2 = 6;
int key1 = 7;
int key2 = 8;
void setup()
{
Serial.begin(115200);
Serial.println("setup");
vw_set_rx_pin(rx);
vw_set_tx_pin(tx);
vw_set_ptt_inverted(true);
vw_setup(2000);
vw_rx_start();
pinMode(key1, INPUT_PULLUP);
pinMode(key2, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen))
{
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '0') )
{
digitalWrite(relay1, LOW);
delay(100);
const char *msg = "101";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(100);
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '2'))
{
digitalWrite(relay1, HIGH);
delay(100);
const char *msg = "103";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(100);
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '4'))
{
digitalWrite(relay2, LOW);
delay(100);
const char *msg = "105";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(100);
}
if ((buf[0] == '1') && (buf[1] == '0') && (buf[2] == '6'))
{
digitalWrite(relay2, HIGH);
delay(100);
const char *msg = "107";
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx();
delay(100);
}
else {
for (int i = 0; i < 10; i++)
buf [i] = ' ' ;
}
}
}
this code was worked. but there is some problem that i can not found ! some code sent and some dont send. also some time for many time can not work just with reset or power off/on. @hek please tag some members and programmer here that they can repair this code. this code is very efficient...
then you can use this sketch in site for RF 315 and 433 mhz device...
this code can solve some problem about short range of NRF. we can define many child id that every child can control many device with RF315 or RF433. for example for more distance we can use this node. with 315mhz and long distance...