I have two nRF24L01 modules (I tried 2 with antenna and 2 without antenna) paired with their power base module and connected to two different Arduino Mega 2560 boards. I'm able to send from one module to another up to 32 byte of data and get the acknowledge without any problems. But when I switch them, when I try to send data from receiver and receive it on the sender I have nothing. No data received, no acknowledge sent as response.
As I said before, first part of this code works well, but when it comes to the second part, which is after delay(1000);
there's silence...
my code is:
Receiver
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include "printf.h"
// Set pipes
const uint64_t pipe01_ = 0xE8E8F0F0A1LL;
const uint64_t pipe02_ = 0xE8E8F0F0A2LL;
const uint64_t pipe03_ = 0xA3LL;
const uint64_t pipe04_ = 0xA4LL;
const uint64_t pipe05_ = 0xA5LL;
const uint64_t pipe06_ = 0xA6LL;
RF24 radio(9, 53);
void setup()
{
Serial.begin(115200);
delay(250);
radio.begin();
radio.setChannel(0x57);
radio.setPALevel(RF24_PA_LOW);
radio.enableAckPayload();
radio.openReadingPipe(1, pipe01_);
radio.openWritingPipe(pipe02_);
radio.enableDynamicPayloads();
radio.startListening();
}
void loop()
{
if (radio.available())
{
char newMessage[33];
memset(newMessage, 0, sizeof(byte) * 33);
while (radio.available())
{radio.read(newMessage, 32);}
Serial.println("newMessage: " + String((const char*)newMessage));
delay(1000);
radio.stopListening();
char response[32] = "1234567890qwertyuiopasdfghjklzxc";
radio.write(response, 32);
unsigned long ack = 0;
if (radio.isAckPayloadAvailable())
{
radio.read(&ack, sizeof(ack));
Serial.println("ack: " + String(ack));
}
radio.startListening();
}
}
Sender
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include "printf.h"
// Set pipes
const uint64_t pipe01_ = 0xE8E8F0F0A1LL;
const uint64_t pipe02_ = 0xA2LL;
const uint64_t pipe03_ = 0xA3LL;
const uint64_t pipe04_ = 0xA4LL;
const uint64_t pipe05_ = 0xA5LL;
const uint64_t pipe06_ = 0xA6LL;
RF24 radio(9, 53);
void setup()
{
Serial.begin(115200);
delay(250);
radio.begin();
radio.setChannel(0x57);
radio.setPALevel(RF24_PA_LOW);
radio.enableAckPayload();
radio.openReadingPipe(2, pipe02_);
radio.openWritingPipe(pipe01_);
radio.enableDynamicPayloads();
radio.stopListening();
char message[32] = "qwertyuiopasdfghjklzxc1234567890";
radio.write(message, 32);
radio.startListening();
}
void loop()
{
if (radio.available())
{
char respMessage[33];
memset(respMessage, 0, sizeof(byte) * 33);
while (radio.available())
{radio.read(respMessage, 32);}
Serial.println("respMessage: " + String((const char*)respMessage));
}
}
Please help me.
qwertyuiopasdfghjklzxc1234567890
contains 32 characters plus an implicit null terminator - for which there is no space in the array. It would be safer to declare this aschar message[] = "..."
. You might suffer from corrupted memory here.