Nrf24l01 library error

Hello,

I just started working with nrf24l01 wireless module. I wanted to send a pir sensor data wireless module using nrf24l01 so i searched a bit i found a library which works on the same project

http://blog.theinventorhouse.org/usando-nrf24l01-y-sensor-pir-con-arduino/

The above website is in spanish. i downloaded the necessary library’s mentioned by the person which are given at http://playground.arduino.cc/InterfacingWithHardware/Nrf24L01

The issue i am facing is i couldn’t add the library to the ide which are given in the arduino playground. i also searched for other library’s they were added immediately.

The error i am getting is

Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\JD\Desktop\WirelessPir\arduino-nrf24l01-master\arduino-nrf24l01-master\Mirf\examples\attiny84_mirf\attiny84_mirf.ino:27:19: fatal error: SPI85.h: No such file or directory

 #include <SPI85.h>

                   ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Please explain exactly what you mean by "couldn't add the library to the ide".

when i try to add the zip folder of library i get following error

Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\JD\Desktop\WirelessPir\arduino-nrf24l01-master\arduino-nrf24l01-master\Mirf\examples\attiny84_mirf\attiny84_mirf.ino:27:19: fatal error: SPI85.h: No such file or directory

 #include <SPI85.h>

                   ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
Invalid library found in C:\Program Files\Arduino\libraries\arduino-nrf24l01-master: C:\Program Files\Arduino\libraries\arduino-nrf24l01-master
Invalid library found in C:\Program Files\Arduino\libraries\arduino-tasks-master: C:\Program Files\Arduino\libraries\arduino-tasks-master
Invalid library found in C:\Program Files\Arduino\libraries\ArduLibrariesmaster: C:\Program Files\Arduino\libraries\ArduLibrariesmaster
Invalid library found in C:\Program Files\Arduino\libraries\SoftwareSerial-master: C:\Program Files\Arduino\libraries\SoftwareSerial-master
Invalid library found in C:\Program Files\Arduino\libraries\TimedAction-1_6: C:\Program Files\Arduino\libraries\TimedAction-1_6
Invalid library found in C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master: C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master
Specified folder/zip file does not contain a valid library
Invalid library found in C:\Program Files\Arduino\libraries\arduino-nrf24l01-master: C:\Program Files\Arduino\libraries\arduino-nrf24l01-master
Invalid library found in C:\Program Files\Arduino\libraries\arduino-tasks-master: C:\Program Files\Arduino\libraries\arduino-tasks-master
Invalid library found in C:\Program Files\Arduino\libraries\ArduLibrariesmaster: C:\Program Files\Arduino\libraries\ArduLibrariesmaster
Invalid library found in C:\Program Files\Arduino\libraries\SoftwareSerial-master: C:\Program Files\Arduino\libraries\SoftwareSerial-master
Invalid library found in C:\Program Files\Arduino\libraries\TimedAction-1_6: C:\Program Files\Arduino\libraries\TimedAction-1_6
Invalid library found in C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master: C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master
Invalid library found in C:\Program Files\Arduino\libraries\arduino-nrf24l01-master: C:\Program Files\Arduino\libraries\arduino-nrf24l01-master
Invalid library found in C:\Program Files\Arduino\libraries\arduino-tasks-master: C:\Program Files\Arduino\libraries\arduino-tasks-master
Invalid library found in C:\Program Files\Arduino\libraries\ArduLibrariesmaster: C:\Program Files\Arduino\libraries\ArduLibrariesmaster
Invalid library found in C:\Program Files\Arduino\libraries\SoftwareSerial-master: C:\Program Files\Arduino\libraries\SoftwareSerial-master
Invalid library found in C:\Program Files\Arduino\libraries\TimedAction-1_6: C:\Program Files\Arduino\libraries\TimedAction-1_6
Invalid library found in C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master: C:\Program Files\Arduino\hardware\arduino\avr\libraries\SoftwareSerial-master

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

i extracted the zip folder and added it in library folder of arduino still it doesn’t show anything

Have a look at this Simple nRF24L01+ Tutorial. The code works and the library it uses is specified in the tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

...R

You’re compiling for Mega. Looking at the file path, you seem to be compiling ATtiny code. I think that SPI85 is specifically for the attiny.

See what happens when you change SPI85 to SPI in your source code.

Thanks Robin2 for the tutorial it helps quite for beginner for understanding of nrf24l01, i tried all of your examples and all works quite well. i just now want to hook up a pir sensor to slave and take data on master side. it is quite difficult though understanding the wireless communication using this module.

sterretje sure will try what you have suggested

JairajDange:
i just now want to hook up a pir sensor to slave and take data on master side.

Start with one of my examples, get it working, then add code to it to collect the sensor data and have it transmitted.

I'm assuming you will have figured out in another separate program how to collect the sensor data.

...R

Hello,

I tried Robin2 example no 1 which is given in the tutorial .
for pir sensor the below code is for master and the pir is connected to the pro mini which the nrf24l01 connected

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


#define CE_PIN   9
#define CSN_PIN 10

int buttonInput = 2;
int pir1;

const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char pirData[5]="ON 0";



unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 250; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");
    pinMode(buttonInput,INPUT_PULLUP);
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {

    pir1=digitalRead(buttonInput);
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
}

//====================

void send() {

   
    bool rslt1;
   
        // 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
  pirTest();
  rslt1 = radio.write( &pirData, sizeof(pirData) );
    
    
    
    if (rslt1) {
     
        Serial.println("  Acknowledge received");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }
}

//================

void updateMessage() {
        // so you can see that new data is being sent
  
}



void pirTest()
{
if(pir1 == HIGH)
{
  Serial.println("PIR 1");
  
pirData[5] ="ON 1";
}

else if(pir1 == LOW)
{
    Serial.println("PIR LOW");
   
pirData[5] = "ON 0";
}
}

the below code is for slave that is the receiver which is mega

// 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);
char pirDataReceived[5];
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        
        radio.read( &pirDataReceived, sizeof(pirDataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        
       Serial.println("Pir data ");
       Serial.println(pirDataReceived);
        
        
        newData = false;
          
       
        
    }
}

the data is getting transferred but the output is not what i am expecting

i wanted output to be when pir== HIGH the data transfer should be “ON 1”

and when pir==LOW the data transfer should be “ON 0”

but i am receiving output “ON 0” in both cases of state of PIR

pirData[5] ="ON 1";

That makes the sixth element (char) of a 5 element array = to "ON 1". Array elements are numbered from 0, so a 5 element array has elements numbered 0 to 4. And "ON 1" is 4 characters and will not fit in a 1 character (array element) space. And the memory that you are sending to does not belong to the array.

To change the content of pirData, use strncpy. The n (size of your array) in strncpy will prevent copying past the end of the array.

Like this:

char pirData[5]="ON 0";

void setup()
{
Serial.begin(115200);
Serial.println(pirData);
strncpy(pirData, "ON 1", 5);
Serial.println(pirData);
}

void loop()
{

}

groundFungus it does work but the changes i had to make is use the strncpy . not in the setup but in the function

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);
}
}

The output i am receiving is what i wanted when pir1 == HIGH the output is “ON 1” and pir1 == LOW the output is “ON 0”

now the problem is i have added a function to do certain task after receiving the data at the slave side it is not working

// 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);
char pirDataReceived[5];
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        
        radio.read( &pirDataReceived, sizeof(pirDataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        
       Serial.println("Pir data ");
       Serial.println(pirDataReceived);
        
        call();
        newData = false;
          
       
        
    }
}


void call()
{

  if(pirDataReceived=="ON 1")
  {
    Serial.println("Led On");
  }
  else if(pirDataReceived=="ON 0")
  {
    Serial.println("Led Off");
  }
}

i am getting ON 1 and ON 0 respt depending on the pir state but i wanted to do a certain task after getting the output which is in the call() function

You can't do this with cstrings

  if(pirDataReceived=="ON 1")

When using Cstrings you must use strcmp() to compare values rather than ==

...R

Thanks Robin2 it does work, next thing i should do is to use MultiTxAckPayload.ino from your tutorial to do with multiple nrf modules or is their a way to keep a single slave and use multiple masters as i wanted to work with multiple pir sensors as transmitter and and a single receiver.

JairajDange:
or is their a way to keep a single slave and use multiple masters

Please describe what you want to do. It may be that you just need to reverse your notion of what is master and what is slave.

...R

I want to add multiple PIR sensors to multiple nrf24l01 module and make a single receiver for it. as in the one way transmission example the master sends the data and slave receives it. i thought of making a single receiver and multiple senders.

JairajDange:
I want to add multiple PIR sensors to multiple nrf24l01 module and make a single receiver for it.

The simplest option would be for the master to poll the slaves in turn. However things will be more complex if you want the Arduinos at the sensors to spend most of their time asleep to save energy.

You can probably see that you have not provided enough information about how you want your project to work.

...R

Robin2:
However things will be more complex if you want the Arduinos at the sensors to spend most of their time asleep to save energy.

...R

I didn't took power in consideration before staring the project, i have used pro mini 3.3v version attached to the pir sensor as it is low power consuming.

Robin2:
The simplest option would be for the master to poll the slaves in turn.

yes i also thought of same thing to keep a master and let slave send data one after another to the master

Robin2:
You can probably see that you have not provided enough information about how you want your project to work.

...R

I want to create a wireless pir sensor network using nrf24l01 keeping one control unit which would be a central receiver and pir sensors on the transmitter side, i just started with a single sensor now was thinking of making it to multiple sensors

JairajDange:
I want to create a wireless pir sensor network using nrf24l01 keeping one control unit which would be a central receiver and pir sensors on the transmitter side, i just started with a single sensor now was thinking of making it to multiple sensors

That is just a rehash of your earlier information. It does not really add anything.

My suggestion is that you start with a master and multiple slaves using the Multi example from my tutorial and see how you get on. It will probably do all that you require and if not you will at least have a more specific knowledge of what is required.

...R

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

JairajDange:
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

Did you try my example without any changes and did it work?

If you did not, or if it did not work then get it working before you try anything more complex.

I wonder if your strncpy() code is correct I can't recall if you should copy 4 chars or 5 (including the terminating 0).

...R

Robin2:
Did you try my example without any changes and did it work?

yes i tried your example for SimpleTxAckPayload i have mentioned it in the previous reply it wasn't working until..

i started testing each bit of the sketch and when i placed a command in slave sketch

Serial.println("Write Ack payload check");
radio.writeAckPayload(1, &pirData, sizeof(pirData)); // load the payload for the next time

the sketch started transfering and receiving data from slave to master and vice versa

i couldn't understand why did this happen it just started working

if i remove the code "Serial.println("Write Ack payload check");" the slave doesn;t transfer data to the master