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