Rf24 Receiver Not Sending a response

I am having an esp32 and an arduino uno each of them connected to the rf module nrf24

here my esp is my transmitter and my arduino is my reciever , now what i am trying to do is that when i send 1 from esp the led which is connected on the gpio 4 of arduino uno should light up and if I send 0 it should turn off, i have written a code for this application and that is working fine

Now I want to add a part that when the receiver receives the data and the led is on, the receiver sends "LED on " to the transmitter and that should be received at the transmitter end and the transmitter should print it on the serial terminal

now I am unable to crack this response part any one can help please ?

here is my transmitter code
#include <RF24.h>

// Define the pins for CE and CSN
#define CE_PIN 27
#define CSN_PIN 5

RF24 radio(CE_PIN, CSN_PIN);

// Address of the receiving module
const byte address[6] = "00001";
char data;

void setup() {
Serial.begin(115200);

// Initialize the NRF24L01+ module
if (!radio.begin()) {
Serial.println("NRF24L01+ initialization failed!");
while (1); // Stop if initialization failed
}

radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.stopListening();

Serial.println("Transmitter ready!");
}

void loop() {

Serial.println("Enter you data");
if(radio.available())
{
radio.openReadingPipe(0,address);
radio.startListening();
char rsp[32];
radio.read(&rsp,sizeof(rsp));
rsp[sizeof(rsp)] = '\0';
Serial.println(rsp);
}
while(!Serial.available())
{
;
}
data = Serial.read();
bool success = radio.write(&data, sizeof(data));

if (success)
{
Serial.println("Message sent successfully!");
}
else
{
Serial.println("Message failed to send.");
}

delay(100); // Send data every second
}

here is my receiver code
#include <RF24.h>

// Define the pins for CE and CSN
#define CE_PIN 9
#define CSN_PIN 10

RF24 radio(CE_PIN, CSN_PIN);

// Address of the transmitting module
const byte address[6] = "00001";

char data[] = "Arduino";

void setup() {
Serial.begin(115200);

// Initialize the NRF24L01+ module
if (!radio.begin()) {
Serial.println("NRF24L01+ initialization failed!");
while (1); // Stop if initialization failed
}

// Open pipes for reading and writing
radio.openReadingPipe(0, address);
radio.openWritingPipe(address); // Configure the writing pipe during setup
radio.setPALevel(RF24_PA_LOW);
radio.setDataRate(RF24_250KBPS);
radio.startListening(); // Start in listening mode

Serial.println("Receiver ready!");
pinMode(4, OUTPUT);
}

void loop() {
if (radio.available()) {
char text[32] = {0}; // Initialize and null-terminate the buffer
radio.read(&text, sizeof(text) - 1); // Read data into the buffer
text[sizeof(text) - 1] = '\0'; // Ensure null-termination

if (strcmp(text, "1") == 0) {
  digitalWrite(4, HIGH);
  Serial.println("Arduino LED On");

  // Stop listening and prepare to send a response
  radio.stopListening();
  delay(5); // Allow time for mode transition

  bool success = radio.write(&data, sizeof(data));
  if (success) {
    Serial.println("Response Sent Successfully");
  } else {
    Serial.println("Response Sending Failed");
  }

  // Return to listening mode
  radio.startListening();
} else {
  digitalWrite(4, LOW);
  Serial.println("Arduino LED Off");
}

}
}

Thanks in advance

Please post your sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

1 Like

After you follow @UKHeliBob suggestion add an annotated schematic showing exactly how you have wired this. Be sure to include all connections, power, ground, power sources and note any lead over 10"/25cm.

Power Stability Issues with RF24 Radio Modules

As described in the RF24 Common Issues Guide, radio modules, especially the PA+LNA versions, are highly reliant on a stable power source. The 3.3V output from Arduino is not stable enough for these modules in many applications. While they may work with an inadequate power supply, you may experience lost packets or reduced reception compared to modules powered by a more stable source.

Symptoms of Power Issues:

  • Radio module performance may improve when touched, indicating power stability issues.
  • These issues are often caused by the absence of a capacitor, a common cost-saving omission by some manufacturers.

Temporary Patch :

  • Add Capacitors: Place capacitors close to the VCC and GND pins of the radio module. A 10uF capacitor is usually sufficient, but the exact value can depend on your circuit layout.
  • Use Low ESR Capacitors: Capacitors with low Equivalent Series Resistance (ESR) are recommended, as they provide better power stability and performance.

Adding the appropriate capacitors can greatly improve the reliability of your RF24 module by ensuring a stable power supply, thus minimizing packet loss and enhancing overall performance. A separate power supply for the radios is the best solution.

There is no link to the part being used so I took a SWAG:
nRF24L01 Features
2.4GHz RF transceiver Module
Operating Voltage: 3.3V
Nominal current: 50mA
Range : 50 – 200 feet
Operating current: 250mA (maximum)
Communication Protocol: SPI
Baud Rate: 250 kbps - 2 Mbps.
Channel Range: 125
Maximum Pipelines/node : 6
Low cost wireless solution:

This is my transmitter code which is ESP32

#include <RF24.h>

// Define the pins for CE and CSN
#define CE_PIN 27
#define CSN_PIN 5

RF24 radio(CE_PIN, CSN_PIN);

// Address of the receiving module
const byte address[6] = "00001";
char data;

void setup() {
  Serial.begin(115200);

  // Initialize the NRF24L01+ module
  if (!radio.begin()) {
    Serial.println("NRF24L01+ initialization failed!");
    while (1); // Stop if initialization failed
  }
  
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();

  Serial.println("Transmitter ready!");
}

void loop() {
  
  Serial.println("Enter you data");
  if(radio.available())
  {
    radio.openReadingPipe(0,address);
    radio.startListening();
    char rsp[32];
    radio.read(&rsp,sizeof(rsp));
    rsp[sizeof(rsp)] = '\0';
    Serial.println(rsp);
  }
  while(!Serial.available())
  {
    ;
  }
  data = Serial.read();
  bool success = radio.write(&data, sizeof(data));

  if (success) 
  {
    Serial.println("Message sent successfully!");
  } 
  else 
  {
    Serial.println("Message failed to send.");
  }

  delay(100); // Send data every second
}

This is my receiver code

#include <RF24.h>

// Define the pins for CE and CSN
#define CE_PIN 9
#define CSN_PIN 10

RF24 radio(CE_PIN, CSN_PIN);

// Address of the transmitting module
const byte address[6] = "00001";

char data[] = "Arduino";

void setup() {
  Serial.begin(115200);

  // Initialize the NRF24L01+ module
  if (!radio.begin()) {
    Serial.println("NRF24L01+ initialization failed!");
    while (1)
      ;  // Stop if initialization failed
  }

  // Open pipes for reading and writing
  radio.openReadingPipe(0, address);
  radio.openWritingPipe(address);  // Configure the writing pipe during setup
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();  // Start in listening mode

  Serial.println("Receiver ready!");
  pinMode(4, OUTPUT);
}

void loop() {
  if (radio.available()) {
    char text[32] = { 0 };                // Initialize and null-terminate the buffer
    radio.read(&text, sizeof(text) - 1);  // Read data into the buffer
    text[sizeof(text) - 1] = '\0';        // Ensure null-termination

    if (strcmp(text, "1") == 0) {
      digitalWrite(4, HIGH);
      Serial.println("Arduino LED On");

      // Stop listening and prepare to send a response
      radio.stopListening();
      delay(5);  // Allow time for mode transition

      bool success = radio.write(&data, sizeof(data));
      if (success) {
        Serial.println("Response Sent Successfully");
      } else {
        Serial.println("Response Sending Failed");
      }

      // Return to listening mode
      radio.startListening();
    } else {
      digitalWrite(4, LOW);
      Serial.println("Arduino LED Off");
    }
  }
}

till today i am able to send a msg in json format and receive it successfully based on the target specified now what is happening is that if i braodcast a message and a node receives it and if it is not the target node then it should broadcast it again but this last part is not happening i dont know why please help

this is my transmitter code which is an esp32

#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
#include <ArduinoJson.h>

StaticJsonDocument<128> jsonDoc;
RF24 radio(27, 5);  // CE, CSN pins for ESP32
const byte address[6] = "00001";  // RF Address
const byte address2[6] = "00002";
char responseBuffer[32];

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(address);
  radio.openReadingPipe(1, address2);
  radio.setPALevel(RF24_PA_LOW);
  radio.stopListening();
}

void loop() {
  // Create a JSON packet
  if(radio.available())
  {
    radio.startListening();
    radio.read(&responseBuffer,sizeof(responseBuffer));
    Serial.println(responseBuffer);
  }
  
  Serial.println("Enter your data to be sent");
  while(!Serial.available())
  {
    ;
  }
  String data = Serial.readStringUntil('\n');
  
  jsonDoc["t"] = "esp";  // Target node name
  jsonDoc["d"] = data;

  // Serialize JSON to a char array
  char jsonBuffer[32];
  serializeJson(jsonDoc, jsonBuffer);

  Serial.print("Sending JSON: ");
  Serial.println(jsonBuffer);

  // Send JSON packet
  if (radio.write(&jsonBuffer, sizeof(jsonBuffer))) {
    Serial.println("Message sent successfully");
  } else {
    Serial.println("Message failed to send");
  }
}

and this is my receiver code which is an arduino uno

#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
#include <ArduinoJson.h>

StaticJsonDocument<128> jsonDoc;
StaticJsonDocument<128> responseDoc;

RF24 radio(9, 10);  // CE, CSN pins for Arduino
const byte address[6] = "00001";  // RF Address
const byte address2[6] = "00002";
const char* nodeName = "uno";  // Set the name of this node
const int ledPin = 4;  // LED connected to GPIO 4

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  radio.begin();
  radio.openWritingPipe(address2);
  radio.openReadingPipe(1, address);
  radio.setPALevel(RF24_PA_LOW);
  radio.startListening();
}

void loop() {
  char jsonBuffer[32] = "";

  if (radio.available()) {
    radio.read(&jsonBuffer, sizeof(jsonBuffer));
    Serial.print("Received JSON: ");
    Serial.println(jsonBuffer);

    // Parse JSON
    
    DeserializationError error = deserializeJson(jsonDoc, jsonBuffer);

    if (error) {
      Serial.print("JSON parsing failed: ");
      Serial.println(error.c_str());
      return;
    }

    String target = jsonDoc["t"];
    String message = jsonDoc["d"];

    Serial.print("Target: ");
    Serial.println(target);
    Serial.print("Message: ");
    Serial.println(message);

    // Check if the target matches this node
    if (strcmp(target.c_str(), nodeName) == 0) 
    {
      Serial.println("Target matched! Turning LED ON");
      digitalWrite(ledPin, HIGH);
      //delay(2000);  // Keep LED on for 2 seconds
      //
    } 
    else 
    {
      digitalWrite(ledPin, LOW);
      Serial.println("Target did not match. Broadcasting the data");
      char responseBuffer[32];
      responseDoc["t"] = target;
      responseDoc["d"] = message;

      serializeJson(responseDoc,responseBuffer);
      Serial.println("Sending JSON : ");
      radio.stopListening();
      bool success = radio.write(&responseBuffer,sizeof(responseBuffer));
      radio.startListening();
      
      if(success)
      {
        Serial.println("Broadcasting Success");
      }
      else
      {
        Serial.println("Braodcasting Failed");
      }
      
    }
  }
}

Thanks a lot in advance

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.