Multiple slaves Arduino

Problem with reciving data from 2 slaves,response get mixed(sorry about amateur code - beginner)need help clearing extra code and optimization

Tx1 -Arduino + DHT11+MQ2+NRF24l01

#include <DHT.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>  

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


DHT dht(DHTPIN, DHTTYPE);

const byte slaveAddress[5] = {'R','x','A','A','A'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio (7, 8);



struct package
  {
    float mq2 = 0;
    float dht = 0;
    float temperature = 0;
    float humidity = 0;
  };

typedef struct package Package;
Package data;


char dataReceived[10]; // must match dataToSend in master

bool newData = false;

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


void setup() {
pinMode(MQ2, INPUT);
    Serial.begin(9600);

    Serial.println("Slave Starting...");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);

    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

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

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

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

void send() {
 


data.temperature = dht.readTemperature();
  data.humidity = dht.readHumidity();

  
  data.mq2 = analogRead(MQ2);
  delay(10);
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &data, sizeof(data) );
     
            
            Serial.print("Kvaliteta zraka: ");
  Serial.println(data.mq2);
    Serial.print("Humidity: "); 
    Serial.print(data.humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(data.temperature);
    Serial.println(" *C");
    
        radio.startListening();

        Serial.print("Reply Sent ");




        if (rslt) {
            Serial.println(" Received");
           
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

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

void getData() {

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data Received ");
        Serial.println(dataReceived);
    }
}

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

Tx2 -Arduino + HC-SR04+NRF24l01

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>   // Comes with Arduino IDE
#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.


const byte slaveAddress[5] = {'R','x','A','A','B'};
const byte masterAddress[5] = {'T','X','a','a','a'};
RF24 radio (7, 8);




struct package
  {
    float ultrasonic = 0;
   
  };

typedef struct package Package;
Package data;


char dataReceived[10]; // must match dataToSend in master

bool newData = false;

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


void setup() {

    Serial.begin(9600);

    Serial.println("Slave Starting...");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);

    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

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

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

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

void send() {
                   
 
data.ultrasonic = sonar.ping_cm();
  
  
  delay(10);
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &data, sizeof(data) );
     
            
            Serial.print("Ultrasonic: ");
  Serial.println(data.ultrasonic);
  Serial.println("cm");
    
        radio.startListening();

        Serial.print("Reply Sent ");




        if (rslt) {
            Serial.println(" Received");
           
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

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

void getData() {

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data Received ");
        Serial.println(dataReceived);
    }
}

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

Rx -Arduino + NRF24l01

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>   // Comes with Arduino IDE


const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
const byte slave2Address[5] = {'R', 'x', 'A', 'A', 'B'};
const byte masterAddress[5] = {'T', 'X', 'a', 'a', 'a'};
RF24 radio (7, 8);

struct payload {

  float mq2;
  float dht;
  float temperature;
  float humidity;


} dataREV;

float ultrasonic;

char dataToSend[10] = "Node 0 ";

const unsigned long txIntervalMillis = 1000;

void setup() {
  Serial.begin(9600);
  Serial.println(F("Master Starting"));
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.startListening();
}

void loop() {
  static unsigned long lastSend;
  static byte target = 0;
  unsigned long topLoop = millis();
  if (topLoop - lastSend >= txIntervalMillis) {
    radio.stopListening();
    radio.openWritingPipe(target ? slave2Address : slaveAddress);
    Serial.print(target);
    Serial.print(F(": '"));
    Serial.print(dataToSend);
    if (radio.write( &dataToSend, sizeof(dataToSend))) {
      Serial.println(F("' OK"));
      if (++dataToSend[8] > '9') {
        dataToSend[8] = '0';
      }
    } else {
      Serial.println(F("' failed"));
    }
    radio.startListening();
    if (++target > 1) {
      target = 0;
    }
    lastSend = topLoop;
  }
  if (radio.available()) {
    radio.read(&dataREV, sizeof(dataREV));


Serial.print("MQ2: ");
  Serial.println(dataREV.mq2);
    Serial.print("Humidity: "); 
   
    Serial.print(dataREV.humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
 
   Serial.print(dataREV.temperature);
    Serial.println(" *C");
     Serial.println();

      radio.read(&data, sizeof(data));

Serial.print("Ultrasonic: ");
  Serial.println(ultrasonic);

 
  }
}

Capture.PNG

Have a look at this Simple nRF24L01+ Tutorial. There is an example for two slaves that could be extended to many more.

Wireless code can be very difficult to debug. Get the wireless communication working reliably before adding any other features into the program.

…R

just need a way to seperate results from slave1 and slave 2.....

loop66: just need a way to seperate results from slave1 and slave 2.....

In the message sent by slave1 include an 'A' and in the message sent by slave2 include a 'B'

This problem is much easier to deal with if you use my 2nd example which uses the ackPayload feature. In that case the master initiates all the calls so there is no doubt about which slave the data came from.

...R