Hello,
Transmitting data from multiple slaves to master, and making the master as receiver only i tried following code but didn't get any result
For slave:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
int buttonInput = 2;
int pir1;
RF24 radio(CE_PIN, CSN_PIN);
char pirData[5]="ON 0";
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
pinMode(buttonInput,INPUT_PULLUP);
Serial.println("SimpleSleepMode Slave");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.startListening();
radio.writeAckPayload(1, &pirData, sizeof(pirData));
}
//=============
void loop() {
pir1 = digitalRead(buttonInput);
pirTest();
}
//==============
void pirTest()
{
if(pir1 == HIGH)
{
Serial.println("PIR 1");
strncpy(pirData, "ON 1", 5);
}
else if(pir1 == LOW)
{
Serial.println("PIR LOW");
strncpy(pirData, "ON 0", 5);
}
delayMicroseconds(500);
radio.writeAckPayload(1, &pirData, sizeof(pirData)); // load the payload for the next time
}
and for master
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte numSlaves = 2;
const byte slaveAddress[numSlaves][5] = {
// each slave needs a different address
{'R','x','A','A','A'},
{'R','x','A','A','B'}
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char pirDataReceived[5];
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 250; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SleepMode Master");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
// radio.openWritingPipe(slaveAddress);
radio.openReadingPipe(1,slaveAddress);
radio.startListening();
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
// call each slave in turn
for (byte n = 0; n < numSlaves; n++){
radio.read(&pirDataReceived, sizeof(pirDataReceived));
newData = true;
call();
showData();
Serial.print("\n");
}
prevMillis = millis();
}
//================
//=================
void showData() {
if (newData == true) {
Serial.println(" Acknowledge data ");
Serial.println(pirDataReceived);
newData = false;
}
}
void call()
{
if(strcmp(pirDataReceived,"ON 1")==0)
{
Serial.println("Led 1On");
}
else if(strcmp(pirDataReceived,"ON 2")==0)
{
Serial.println("Led 2On");
}
else if(strcmp(pirDataReceived,"ON 0")==0)
{
Serial.println("Led Off");
}
}
the transfer of data is not successful. what can be the possible reason?