Need help for nrf24L01 multiple transmitter

Good day i cant add another transmitter to my reciever:
this the basic code for my reciever

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
//------------------------------------------
RF24 radio(9, 10);
//------------------------------------------

const uint64_t pipe1 = 0xF0F0F0F066;
const uint64_t pipe2 = 0xF0F0F0F062;

//------------------------------------------
struct sensor
{
  float temp;
  int   hum;
  byte  sensorNum;
};

sensor sensorData;

struct sensor2
{
  float temp;
  float hum;
  
};
sensor2 sensordata2;
//===============================================================
void setup()
{
  Serial.begin(9600);
  radio.printDetails();
  radio.begin();
  radio.openReadingPipe(2, pipe2);
  radio.openReadingPipe(1, pipe1);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.startListening();
}
//===============================================================
void loop()
{
  if(radio.available()){
    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Sector 1 ");
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);}
    }
    radio.read(&sensordata2, sizeof(sensor2));
    {
    Serial.println("Sector 2");
    Serial.println("Temperature ");
    Serial.println(sensordata2.temp);
    Serial.println("Humidity ");
    Serial.println(sensordata2.hum);
    }
  delay(2000);

  
  
}

this the code for my second transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <DHT.h>
//----------------------------------
RF24 radio(9, 10); //CN and CSN  pins of nrf
DHT dht(2, DHT22);
//----------------------------------
const uint64_t pipe2 = 0xF0F0F0F062;
//----------------------------------
struct sensor2
{
  float temp;
  float hum;
  
};
sensor2 sensordata2;
//=============================================
void setup()
{
  Serial.begin(9600);
  dht.begin();
  radio.begin();
  radio.openWritingPipe(pipe2);
  radio.setPALevel(RF24_PA_LOW);
  radio.setDataRate(RF24_250KBPS);
  radio.stopListening();
  //------------------------------

}
//=============================================
void loop()
{
  delay(2000);
  sensordata2.temp = dht.readTemperature();
  sensordata2.hum = dht.readHumidity();
  Serial.println("Humidity: ");
  Serial.println(sensordata2.hum);
  Serial.println("Temperature: ");
  Serial.println(sensordata2.temp);
  //-------------------------------------------
  radio.write(&sensordata2, sizeof(sensor2));
}

the result i always get is this

15:34:28.031 -> Sector 1

15:34:28.031 -> Temperature

15:34:28.031 -> 27.80

15:34:28.058 -> Humidity

15:34:28.058 -> 48

15:34:28.058 -> Sector 2

15:34:28.058 -> Temperature

15:34:28.101 -> 0.00

15:34:28.101 -> Humidity

15:34:28.101 -> 0.00

I am just starting to learn about coding, your help would be greatly appreciatedPreformatted text

Please post your code inline. Use the code tags (this button </> in the editor).

heyyy thanks for the tip. i have edited it already

Thanks.. much easier for anyone trying to help.

So what exactly is your problem?

the second transmitter is displaying zero values. I tried to use different format but the result is always the same the value from transmitter 2 is not showing. the second transmitter is the one with sector 2

15:34:28.031 -> Sector 1

15:34:28.031 -> Temperature

15:34:28.031 -> 27.80

15:34:28.058 -> Humidity

15:34:28.058 -> 48

15:34:28.058 -> Sector 2

15:34:28.058 -> Temperature

15:34:28.101 -> 0.00

15:34:28.101 -> Humidity

15:34:28.101 -> 0.00

each transmitter work individually when connected to receiver but when together 1 fails

If you swap the reading of sensor and sensor2 around then what happens?

Why are the data structures for the 2 sensor different?

They strategy "if one packet is available, read two packets" is bound to fail.

Using a fat 2 seconds delay in a receiver, is likewise unclever.

I think you need to be using the sensorData structure and populate the sensorNum field. Then you only need the following...

  if(radio.available())
  {
    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);}
    Serial.println("Sensor ");
    Serial.println(sensorData.sensorNum);
    }
  }

the result doesnt change even with same structure so i tried to search for a new format hoping the result will change

any suggestion, what should i try to do?

The problem is that you are read 2 sets of data... but there may only be one received.

Try the code in post #8

The format of the data must be the same.. because you don't know which one you will receive first.

I have tried the post number 8

if(radio.available())
  {
    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);
    Serial.println("Sensor ");
    Serial.println(sensorData.sensorNum);
    }

    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);
    Serial.println("Sensor ");
    Serial.println(sensorData.sensorNum);
    }
  }
}

the result is this
Humidity

46

Sensor

1

Temperature

0.00

Humidity

0

Sensor

0

I have also removed the delay, the second sensor is not registering

That is not what I suggested in post 8... get rid of the second radio.read() block. You only need this code ONCE.

    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);
    Serial.println("Sensor ");
    Serial.println(sensorData.sensorNum);
    }

ok ok sorry

if(radio.available())
  {
    radio.read(&sensorData, sizeof(sensorData));
    {
    Serial.println("Temperature ");
    Serial.println(sensorData.temp);
    Serial.println("Humidity ");
    Serial.println(sensorData.hum);
    Serial.println("Sensor ");
    Serial.println(sensorData.sensorNum);
    }
  

Temperature

26.80

Humidity

46

Sensor

1

this is the result

Only read a packet, when one is available.

Don't use any delays in a receiver.

To identify the source of the packet,
you can use an identifying field in a common data structure,
or use different pipes, and multiplex by receiving pipe,
or use different sized packets and dynamic payloads, and distinguish by size of the packet.

Using long long as a pipe address, is considered deprecated,
and could point to using an outdated library, or outdated sample code.

Have you ever sent something from sensor 2?

and get rid of the delay() in the receiver if you haven't already... it serves no purpose.

okay i will research on packets, payloads, and will try use different pipes, thanks for the input

okay okay i will try and follow your instructions, thanks for the input

heyyy bro i just made it work the problem is one of the module is defective and i followed your advice and use same struction also the one of the problem is the second radio.read(&sensorData, sizeof(sensorData));, the youtube video that i was following use it twice so i use it twice. thanks for your patience and help

1 Like

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