Header

How can i print based on node header in master ? , because values from both nodes are mixed

example:

Serial.println(header.from_node);
if (header.type == '01')
{
Serial.print("TEMP:");
 Serial.println(message.temperature);
 Serial.print("HUMY:");
 Serial.println(message.humidity);
 Serial.print("MQ2:");
 Serial.println(message.mq);
}
if (header.type == '02')
{
 Serial.print("ULTRASONIC:");
Serial.println(message.ultrasonic);
}

Slave 1:

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <DHT.h>

#define DHTTYPE DHT11   // DHT 11 
#define MQ2 A2
#define DHTPIN 3     // what pin we're connected to


DHT dht(DHTPIN, DHTTYPE);

// Radio with CE & CSN connected to 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 01;
const uint16_t parent_node = 00;

// Time between packets (in ms)
const unsigned long interval = 2000;

// Structure of our message
struct message_t {
  float temperature;
  float humidity;
  float mq2;
};
message_t message;

// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{
    pinMode(MQ2, INPUT);
  // Initialize all radio related modules
  SPI.begin();
  radio.begin();
  delay(5);
  network.begin(90, this_node);

  // Initialize the DHT library
  dht.begin();

  // Set up the Serial Monitor
  Serial.begin(9600);
}

void loop() {

  // Update network data
  network.update();

  // Read humidity (percent)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  float mq = analogRead(MQ2);

  // Construct the message we'll send (replace t with f if you're sending Fahrenheit)
  message = (message_t){ t, h, mq };
  
  // Headers will always be Temperature for this node
  // We set it again each loop iteration because fragmentation of the messages might change this between loops
  header.type = 't';

  // Writing the message to the network means sending it
  if (network.write(header, &message, sizeof(message))) {
    Serial.print("Message sent\n"); 
  } else {
    Serial.print("Could not send message\n"); 
  }

  // Wait a bit before we start over again
  delay(interval);
}

Slave 2:

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <DHT.h>

#include <NewPing.h>

#define TRIGGER_PIN  2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     3  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

// Radio with CE & CSN connected to 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 02;
const uint16_t parent_node = 00;

// Time between packets (in ms)
const unsigned long interval = 2000;

// Structure of our message
struct message_t {
  float ultrasonic;
};
message_t message;

// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{

  // Initialize all radio related modules
  SPI.begin();
  radio.begin();
  delay(5);
  network.begin(90, this_node);



  // Set up the Serial Monitor
  Serial.begin(9600);
}

void loop() {

  // Update network data
  network.update();

  // Read humidity (percent)
  float u = sonar.ping_cm();

  // Construct the message we'll send (replace t with f if you're sending Fahrenheit)
  message = (message_t){ u };
  
  // Headers will always be Temperature for this node
  // We set it again each loop iteration because fragmentation of the messages might change this between loops
  header.type = 't';

  // Writing the message to the network means sending it
  if (network.write(header, &message, sizeof(message))) {
    Serial.print("Message sent\n"); 
  } else {
    Serial.print("Could not send message\n"); 
  }

  // Wait a bit before we start over again
  delay(interval);
}

Master:

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


// Radio with CE & CSN connected to 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 00;
const uint16_t parent_node = 01;

// Time between packets (in ms)
const unsigned long interval = 2000;

// Structure of our message
struct message_t {
  float temperature;
  float humidity;
  float mq;
  float ultrasonic;
};
message_t message;

// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{
  // Initialize all radio related modules
  SPI.begin();
  radio.begin();
  delay(5);
  network.begin(90, this_node);


  // Set up the Serial Monitor
  Serial.begin(9600);
}

void loop() {

  // Update network data
  network.update();

  

  // Writing the message to the network means sending it
  if (network.read(header, &message, sizeof(message))) {
    Serial.print("TEMP:");
Serial.println(message.temperature);
 Serial.print("HUMY:");
    Serial.println(message.humidity);
     Serial.print("MQ2:");
Serial.println(message.mq);
Serial.print("ULTRASONIC:");
Serial.println(message.ultrasonic);
  // Wait a bit before we start over again
 
}
}

The 2 slaves each produce different formats of data. Your master code cannot simply combine these as you have attempted.

You could look at using “union” in your structure for the master. Alternatively you can use the “peek” method of the RF24Network class to see which slave sent the data and then chose which structure it is written into.

There is an example in the class documentation.

Your code extracts:

Slave1:

// Structure of our message
struct message_t {
  float temperature;
  float humidity;
  float mq2;
};


Slave2:

// Structure of our message
struct message_t {
  float ultrasonic;
};


Master:

// Structure of our message
struct message_t {
  float temperature;
  float humidity;
  float mq;
  float ultrasonic;
};
if (header.type == '01')

This will not work since 'parent_node' contains 1 and '01' contains two ASCII characters. Use:

if (header.type == 01)

Similarly for '02' use 02.

Of course, the leading zero is unnecessary. 01 and 02 are the same as 1 and 2.

vaj4088:
Of course, the leading zero is unnecessary. 01 and 02 are the same as 1 and 2.

But remember that 010 and 10 are not the same. The leading 0 means OCTAL (base 8) so 010 == 8 and 10 == 012.
And 08 is right out. Not even a valid numeric constant.

new code,after “peek” method, still mixed results

Slave1:

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#include <DHT.h>

#define DHTTYPE DHT11   // DHT 11 
#define MQ2 A2
#define DHTPIN 3     // what pin we're connected to


DHT dht(DHTPIN, DHTTYPE);

// Radio with CE & CSN connected to 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 01;
const uint16_t parent_node = 00;

// Time between packets (in ms)
const unsigned long interval = 2000;

// Structure of our message

struct payload {

 float mq2;
 float dht;
 float temperature;
 float humidity;
} dataREV;



// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{
   pinMode(MQ2, INPUT);
 // Initialize all radio related modules
 SPI.begin();
 radio.begin();
 delay(5);
 network.begin(90, this_node);

 // Initialize the DHT library
 dht.begin();

 // Set up the Serial Monitor
 Serial.begin(9600);
}

void loop() {

 // Update network data
 network.update();

 // Read humidity (percent)
 float h = dht.readHumidity();
 // Read temperature as Celsius
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit
 float f = dht.readTemperature(true);
 float mq = analogRead(MQ2);



 // Construct the message we'll send (replace t with f if you're sending Fahrenheit)
 dataREV = (payload){ t, h, mq };

Serial.print("TEMP:");
Serial.println(t);
Serial.print("HUMY:");
   Serial.println(h);
    Serial.print("MQ2:");
Serial.println(mq);

 
 // Headers will always be Temperature for this node
 // We set it again each loop iteration because fragmentation of the messages might change this between loops
 header.type = 'd';

 // Writing the message to the network means sending it
 if (network.write(header, &dataREV, sizeof(dataREV))) {
   Serial.print("Message sent\n"); 
 } else {
   Serial.print("Could not send message\n"); 
 }

 // Wait a bit before we start over again
 delay(interval);
}

Master:

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


// Radio with CE & CSN connected to 7 & 8
RF24 radio(7, 8);
RF24Network network(radio);

// Constants that identify this node and the node to send data to
const uint16_t this_node = 00;
const uint16_t sens_node = 01;
const uint16_t parent_node = 02;


// Time between packets (in ms)
const unsigned long interval = 2000;

// Structure of our message
struct message_t {

 float ultrasonic;
};
message_t message;


struct payload {
 float mq2;
 float dht;
 float temperature;
 float humidity;
} dataREV;

// The network header initialized for this node
RF24NetworkHeader header(parent_node);

void setup(void)
{
 // Initialize all radio related modules
 SPI.begin();
 radio.begin();
 delay(5);
 network.begin(90, this_node);


 // Set up the Serial Monitor
 Serial.begin(9600);
}

void loop() {

 // Update network data
 network.update();

 while ( network.available() )  {
 RF24NetworkHeader header;
 uint32_t time;
 network.peek(header);
 if(header.type == 't'){
   network.read(header,&message,sizeof(message));
   Serial.print("ULTRASONIC: ");
   Serial.println(message.ultrasonic);
   Serial.println("========================");
}

   if(header.type == 'd'){
   network.read(header,&dataREV,sizeof(dataREV));
   Serial.print("TEMP:");
Serial.println(dataREV.temperature);
Serial.print("HUMY:");
   Serial.println(dataREV.humidity);
    Serial.print("MQ2:");
Serial.println(dataREV.mq2);
 }

}
}

RESULT(MASTER):

TEMP:121.00 <----mq2
HUMY:0.00
MQ2:20.00 <----temperature
ULTRASONIC: 91.00
ULTRASONIC: 90.00
TEMP:121.00
HUMY:0.00
MQ2:20.00
ULTRASONIC: 90.00
TEMP:121.00
HUMY:0.00
MQ2:20.00
ULTRASONIC: 0.00
TEMP:120.00
HUMY:0.00
MQ2:20.00

In slave 1 you build this structure:

struct payload {

  float mq2;
  float dht;
  float temperature;
  float humidity;
} dataREV;

which has 4 floats in it. But you do this later which appears to assign only 3 floats to it and in an odd order.

dataREV = (payload) { t, h, mq };

Then you send the entire structure dataREV:

 if (network.write(header, &dataREV, sizeof(dataREV))) { . . . }

So the master will receive some rubbish.
Get the structure correct on Slave1 so it contains only data which you want to send to the master, and ensure the correct data is in the correct variables. Make sure then that the structure to receive this data on the master has the same definitions in the same order.

Also, instead of creating new floats h,t etc. you could directly use the floats in dataREV like this:dataREV.humidity, dataREV.temperature etc. then you would also not need this :

dataREV = (payload) { t, h, mq };

THNXXX , PROBLEM SOLVED :slight_smile: