2

I am using Nrf24Lo1 to communicate a payload of button commands resulting in leds being lit on second. This is working OK. My aim is to have one as Tx and more than one as RX, I can find several that work in the opposite direction, but none can be adapted. Is it possible to have more than one payload (say "payload2" sent to its own pipe address (Rx)?

//Payload_TX
#include "SPI.h"
#include "RF24.h"
#include "nRF24L01.h"
#define CE_PIN 7
#define CSN_PIN 8
#define INTERVAL_MS_TRANSMISSION 250
RF24 radio(CE_PIN, CSN_PIN);
#define WHICH_NODE 1     // must be a number from 1 - 6 identifying the PTX node

const uint64_t wAddress[] = {0x7878787878LL, 0xB3B4B5B6F1LL, 0xB3B4B5B6CDLL, 0xB3B4B5B6A3LL, 0xB3B4B5B60FLL, 0xB3B4B5B605LL};
const uint64_t PTXpipe = wAddress[0]; 
const int ip1 = 3;
const int ip2 = 6;
const int ip3 = 15;
const int ip4 = 16;
const int ip5 = 17;
const int ip6 = 18;

//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
  byte button1;
  byte button2;
  byte button3;
  byte button4;
  byte button5;
  byte button6;
};
payload payload;

//struct payload2 {
// byte data1;
//char data2;
//};
//payload2 payload2;

void setup()
{
  Serial.begin(115200);
  pinMode(ip1, INPUT_PULLUP);
  pinMode(ip2, INPUT_PULLUP);
  pinMode(ip3, INPUT_PULLUP);
  pinMode(ip4, INPUT_PULLUP);
  pinMode(ip5, INPUT_PULLUP);
  pinMode(ip6, INPUT_PULLUP);

  radio.begin();
  //Append ACK packet from the receiving radio back to the transmitting radio
  radio.setAutoAck(false); //(true|false)
  //Set the transmission datarate
  radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)
  //Greater level = more consumption = longer distance
  radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)
  radio.setChannel(108);
  //Default value is the maximum 32 bytes
  radio.setPayloadSize(sizeof(payload));
  //radio.setPayloadSize(sizeof(payload2));
  //Act as transmitter
  radio.openWritingPipe(PTXpipe);
  radio.stopListening();
}
void loop()
{
  radio.openWritingPipe(PTXpipe);
  payload.button1 = digitalRead(ip1);
  payload.button2 = digitalRead(ip2);
  payload.button3 = digitalRead(ip3);
  payload.button4 = digitalRead(ip4);
  payload.button5 = digitalRead(ip5);
  payload.button6 = digitalRead(ip6);
  radio.write(&payload, sizeof(payload));
  Serial.print("Data1:");
  Serial.println(payload.button1);
  Serial.print("Data2:");
  Serial.println(payload.button2);
  Serial.println("Sent");
  delay(INTERVAL_MS_TRANSMISSION);
}`

 // payload Rx
 #include "SPI.h"
#include "RF24.h"
#include "nRF24L01.h"
#define led1 3
#define led2 4
#define led3 5
#define CE_PIN 7
#define CSN_PIN 8
#define INTERVAL_MS_SIGNAL_LOST 1000
#define INTERVAL_MS_SIGNAL_RETRY 250
RF24 radio(CE_PIN, CSN_PIN);
//const byte address[6] = "00001";
const uint64_t rAddress[] = {0x7878787878LL, 0xB3B4B5B6F1LL, 0xB3B4B5B6CDLL, 0xB3B4B5B6A3LL, 0xB3B4B5B60FLL, 0xB3B4B5B605LL };
//NRF24L01 buffer limit is 32 bytes (max struct size)
struct payload {
  byte button1;
  byte button2;
  byte button3;
  byte button4;
  byte button5;
  byte button6;
};
payload payload;
unsigned long lastSignalMillis = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);


  radio.begin();
  //Append ACK packet from the receiving radio back to the transmitting radio
  radio.setAutoAck(false); //(true|false)
  //Set the transmission datarate
  radio.setDataRate(RF24_250KBPS); //(RF24_250KBPS|RF24_1MBPS|RF24_2MBPS)
  //Greater level = more consumption = longer distance
  radio.setPALevel(RF24_PA_MIN); //(RF24_PA_MIN|RF24_PA_LOW|RF24_PA_HIGH|RF24_PA_MAX)
  radio.setChannel(108);
  //Default value is the maximum 32 bytes1
  radio.setPayloadSize(sizeof(payload));
  //Act as receiver
  radio.openReadingPipe(0,rAddress[0]);
  radio.startListening();
}
void loop()
{
  unsigned long currentMillis = millis();
  if (radio.available() > 0) {
    radio.read(&payload, sizeof(payload));
    Serial.println("Received");
    Serial.print("Button1:");
    Serial.println(payload.button1);
    Serial.print("Button2:");
    Serial.println(payload.button2);
    if (payload.button1 == 0) {
      digitalWrite(led1, HIGH);
    } else {
      digitalWrite(led1, LOW);
    }
    if (payload.button2 == 0) {
      digitalWrite(led2, HIGH);
    } else {
      digitalWrite(led2, LOW);
    }
    if (payload.button3 == 0) {
      digitalWrite(led3, HIGH);
    } else {
      digitalWrite(led3, LOW);
    }
    lastSignalMillis = currentMillis;
  }
  if (currentMillis - lastSignalMillis > INTERVAL_MS_SIGNAL_LOST) {
    lostConnection();
  }

}
void lostConnection()
{
  Serial.println("We have lost connection, preventing unwanted behavior");
  delay(INTERVAL_MS_SIGNAL_RETRY);
}

I am rather old any any help gratefully received. Thanks for reading.

1
  • Just change the address you send to with radio.openWritingPipe(...)...?
    – Majenko
    Commented Apr 5, 2022 at 13:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.