Hello,
I tried Robin2 tutorial example SimpleTxAckPayload for the same single pir sensor i was nable to transfer data from master to slave but couldn’t transfer from slave to master
Following are the sketches
// SimpleTxAckPayload - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char pirDataReceived[5];
char dataToSend[10] = "Message 0";
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/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino"));
Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
showData();
}
//================
void send() {
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("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");
}
prevMillis = millis();
}
//=================
void showData() {
if (newData == true) {
Serial.print(" Acknowledge data ");
Serial.print(pirDataReceived);
newData = false;
}
}
//================
void call()
{
if(strcmp(pirDataReceived,"ON 1")==0)
{
Serial.println("Led On");
}
else if(strcmp(pirDataReceived,"ON 0")==0)
{
Serial.println("Led Off");
}
}
above is the sketch which i tried for master and output i received at master side was
Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino
SimpleTxAckPayload Starting
Data Sent Message 0 Acknowledge but no data
Data Sent Message 0 Acknowledge but no data
Data Sent Message 0 Acknowledge but no data
Data Sent Message 0 Acknowledge but no data
now at the slave side i tried with the following sketch
// 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.writeAckPayload(1, &pirData, sizeof(pirData)); // pre-load data
radio.startListening();
}
//==========
void loop() {
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);
}
radio.writeAckPayload(1, &pirData, sizeof(pirData)); // load the payload for the next time
}
the output i received is
PIR LOW
Data received
Message 0
ackPayload sent
ON 0
PIR LOW
Data received
Message 0
ackPayload sent
ON 0
PIR LOW
Data received
Message 0
ackPayload sent
ON 0
PIR LOW
Data received
Message 0
ackPayload sent
ON 0
which shows that the data from master is received by slave but the opposite is not working
i wanted to know what is going wrong