I have been working with 2 nrf since last month. It worked fine for a month and now it is not working.
I have tried to send a data to other while the other verify the received data and send a reply.
Here is my code for the transmitter.
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <string.h>
RF24 radio(9, 8); // CE, CSN
const byte addresses[][6] = { "00001", "00002" };
bool sending;
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.setDataRate(RF24_250KBPS);
radio.setChannel(50);
radio.setPALevel(RF24_PA_MAX);
}
void loop() {
// unsigned long start_time = millis();
// while (!radio.available() && millis() - start_time < 200) {
// // Wait for a response for 200ms
// }
radio.startListening();
if (radio.available()) {
char textReceived[32] = { 0 };
radio.read(&textReceived, sizeof(textReceived));
Serial.println(textReceived);
if (strncmp(textReceived, "B1", 2) == 0) {
Serial.println("true");
sending = true;
} else {
sending = false;
}
}
// delay(1000);
const char text[] = "Resb1";
if (sending == true) {
radio.stopListening(); // First, stop listening so we can talk.
radio.write(&text, sizeof(text));
Serial.println("reply sent");
radio.startListening(); // Now, start listening for response
sending = false;
delay(1000);
}
}
Here is my code to verify it and send reply.
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
#include <string.h>
#include <SoftwareSerial.h>
RF24 radio(9, 8); // CE, CSN
SoftwareSerial espSerial(6, 7);
const byte addresses[][6] = { "00001", "00002" };
void setup() {
Serial.begin(115200);
espSerial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setChannel(50);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);
}
void loop() {
// radio.startListening(); // Start listening for messages
const char response[] = "B1";
Serial.println("inloop");
radio.stopListening(); // First, stop listening so we can talk.
radio.write(&response, sizeof(response));
radio.startListening();
if (radio.available()) {
Serial.println("ravil");
// const char response[] = "B1";
// radio.stopListening(); // First, stop listening so we can talk.
// radio.write(&response, sizeof(response));
char textReceived[32] = { 0 };
radio.read(&textReceived, sizeof(textReceived));
Serial.println(textReceived);
if (strncmp(textReceived, "Resb1", 5) == 0) {
Serial.println("inrange");
espSerial.println("B1in");
}else{
espSerial.println("B1not");
}
} else {
Serial.println("B1 not in range");
espSerial.println("B1not");
}
delay(1000);
}
Wiring .
- Connect the NRF GND pin 1 to Arduino Uno GND pin
- Connect the NRF VCC pin 2 to Arduino Uno 3.3V pin
- Connect the NRF CE pin 3 to Arduino Uno digital pin 9
- Connect the NRF CSN pin 4 to Arduino Uno digital pin 8
- Connect the NRF SCK pin 5 to Arduino Uno digital pin 13
- Connect the NRF MOSI pin 6 to Arduino Uno digital pin 11
- Connect the NRF MISO pin 7 to Arduino Uno digital pin 12
- Connect the NRF IRQ pin 8 to Arduino Uno digital pin 10, we will not use.
I also added a 100uf capacitor.
On my serial monitor i get sth like on the picture attached eventhouth the transmitter is on line.
I want to get sth like “B1” , “replysent” on transmitter and “Resb1″ ,”b1inrange” on receiver.
I use a clone and a original uno boards.
Any help should be appreciated.
Gaja harish is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.