Hello,
here is a crazy thing!
I have a master module with an nrf24 which communicates with "slave" modules, the number of which can change and whose code is as follows
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
//NRF24L01
RF24 radio(8, SS); // Micro nRF24L01 (CE,CSN)
char dataToSend = 'm'; //Message envoyé pour collecte datas à chaque capteur
char ackData[32]; //Réponse avec datas des capteurs
uint8_t nbrCapteurs = 0;
const uint8_t numSlaves = 13;
const uint8_t slaveAddress[numSlaves][5] = {
{'R', 'x', 'A', 'A', 'A'},
{'R', 'x', 'A', 'A', 'B'},
{'R', 'x', 'A', 'A', 'C'},
{'R', 'x', 'A', 'A', 'D'},
{'R', 'x', 'A', 'A', 'E'},
{'R', 'x', 'A', 'A', 'F'},
{'R', 'x', 'A', 'A', 'G'},
{'R', 'x', 'A', 'A', 'H'},
{'R', 'x', 'A', 'A', 'I'},
{'R', 'x', 'A', 'A', 'J'},
{'R', 'x', 'A', 'A', 'K'},
{'R', 'x', 'A', 'A', 'L'},
{'R', 'x', 'A', 'A', 'M'}
};
void setup() {
Serial.begin(115200);
SPI.begin(); //Communication RF
delay(50);
radio.begin();
radio.setChannel(120);
radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.enableAckPayload();
radio.setRetries(5, 3); // delay, count
Serial.println("Choisir le nombre de capteurs");
while (nbrCapteurs == 0) {
if (Serial.available()) {
nbrCapteurs = Serial.read() - '0';
}
}
}
void loop() {
for (uint8_t n = 0; n < nbrCapteurs; n++) {
radio.openWritingPipe(slaveAddress[n]);
if (radio.write(&dataToSend, sizeof(dataToSend))) {
if (radio.isAckPayloadAvailable()) {
radio.read(&ackData, sizeof(ackData));
Serial.println(ackData);
}
}
}
}
The code of the "slaves" to whom it requests information
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 10
#define CSN_PIN SS
const uint8_t address[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
char ackData[32]; // the two values to be sent to the master
bool newData = false;
int test;
void setup() {
radio.begin();
radio.setChannel(120);
radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.openReadingPipe(1, address);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
}
void loop() {
getData();
showData();
}
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
updateReplyData();
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
Serial.print(" ackPayload sent ");
Serial.println(ackData);
newData = false;
}
}
//================
void updateReplyData() {
test++;
if (test > 100){
test = 0;
}
sprintf(ackData, "i0/%i/%s/%s/%s", test, "01", "01", "01");
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}
Well it works with 3, 5, 6, etc sensors, but not with 4! : o
I have been desperately looking for a solution for a week.
Any help or suggestion is welcome.
Thank you