NRF24L01 issue, please check my code.

Hi,

Please someone can debug my code? I am stuck on my simple working code which is stoped working now. I use this library: GitHub - maniacbug/RF24: Arduino driver for nRF24L01 This is NRF24L01 Recevier and the next one is transmitter:

// http://www.bajdi.com
// Sending a struct with the nRF24L01 module
// Data to be sent is the reading of 2 analog pins
// Data received is the analog reading of 2 pins on the other Arduino 

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

typedef struct{
  float humd;
  float temp;
  float pres_T;
  float pres_P;
  float volt;
}B_t;

B_t server;

RF24 radio(6,5);   // uint8_t _cepin, uint8_t _cspin
const uint64_t pipe  = 0xE8E8F0F0E1LL;

void setup()
{
  Serial.begin(9600);
  
  radio.begin();
  radio.openReadingPipe(0,pipe);
  radio.startListening();
  //radio.printDetails();
}

void loop(void) { 
  if( radio.available()) {
    bool done = false;
    while (!done) {
      radio.read( &server, sizeof(server) );
    }
  }

  // serial print received data
  Serial.print("server.humd = ");
  Serial.println(server.humd);
  Serial.print("server.temp = ");
  Serial.println(server.temp);
  Serial.print("server.volt = ");
  Serial.println(server.volt);
  delay(5000);
}
#include <Wire.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "DHT.h"
#include <SFE_BMP180.h>

RF24 radio(6,5); // ce,csn pins
#define DHTPIN 2 
#define DHTTYPE DHT22
#define ALTITUDE -22.81 // SETTINGS MENU ITEM
DHT dht(DHTPIN, DHTTYPE);
SFE_BMP180 pressure;


const uint64_t pipe = 0xE8E8F0F0E1LL;

typedef struct{
  float humd;
  float temp;
  float pres_T;
  float pres_P;
  float volt;
}B_t;

B_t server;

float readVcc() {
  //long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(4); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  long result = ADCL;
  result |= ADCH<<8;
  result =  (1126400L / result) / 1000; // Back-calculate AVcc in mV
  return result;
}

void setup(void) {
  Serial.begin(9600);
  
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipe);
  radio.stopListening();
  radio.printDetails();
  
  dht.begin();
  pressure.begin(); 
}

void loop(void) {
    char status;
    double T,P,p0;
    
    server.volt = readVcc();
    server.humd = dht.readHumidity(); // Humidity DHT22
    server.temp = dht.readTemperature();  // Temperature DHT22
    
    status = pressure.startTemperature();
    if (status != 0) {
      delay(status);

      status = pressure.getTemperature(T);
      if (status != 0) {
        server.pres_T = (T,2);  // Temperature BMP180
        status = pressure.startPressure(3);
        if (status != 0) {
          delay(status);
          status = pressure.getPressure(P,T);
          if (status != 0) {
            server.pres_P = P;  // Pressure BMP180
          }
        }
      }
    }
    
    Serial.println(server.humd);
    Serial.println(server.temp);
    Serial.println(server.pres_T);
    Serial.println(server.pres_P);
    Serial.println(server.volt,2);
    
    radio.write(&server, sizeof(server));
    
    
    delay(5000);
}
void loop(void) {
  if( radio.available()) {
    bool done = false;
    while (!done) {
      radio.read( &server, sizeof(server) );
    }
  }
...

This while loop ends when “done” is true. When “done” will be true? I can’t find it in your code, so it will loop forever, reading from NRF24, even if there isn’t data to read.

Try removing the while() and “done”.

Here is the whole code for receiver:

// http://www.bajdi.com
// Sending a struct with the nRF24L01 module
// Data to be sent is the reading of 2 analog pins
// Data received is the analog reading of 2 pins on the other Arduino

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

typedef struct{
  float humd;
  float temp;
  float pres_T;
  float pres_P;
  float volt;
}B_t;

B_t server;

RF24 radio(6,5);   // uint8_t _cepin, uint8_t _cspin
const uint64_t pipe  = 0xE8E8F0F0E1LL;

void setup()
{
  Serial.begin(9600);
 
  radio.begin();
  radio.openReadingPipe(0,pipe);
  radio.startListening();
  //radio.printDetails();
}

void loop(void) {
  if( radio.available()) {
      radio.read( &server, sizeof(server) );
  }

  // serial print received data
  Serial.print("server.humd = ");
  Serial.println(server.humd);
  Serial.print("server.temp = ");
  Serial.println(server.temp);
  Serial.print("server.volt = ");
  Serial.println(server.volt);
  delay(5000);
}

Start with something more simple.

I got my NRF24's to work following this tutorial. I suggest you get that working first and then extend it to take in your own requirements.

...R

@giova014 code works now :slight_smile: Actually I have tried to do something like "done = radio.read( &server, sizeof(server) );" which will give true but i have missed that part :S

Currntly the problem is very organic. I cant get data from other NRF unit. I see that tranmitter works OK as it is not fails on transmitting data but receiver fails on catching it. I am debugin it with this smal modification:
if( radio.available()) {
if(radio.read( &server, sizeof(server) )) {
Serial.print("OK");
}
}

Same done with radio.write function too and it returns true but radio.read always fails. Is there are any way that I can detailed debug it. I am sure that wiering is OK the only changes is I use CE, CSN pins as 6, 7.

Actually, the "RF24.h" read() function return is of void type, sou you can't test it for true or false.

What you can do is reset the variable and then print it after receiving the data:

  if(radio.available()){
	memset(data,0,sizeof(data));
        radio.read(&data,sizeof(data));
        Serial.print(" Received data: ");
	Serial.print(server.humd);
	Serial.print(",");
        Serial.print(server.temp);
	Serial.print(",");
	Serial.print(server.pres_T);
	Serial.print(",");
	Serial.print(server.pres_P);
	Serial.print(",");
	Serial.print(server.volt);
  }

If anything is printed as zero, there is a problem.

Nope, server is not array, but struct in my code. I am getting following error.

error: cannot convert 'B_t' to 'void*' for argument '1' to 'void* memset(void*, int, size_t)'
cannot convert 'B_t' to 'void*' for argument '1' to 'void* memset(void*, int, size_t)'

Just remove the memset and see what happens!

Yes test so too. nothing received by transmitter. It loops every sec but shows only zeros :S
also radio.printDetails(); function not works too. I have updated my code, please see below:

Receiver

// http://www.bajdi.com
// Sending a struct with the nRF24L01 module
// Data to be sent is the reading of 2 analog pins
// Data received is the analog reading of 2 pins on the other Arduino 

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

typedef struct{
  float humd;
  float temp;
  char pres_T;
  char pres_P;
  long volt;
}B_t;

B_t server;

RF24 radio(9,10);   // CE_PIN, CSN_PIN
const uint64_t pipe  = 0xE8E8F0F0E1LL;

void setup()
{
  Serial.begin(9600);
  
  radio.begin();
  radio.openReadingPipe(0,pipe);
  radio.startListening();
  radio.printDetails();
}

void loop(void) { 
  if( radio.available()) {

    bool done = false;
    while(!done) {
      done = radio.read(&server, sizeof(server));
    }

  }

    // serial print received data
    Serial.print("server.humd = ");
    Serial.println(server.humd);
    Serial.print("server.temp = ");
    Serial.println(server.temp);
    Serial.print("server.volt = ");
    Serial.println(server.volt);
  delay(5000);
}

Transmitter

#include <Wire.h>
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "DHT.h"
#include "TSL2561.h"
#include <SFE_BMP180.h>

RF24 radio(9,10); // ce,csn pins
#define DHTPIN 2 
#define DHTTYPE DHT22
#define ALTITUDE -22.81 // SETTINGS MENU ITEM
DHT dht(DHTPIN, DHTTYPE);
SFE_BMP180 pressure;
TSL2561 tsl(TSL2561_ADDR_FLOAT);

const uint64_t pipe = 0xE8E8F0F0E1LL;

typedef struct{
  float   humd;
  float   temp;
  uint32_t lum;
  char    pres_T;
  char    pres_P;
  long    volt;
}B_t;

B_t server;

long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(4); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

void setup(void) {
  Serial.begin(9600);
  
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(pipe);
  radio.stopListening();
  radio.printDetails();
  
  dht.begin();
  pressure.begin();
  tsl.begin();
  tsl.setGain(TSL2561_GAIN_16X);
  tsl.setTiming(TSL2561_INTEGRATIONTIME_101MS);
}

void loop(void) {
    char status;
    double T,P,p0;
    uint16_t ir, full;
    
    server.volt   = readVcc();
    server.lum    = tsl.getFullLuminosity(); // Lumonisity
    server.humd   = dht.readHumidity(); // Humidity DHT22
    server.temp   = dht.readTemperature();  // Temperature DHT22
    
    status = pressure.startTemperature();
    if (status != 0) {
      delay(status);

      status = pressure.getTemperature(T);
      if (status != 0) {
        server.pres_T = (T,2);  // Temperature BMP180
        status = pressure.startPressure(3);
        if (status != 0) {
          delay(status);
          status = pressure.getPressure(P,T);
          if (status != 0) {
            server.pres_P = P;  // Pressure BMP180
          }
        }
      }
    }

    
    
    Serial.println(server.humd);
    Serial.println(server.temp);
    Serial.println(server.pres_T);
    Serial.println(server.pres_P);
    Serial.println(server.volt);

    // Send Data
    radio.write(&server, sizeof(server));
    
    
    delay(5000);
}

In the receiver change from

radio.openReadingPipe(0,pipe);

To

radio.openReadingPipe(1,pipe);

In order to the printDetails work, you need to include:

#include <printf.h>

And add in your setup():

printf_begin();

Test again.

@trecords, do you have any code that works to transmit and receive data using your NRF24s?

...R