Hello
I am working on NRF24l01 based wireless module to which a pir sensor is connected, As some part of the topic is discussed in previous topic which was to initially to setup and make the code to work for multiple sensors and one single master.
The link is given below
https://forum.arduino.cc/index.php?topic=512027.0
Now i want to make some modification in the code The previous code for master is
// MultiTxAckPayload - the master or the transmitter
// works with two Arduinos as slaves
// each slave should the SimpleRxAckPayload program
// one with the adress {'R','x','A','A','A'}
// and the other with {'R','x','A','A','B'}
#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 dataToSend[10] = "Message 0";
char dataToSend[10] = "ToSlvN 0";
char pirDataReceived[5];
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
//===============
void setup() {
Serial.begin(9600);
Serial.println(F("Source File = /mnt/SGT/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/MultiTxAckPayload.ino "));
Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
// radio.openWritingPipe(slaveAddress); -- moved to loop()
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
// showData(); -- moved into send()
}
//================
void send() {
// call each slave in turn
for (byte n = 0; n < numSlaves; n++){
// open the writing pipe with the address of a slave
radio.openWritingPipe(slaveAddress[n]);
// include the slave number in the message
dataToSend[5] = n + '0';
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print(" ======== For Slave ");
Serial.print(n);
Serial.println(" ========");
Serial.print(" Data Sent ");
Serial.print(dataToSend);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&pirDataReceived, sizeof(pirDataReceived));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
call();
}
else {
Serial.println(" Tx failed");
}
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");
}
}
and for slave is
// 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'};
RF24 radio(CE_PIN, CSN_PIN);
int buttonInput = 2;
int pir1;
char pirData[5]="ON 0";
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 250; // send once per second
char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//==============
void setup() {
Serial.begin(9600);
pinMode(buttonInput,INPUT_PULLUP);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &pirData, sizeof(pirData)); // pre-load data
}
//==========
void loop() {
pir1 = digitalRead(buttonInput);
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
pirTest();
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.println("Data received ");
Serial.println(dataReceived);
Serial.println(" ackPayload sent ");
Serial.println(pirData);
newData = false;
}
}
//================
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
}
i want to somewhat optimize the code and only send data by the slave when there is a trigger by the pir and rest of the time it should go in sleep mode. This will help to reduce power consumption . Please give me some suggestion what should be done in the code