Seperating data from nRF24L01 received 2

I want to have a couple of sensors in my project. They communicate using nRF24L01. So my issue was that I received the value's of my sensor, but I can't figure out how to separate them, both values are in there but I can't find a way to separate them and put them in a separate variable/float.
Transmitter

#include <NewPing.h>
#include "SPI.h"
#include "DHT.h"
#define TRIGGER_PIN  5
#define ECHO_PIN     6
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define DHTPIN 2     

#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
RF24 radio(9, 10);

const byte adress[6] = "00001";
#define DHTTYPE DHT11  

#include <SparkFun_LPS25HB_Arduino_Library.h>

LPS25HB pressureSensor;

DHT dht(DHTPIN, DHTTYPE);

#include "Wire.h"
#include <MPU6050_light.h>

MPU6050 mpu(Wire);

long timer = 0;

struct dataStruct {


float temperature;
float humidity;
float Heatindex;
float pressure;

float distance;
float Accx;
float Accy;
float Accz;

float gyrox;
float gyroy;
float gyroz;

float ivmeacix;
float ivmeaciy;

float acix;
float aciy;
float aciz;

}sensorInput;


void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));

  dht.begin();

  radio.begin();
  radio.openWritingPipe(adress);
  radio.setPALevel(RF24_PA_MAX); 

  radio.stopListening();
  
 Wire.begin();
  
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } 
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(true,true); // gyro and accelero
  Serial.println("Done!\n");

  
while (!Serial)
  {
  }
  Serial.println("LPS25HB Pressure Sensor Example 2 - Configuring I2C Past Default");
  Serial.println();

  Wire.begin();
  Wire.setClock(400000);


  pressureSensor.begin(Wire, LPS25HB_I2C_ADDR_DEF); 
                                                                         
                                                  
  if (pressureSensor.isConnected() == false)
  {
    Serial.println("LPS25HB not found with your settings: ");
    Serial.println("Troubleshooting: ");
    Serial.println("  Ensure the correct I2C address is used (in begin() function)");
    Serial.println("  Ensure the correct Wire port is used (in begin() function)");
    Serial.println("  Ensure the sensor is connected correctly");
    Serial.println("Reset the board or re-upload to try again.");
    Serial.println("");
    while (1)
      ;
  }
  }
void loop() {
  
  delay(2000);

 
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);


 sensorInput.temperature = dht.readTemperature();
  sensorInput.humidity =dht.readHumidity();
   sensorInput.Heatindex = dht.computeHeatIndex (); 
sensorInput.pressure = pressureSensor.getPressure_hPa();
sensorInput.Accx = mpu.getAccX();
sensorInput.Accy = mpu.getAccY();
sensorInput.Accz = mpu.getAccZ();
sensorInput.gyrox = mpu.getGyroX();
sensorInput.gyroy = mpu.getGyroY();
sensorInput.gyroz = mpu.getGyroZ();
sensorInput.ivmeacix = mpu.getAccAngleX();
sensorInput.ivmeaciy = mpu.getAccAngleY();
sensorInput.acix = mpu.getAngleX();
sensorInput.aciy = mpu.getAngleY();
sensorInput.aciy = mpu.getAngleZ();








   
  radio.write(&sensorInput, sizeof(sensorInput));
 
  

  delay(1000);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  
  float hif = dht.computeHeatIndex(f, h);
  
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("째C "));
  Serial.print(f);
  Serial.print(F("째F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("째C "));
  Serial.print(hif);
  Serial.println(F("째F"));

  printResults(sonar.ping());
}
void printResults(unsigned int ping) {
  Serial.print("Distance: ");
  Serial.print(sonar.convert_cm(ping));
  Serial.println("cm");
   Serial.print("Pressure (hPa): ");
  Serial.print(pressureSensor.getPressure_hPa());
  Serial.print(", Temperature (degC): ");
  Serial.println(pressureSensor.getTemperature_degC()); 

  delay(40);

  mpu.update();

  if(millis() - timer > 1000){ 
    Serial.print(F("TEMPERATURE: "));Serial.println(mpu.getTemp());
    Serial.print(F("ACCELERO  X: "));Serial.print(mpu.getAccX());
    Serial.print("\tY: ");Serial.print(mpu.getAccY());
    Serial.print("\tZ: ");Serial.println(mpu.getAccZ());
  
    Serial.print(F("GYRO      X: "));Serial.print(mpu.getGyroX());
    Serial.print("\tY: ");Serial.print(mpu.getGyroY());
    Serial.print("\tZ: ");Serial.println(mpu.getGyroZ());
  
    Serial.print(F("ACC ANGLE X: "));Serial.print(mpu.getAccAngleX());
    Serial.print("\tY: ");Serial.println(mpu.getAccAngleY());
    
    Serial.print(F("ANGLE     X: "));Serial.print(mpu.getAngleX());
    Serial.print("\tY: ");Serial.print(mpu.getAngleY());
    Serial.print("\tZ: ");Serial.println(mpu.getAngleZ());
    Serial.println(F("=====================================================\n"));
    timer = millis();
  }
}

Receiver

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

#include <RF24.h>
#include <RF24_config.h>

RF24 radio(9, 10);

const byte adress[6] = "00001";

struct dataStruct {


float temperature;
float humidity;
float Heatindex;
float pressure;

float distance;
float Accx;
float Accy;
float Accz;

float gyrox;
float gyroy;
float gyroz;

float ivmeacix;
float ivmeaciy;

float acix;
float aciy;
float aciz;

}sensorInput;

void setup() {
Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, adress);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
  if (radio.available()) 
  {
radio.read(&sensorInput, sizeof(sensorInput));
Serial.print("Humidity: "); Serial.println(sensorInput.humidity);
   

  
  Serial.print(F("%  Temperature: "));Serial.println(sensorInput.temperature);
  Serial.println(F("째C "));
  
  Serial.print(F("Heatindex"));
  Serial.println(sensorInput.temperature);
  Serial.println(F("째C "));
  


    Serial.print(F("ACCELERO  X: "));Serial.println(sensorInput.Accx);
    Serial.print("\tY: ");Serial.print(sensorInput.Accy);
    Serial.print("\tZ: ");Serial.println(sensorInput.Accz);
  
    Serial.print(F("GYRO      X: "));Serial.print(sensorInput.gyrox);
    Serial.print("\tY: ");Serial.print(sensorInput.gyroy);
    Serial.print("\tZ: ");Serial.println(sensorInput.gyroz);
  
    Serial.print(F("ACC ANGLE X: "));Serial.print(sensorInput.ivmeacix);
    Serial.print("\tY: ");Serial.println(sensorInput.ivmeaciy);
    
    Serial.print(F("ANGLE     X: "));Serial.print(sensorInput.acix);
    Serial.print("\tY: ");Serial.print(sensorInput.aciy);
    Serial.print("\tZ: ");Serial.println(sensorInput.aciz);
    Serial.println(F("=====================================================\n"));

  
  }
}
1 Like

Unless you exceed the 32 byte buffer size, the struct you send will be the struct you receive. What is the problem?

I can't see any data on the terminal screen on the receiver.

Neither can I !

The transmit side sensorInput struct is 16 floats which is way more than the 32 byte limit on packet size.

Did you get the radios to communicate using examples from the RF24 library before writing all that code.?

Robin2's simple rf24 tutorial helped me get my radios to work. His example code is simple and is known to work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.