If it is practical to arrange your system so that the receiver is the "master" and calls to each of the "slaves" in turn it will make the whole thing much simpler. It will mean that there is no doubt which slave sends the data and no risk of two slaves sending at the same time and interfering with each other. The second example in this Simple nRF24L01+ Tutorial can be extended to deal with several slaves.
nah,I want make algorithm like that. but I do not understand how to make it. if I had to make a command sequence that is sent simultaneously to the slave then the slave who has the same status with the command received from the master, the slave will transmit the data to the master. like this program
The following program that I have made, but the received data still did not sequentially
Transmitter
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include <BH1750FVI.h> // BH1750 Sensor Library
#include <Wire.h> // I2C Library
#include <DHT.h> // DHT22 Sensor Library
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
const uint64_t pipe = 0xF0F0F0F0ABLL;
int a;
float hum; //Stores humidity value
float temp; //Stores temperature value
uint16_t lux; //Stores intesity value
BH1750FVI LightSensor;
typedef struct{
float A;
float B;
uint16_t C;
int D;
}A_t;
A_t data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Slave Node 0 Starting...");
LightSensor.begin();
LightSensor.SetAddress(Device_Address_H);//Address 0x5C
LightSensor.SetMode(Continuous_H_resolution_Mode);
dht.begin();
radio.begin();
radio.setDataRate(RF24_250KBPS);
//radio.setDataRate(RF24_1MBPS);
//radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0);
radio.setCRCLength(RF24_CRC_16);
radio.openReadingPipe(1, pipe);
}
void loop() {
// put your main code here, to run repeatedly:
getData();
radio.startListening();
if (radio.available()) {
radio.read(&a,sizeof(a));
Serial.println(a);
if ( a == 1 ) {
send();
a = 0;
}
}
}
void getData() {
lux = LightSensor.GetLightIntensity();// Get Lux value
hum = dht.readHumidity();
temp= dht.readTemperature();
data.A = temp;
data.B = hum;
data.C = lux;
data.D = 0;
}
void send() {
radio.stopListening();
radio.openWritingPipe(pipe);
bool ok = radio.write(&data, sizeof(data));
Serial.println("Status " + String(ok));
}
Receiver
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
const uint64_t pipe[4] = {0x0,0xF0F0F0F0ABLL,0xF0F0F0F0BCLL,0xF0F0F0F0CDLL};
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;
bool newData = false;
int a;
typedef struct{
float A;
float B;
uint16_t C;
int D;
}A_t;
A_t data;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Master Node Starting...");
radio.begin();
radio.setDataRate(RF24_250KBPS);
//radio.setDataRate(RF24_1MBPS);
//radio.setDataRate(RF24_2MBPS);
radio.setPALevel(RF24_PA_MAX);
radio.setChannel(0);
radio.setCRCLength(RF24_CRC_16);
radio.openReadingPipe(1, pipe[1]);
radio.openReadingPipe(2, pipe[2]);
//radio.openReadingPipe(3, pipe[3]);
}
void loop() {
// put your main code here, to run repeatedly:
for (a = 1; a < 3; a++) {
send();
// currentMillis = millis();
// if (currentMillis - prevMillis >= txIntervalMillis) {
getData();
// prevMillis = millis();
// }
}
}
void getData() {
if (radio.available()) {
radio.read(&data,sizeof(data));
showData();
newData = true;
}
else {
Serial.println("RTO");
}
}
void showData() {
Serial.print("Time = ");
Serial.print(round(millis()/1000));
Serial.println(" second, Data from node " + String(data.D));
Serial.print("Temp = ");
Serial.print(data.A);
Serial.print(" C, Hum = ");
Serial.print(data.B);
Serial.print(" %, Lux = ");
Serial.print(data.C);
Serial.println(" lux");
newData = false;
}
void send() {
for (byte n = 1; n < 3; n++) {
radio.stopListening();
radio.openWritingPipe(pipe[n]);
bool rslt;
rslt = radio.write( &a, sizeof(a) );
delay(1000);
radio.startListening();
// Serial.print("Node ");
// Serial.print(n);
// if (rslt) {
// if (n < 3) {
// Serial.print(" Done || ");
// }
// else {
// Serial.println(" Done || ");
// }
// }
// else {
// if (n < 3) {
// Serial.print(" Failed || ");
// }
// else {
// Serial.println(" Failed || ");
// }
// }
}
}