Setup
- I have 2 Arduinos... each has two 2.4 GHz nRF24L01+ transceivers (so in total 4).
- Per Arduino, one is only for transmitting, while the other is for receiving (to prevent the need for switching between transmitting and receiving).
- The receiver and transmitter are on different channels (per Arduino, so channel 109 for receiver on arduino 1 and transmitter on arduino 2 and 112 for the other pair).
SPI / Chip Select
Because the nRF24L01+ are SPI devices, I need to use Chip Select, since there is only one SPI bus on an Arduino.
Setup settings
Since I can both receive and send (successfully) I did not add the Setup code (I used slowest speed 250 kbps, 16 bytes payload, no auto ack, no retries, highest power).
Problem
I can transmit/receive from both Arduinos, so all 4 transceivers work correctly. However, what I see in the output (see directly after the logging), is when I send, a lot of messages which should be received are missed. See the log at the end
Also, when I remove the write and replace it by a delay(1) than I miss just occasionally a message, so I think the write is the problem.
Assumption
Is it possible that during the write so many message from the other Arduino are colliding? I would expect a very fast on-air time, and even than, using two different channels should not make messages collide.
So it seems, while calling the Transmit for one RF, the other does not receive somehow. I know the CS pin of the receiver is HIGH (inactive) during transmitting (transmitter CS pin is LOW), but I would assume messages would be received by the RF, and when I set the CS pin of the receiver to LOW (active), that I can read the messages.
Code (of one of the Arduinos)
The code for the other Arduino is almost similar (only opposite port names as transmitter/receiver)
/*
*/
#include <SPI.h>
#include "RF24.h"
static const uint8_t RECEIVER_CE = 5; // Chip Enable
static const uint8_t RECEIVER_CS = 6; // Chip Select
static const uint8_t TRANSMITTER_CE = 7; // Chip Enable
static const uint8_t TRANSMITTER_CS = 8; // Chip Select
RF24 radioReceiver(RECEIVER_CE, RECEIVER_CS);
RF24 radioTransmitter(TRANSMITTER_CE, TRANSMITTER_CS);
uint32_t counter = 0;
static const int PAYLOAD_SIZE = 16;
static const int NR_OF_LOG_ITEMS = 10;
static const int LOG_LENGTH = 25;
uint32_t logData[LOG_LENGTH][NR_OF_LOG_ITEMS];
uint8_t currentLogLine = 0;
void setup()
{
for (int m = 0; m < LOG_LENGTH; m++)
{
for (int n = 0; n < NR_OF_LOG_ITEMS; n++)
{
logData[m][n] = -1;
}
}
pinMode(RECEIVER_CE , OUTPUT);
pinMode(RECEIVER_CS , OUTPUT);
pinMode(TRANSMITTER_CE, OUTPUT);
pinMode(TRANSMITTER_CS, OUTPUT);
digitalWrite(RECEIVER_CE , HIGH);
digitalWrite(TRANSMITTER_CE, HIGH);
Serial.begin(250000);
Serial.println(F("CONTROLLER"));
digitalWrite(RECEIVER_CS , LOW); // Select
digitalWrite(TRANSMITTER_CS, HIGH); // Deselect
radioReceiver.begin();
radioReceiver.setPALevel(RF24_PA_MAX);
radioReceiver.setChannel(109);
radioReceiver.setDataRate(RF24_250KBPS);
radioReceiver.setRetries(0, 0);
radioReceiver.setAutoAck(false);
//radioReceiver.enableAckPayload();
radioReceiver.setPayloadSize(PAYLOAD_SIZE);
//radioReceiver.enableDynamicPayloads();
//radioReceiver.disableCRC();
radioReceiver.setCRCLength(RF24_CRC_8);
digitalWrite(RECEIVER_CS , HIGH); // Deselect
digitalWrite(TRANSMITTER_CS, LOW); // Select
radioTransmitter.begin();
radioTransmitter.setPALevel(RF24_PA_MAX);
radioTransmitter.setChannel(112);
radioTransmitter.setDataRate(RF24_250KBPS);
radioTransmitter.setRetries(0, 0);
radioTransmitter.setAutoAck(false);
//radioTransmitter.enableAckPayload();
radioTransmitter.setPayloadSize(PAYLOAD_SIZE);
//radioTransmitter.enableDynamicPayloads();
radioTransmitter.disableCRC();
radioTransmitter.setCRCLength(RF24_CRC_8);
byte addresses[][6] = {"Read ", "Write"}; // TODO: Change name to slave/contr?
radioReceiver.openReadingPipe(0, addresses[1]);
radioTransmitter.openWritingPipe(addresses[0]);
radioReceiver.startListening();
radioTransmitter.stopListening();
//radio.writeAckPayload(1, &counter, PAYLOAD_SIZE);
//radio.printDetails();
}
uint8_t receiverBuffer [PAYLOAD_SIZE];
uint8_t transmitterBuffer[PAYLOAD_SIZE];
uint32_t prevResponse = -1;
uint32_t response;
void loop(void)
{
// Transmit.
//Serial.print("Sending: ");
//Serial.println(counter);
transmitterBuffer[0] = counter >> 24;
transmitterBuffer[1] = (counter & 0x00FFFFFF) >> 16;
transmitterBuffer[2] = (counter & 0x0000FF00) >> 8;
transmitterBuffer[3] = (counter & 0x000000FF);
digitalWrite(TRANSMITTER_CS, LOW); // Select
digitalWrite(RECEIVER_CS , HIGH); // Deselect
//if (!radioTransmitter.write(transmitterBuffer, PAYLOAD_SIZE))
{
//Serial.println(F("Sending failed."));
}
delay(10); // Fake write
// Receive.
byte pipeNo = 0;
digitalWrite(TRANSMITTER_CS, HIGH); // Deselect
digitalWrite(RECEIVER_CS , LOW); // Select
while ( radioReceiver.available(&pipeNo))
{
radioReceiver.read(receiverBuffer, PAYLOAD_SIZE);
response = (receiverBuffer[0] << 24) +
(receiverBuffer[1] << 16) +
(receiverBuffer[2] << 8) +
(receiverBuffer[3]);
Serial.print("Received: ");
Serial.print(response);
if (prevResponse == -1)
{
prevResponse = response - 1;
}
else
{
if (prevResponse + 1 != response)
{
Serial.print(" MISSED ");
Serial.print(response - prevResponse - 1);
}
prevResponse = response;
}
Serial.println("");
}
// Logging
currentLogLine++;
counter++;
delay(10); // Try again later
}
Log
Received: 10261 MISSED 15
Received: 10262
Received: 10263
Sending: 245
Received: 10278 MISSED 14
Received: 10279
Received: 10280
Sending: 246
Received: 10296 MISSED 15
Received: 10298 MISSED 1
Received: 10299
Sending: 247