6 Arduino Nanos with NRF24L01 as transmitters and 1 NodeMCU with NRF24L01 as receiver

Hi, i finished the code for transmitter 1 and 2 to test if the NodeMCU can receive more than 1 transmitter but it still receives from 1 transmitter (00002). what can I do to fix this?

Transmitter 1 code (Arduino Nano)

///-----------------------------------EFL CODE - TRANSMITTER 1-------------------------------///
#include <DHT.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(0, 10);  // CE, CSN

const byte pipe1[6] = "00001";

const int relayPin1 = 3;  // Pin for relay 1
const int relayPin2 = 4;  // Pin for relay 2
const int relayPin3 = 7;  // Pin for relay 3
const int relayPin4 = 8;  // Pin for relay 4
const int relayPump = 2;  // Pin for relay 5
const int flowPin = 5;    // Pin for Flow meter
const int thPin = 6;      // Pin for Temp and Humidity

const int moisturePin1 = A4;  // Pin for moisture sensor 1
const int moisturePin2 = A3;  // Pin for moisture sensor 2
const int moisturePin3 = A2;  // Pin for moisture sensor 3
const int moisturePin4 = A1;  // Pin for moisture sensor 4
const int phPin = A6;
const int tbdPin = A7;

//const float thresholdResistance = 1.0;  // Threshold resistance in ohms
const float lowerThreshold = 0.9;  // Lower threshold in ohms
const float upperThreshold = 1.5;  // Upper threshold in ohms
const long interval = 2000;        // Reading interval in milliseconds (5 seconds)

unsigned long currentTime;
unsigned long lastTime;
unsigned long pulse_freq;
double flow;

long previousMillis = 0;
DHT dht(thPin, DHT11);

struct SensorData {
  float sensor1;
  float sensor2;
  float sensor3;
  float sensor4;
  float temperature;
  float humidity;
  float pHValue;
  float flow;
  byte transmitterId;
};

SensorData node1data;

void pulse () // Interrupt function

{
   pulse_freq++;
}

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  radio.begin();
  radio.openWritingPipe(pipe1);  // Set the address for writing data
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
  

  // Set relay pins as OUTPUT and turn off relays initially (active HIGH)
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  pinMode(relayPump, OUTPUT);
  pinMode(flowPin, INPUT);
  pinMode(thPin, INPUT);

  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPump, HIGH);
  digitalWrite(flowPin, HIGH);
  digitalWrite(thPin, HIGH);

  dht.begin();
  attachInterrupt(0, pulse, RISING); // Setup Interrupt
}


void loop() {

  byte transmitterId = 1;

  int relay1Status = digitalRead(relayPin1);
  int relay2Status = digitalRead(relayPin2);
  int relay3Status = digitalRead(relayPin3);
  int relay4Status = digitalRead(relayPin4);
  int pumpStatus = digitalRead(relayPump);

  // Read temperature and humidity from DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  int rawValue = analogRead(phPin);
  float pHValue = map(rawValue, 0, 1023, 0, 14);


  unsigned long currentMillis = millis();

  float resistance1 = calculateResistance(analogRead(moisturePin1));
  float resistance2 = calculateResistance(analogRead(moisturePin2));
  float resistance3 = calculateResistance(analogRead(moisturePin3));
  float resistance4 = calculateResistance(analogRead(moisturePin4));

  int sensorValue1 = analogRead(moisturePin1);
  int sensorValue2 = analogRead(moisturePin2);
  int sensorValue3 = analogRead(moisturePin3);
  int sensorValue4 = analogRead(moisturePin4);

  float voltage1 = sensorValue1 * (5.0 / 1023.0);
  float voltage2 = sensorValue2 * (5.0 / 1023.0);
  float voltage3 = sensorValue3 * (5.0 / 1023.0);
  float voltage4 = sensorValue4 * (5.0 / 1023.0);

   // Every second, calculate and print L/Min
   if(currentMillis >= (lastTime + 1000)){
      lastTime = currentMillis; 
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      flow = (pulse_freq / 7.5); 
      pulse_freq = 0; // Reset Counter
   }
      

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    Serial.println("Transmitter ID: " + String(transmitterId));
    printResistance("Sensor 1", resistance1, voltage1, sensorValue1);
    printResistance("Sensor 2", resistance2, voltage2, sensorValue2);
    printResistance("Sensor 3", resistance3, voltage3, sensorValue3);
    printResistance("Sensor 4", resistance4, voltage4, sensorValue4);
    Serial.print(flow); 
    Serial.println(" L/Min");

    

    if (!isnan(humidity) && !isnan(temperature)) {
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println("%\t");
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println("°C");
    } else {
      Serial.println("Failed to read from DHT sensor!");
    }

    // Print pH value to Serial Monitor
    Serial.print("pH Value: ");
    Serial.println(pHValue);

    Serial.print("Relay 1 Status: ");
    Serial.println(relay1Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 2 Status: ");
    Serial.println(relay2Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 3 Status: ");
    Serial.println(relay3Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 4 Status: ");
    Serial.println(relay4Status == LOW ? "ON" : "OFF");

    Serial.print("Pump Status: ");
    Serial.println(pumpStatus == LOW ? "ON" : "OFF");
    
  }

  if (resistance1 < lowerThreshold) {
    digitalWrite(relayPin1, LOW);
  } else if (resistance1 > upperThreshold) {
    digitalWrite(relayPin1, HIGH);
  }

  if (resistance2 < lowerThreshold) {
    digitalWrite(relayPin2, LOW);
  } else if (resistance2 > upperThreshold) {
    digitalWrite(relayPin2, HIGH);
  }

  if (resistance3 < lowerThreshold) {
    digitalWrite(relayPin3, LOW);
  } else if (resistance3 > upperThreshold) {
    digitalWrite(relayPin3, HIGH);
  }

  if (resistance4 < lowerThreshold) {
    digitalWrite(relayPin4, LOW);
  } else if (resistance4 > upperThreshold) {
    digitalWrite(relayPin4, HIGH);
  }

  // Check if any of the 4 relays are on
  if (relay1Status == LOW || relay2Status == LOW || relay3Status == LOW || relay4Status == LOW) {
    // Turn on the 5th relay
    digitalWrite(relayPump, LOW);
  } else {
    // Turn off the 5th relay
    digitalWrite(relayPump, HIGH);
  }
  delay(2000);
    node1data.sensor1 = sensorValue1;
    node1data.sensor2 = sensorValue2;
    node1data.sensor3 = sensorValue3;
    node1data.sensor4 = sensorValue4;
    node1data.temperature = temperature;
    node1data.humidity = humidity;
    node1data.pHValue = pHValue;
    node1data.transmitterId = transmitterId;
    // Send data to receiver
    radio.write(&node1data, sizeof(node1data));
}

// Function to calculate resistance based on analog value
float calculateResistance(int sensorValue) {
  float voltage = sensorValue * (5.0 / 1023.0);
  return (5.0 - voltage) / voltage;
}

// Function to print resistance values
void printResistance(const char* sensorName, float resistance, float voltage, int sensorValue) {
  Serial.print(sensorName);
  Serial.print(" Resistance: ");
  Serial.print(resistance, 2);
  Serial.print(" ohms, Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V, Sensor Value: ");
  Serial.println(sensorValue);
}

Transmitter 2 code (Arduino Nano)

///-----------------------------------EFL CODE - TRANSMITTER 2-------------------------------///
#include <DHT.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(0, 10);  // CE, CSN

const byte pipe2[6] = "00002";

const int relayPin1 = 3;  // Pin for relay 1
const int relayPin2 = 4;  // Pin for relay 2
const int relayPin3 = 7;  // Pin for relay 3
const int relayPin4 = 8;  // Pin for relay 4
const int relayPump = 2;  // Pin for relay 5
const int flowPin = 5;    // Pin for Flow meter
const int thPin = 6;      // Pin for Temp and Humidity

const int moisturePin1 = A4;  // Pin for moisture sensor 1
const int moisturePin2 = A3;  // Pin for moisture sensor 2
const int moisturePin3 = A2;  // Pin for moisture sensor 3
const int moisturePin4 = A1;  // Pin for moisture sensor 4
const int phPin = A6;
const int tbdPin = A7;

//const float thresholdResistance = 1.0;  // Threshold resistance in ohms
const float lowerThreshold = 0.9;  // Lower threshold in ohms
const float upperThreshold = 1.5;  // Upper threshold in ohms
const long interval = 2000;        // Reading interval in milliseconds (5 seconds)

unsigned long currentTime;
unsigned long lastTime;
unsigned long pulse_freq;
double flow;

long previousMillis = 0;
DHT dht(thPin, DHT11);

struct SensorData {
  float sensor1;
  float sensor2;
  float sensor3;
  float sensor4;
  float temperature;
  float humidity;
  float pHValue;
  float flow;
  byte transmitterId;
};

SensorData node2data;

void pulse () // Interrupt function

{
   pulse_freq++;
}

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  radio.begin();
  radio.openWritingPipe(pipe2);  // Set the address for writing data
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
  

  // Set relay pins as OUTPUT and turn off relays initially (active HIGH)
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  pinMode(relayPump, OUTPUT);
  pinMode(flowPin, INPUT);
  pinMode(thPin, INPUT);

  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPump, HIGH);
  digitalWrite(flowPin, HIGH);
  digitalWrite(thPin, HIGH);

  dht.begin();
  attachInterrupt(0, pulse, RISING); // Setup Interrupt
}


void loop() {

  byte transmitterId = 2;

  int relay1Status = digitalRead(relayPin1);
  int relay2Status = digitalRead(relayPin2);
  int relay3Status = digitalRead(relayPin3);
  int relay4Status = digitalRead(relayPin4);
  int pumpStatus = digitalRead(relayPump);

  // Read temperature and humidity from DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  int rawValue = analogRead(phPin);
  float pHValue = map(rawValue, 0, 1023, 0, 14);


  unsigned long currentMillis = millis();

  float resistance1 = calculateResistance(analogRead(moisturePin1));
  float resistance2 = calculateResistance(analogRead(moisturePin2));
  float resistance3 = calculateResistance(analogRead(moisturePin3));
  float resistance4 = calculateResistance(analogRead(moisturePin4));

  int sensorValue1 = analogRead(moisturePin1);
  int sensorValue2 = analogRead(moisturePin2);
  int sensorValue3 = analogRead(moisturePin3);
  int sensorValue4 = analogRead(moisturePin4);

  float voltage1 = sensorValue1 * (5.0 / 1023.0);
  float voltage2 = sensorValue2 * (5.0 / 1023.0);
  float voltage3 = sensorValue3 * (5.0 / 1023.0);
  float voltage4 = sensorValue4 * (5.0 / 1023.0);

   // Every second, calculate and print L/Min
   if(currentMillis >= (lastTime + 1000)){
      lastTime = currentMillis; 
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      flow = (pulse_freq / 7.5); 
      pulse_freq = 0; // Reset Counter
   }
      

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    
    Serial.println("Transmitter ID: " + String(transmitterId));
    printResistance("Sensor 1", resistance1, voltage1, sensorValue1);
    printResistance("Sensor 2", resistance2, voltage2, sensorValue2);
    printResistance("Sensor 3", resistance3, voltage3, sensorValue3);
    printResistance("Sensor 4", resistance4, voltage4, sensorValue4);
    Serial.print(flow); 
    Serial.println(" L/Min");

    

    if (!isnan(humidity) && !isnan(temperature)) {
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println("%\t");
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println("°C");
    } else {
      Serial.println("Failed to read from DHT sensor!");
    }

    // Print pH value to Serial Monitor
    Serial.print("pH Value: ");
    Serial.println(pHValue);

    Serial.print("Relay 1 Status: ");
    Serial.println(relay1Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 2 Status: ");
    Serial.println(relay2Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 3 Status: ");
    Serial.println(relay3Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 4 Status: ");
    Serial.println(relay4Status == LOW ? "ON" : "OFF");

    Serial.print("Pump Status: ");
    Serial.println(pumpStatus == LOW ? "ON" : "OFF");
    
  }

  if (resistance1 < lowerThreshold) {
    digitalWrite(relayPin1, LOW);
  } else if (resistance1 > upperThreshold) {
    digitalWrite(relayPin1, HIGH);
  }

  if (resistance2 < lowerThreshold) {
    digitalWrite(relayPin2, LOW);
  } else if (resistance2 > upperThreshold) {
    digitalWrite(relayPin2, HIGH);
  }

  if (resistance3 < lowerThreshold) {
    digitalWrite(relayPin3, LOW);
  } else if (resistance3 > upperThreshold) {
    digitalWrite(relayPin3, HIGH);
  }

  if (resistance4 < lowerThreshold) {
    digitalWrite(relayPin4, LOW);
  } else if (resistance4 > upperThreshold) {
    digitalWrite(relayPin4, HIGH);
  }

  // Check if any of the 4 relays are on
  if (relay1Status == LOW || relay2Status == LOW || relay3Status == LOW || relay4Status == LOW) {
    // Turn on the 5th relay
    digitalWrite(relayPump, LOW);
  } else {
    // Turn off the 5th relay
    digitalWrite(relayPump, HIGH);
  }
  delay(2000);
    node2data.sensor1 = sensorValue1;
    node2data.sensor2 = sensorValue2;
    node2data.sensor3 = sensorValue3;
    node2data.sensor4 = sensorValue4;
    node2data.temperature = temperature;
    node2data.humidity = humidity;
    node2data.pHValue = pHValue;
    node2data.transmitterId = transmitterId;
    // Send data to receiver
    radio.write(&node2data, sizeof(node2data));
}

// Function to calculate resistance based on analog value
float calculateResistance(int sensorValue) {
  float voltage = sensorValue * (5.0 / 1023.0);
  return (5.0 - voltage) / voltage;
}

// Function to print resistance values
void printResistance(const char* sensorName, float resistance, float voltage, int sensorValue) {
  Serial.print(sensorName);
  Serial.print(" Resistance: ");
  Serial.print(resistance, 2);
  Serial.print(" ohms, Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V, Sensor Value: ");
  Serial.println(sensorValue);
}
 


Receiver code (NodeMCU)

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

const float lowerThreshold = 0.4;  // Lower threshold in ohms
const float upperThreshold = 1.5;  // Upper threshold in ohms

float percent1 = 0;
float percent2 = 0;
float percent3 = 0;
float percent4 = 0;
float resistance1 = 0;
float resistance2 = 0;
float resistance3 = 0;
float resistance4 = 0;
float voltage1 = 0;
float voltage2 = 0;
float voltage3 = 0;
float voltage4 = 0;
float sensorValue1 = 0;
float sensorValue2 = 0;
float sensorValue3 = 0;
float sensorValue4 = 0;
float relay1 = 0;
float relay2 = 0;
float relay3 = 0;
float relay4 = 0;
float pump = 0;


RF24 radio(D4, D2);

const byte addresses [][6] = {"00001", "00002"};

struct SensorData {
  float sensor1;
  float sensor2;
  float sensor3;
  float sensor4;
  float temperature;
  float humidity;
  float pHValue;
  float flow;
};

SensorData receivedData;

void setup() {
  Serial.begin(9600);
  radio.begin();

  radio.openReadingPipe(0, addresses[0]);  // Set up the first reading pipe for "00001"
  radio.openReadingPipe(1, addresses[1]);  // Set up the second reading pipe for "00002"
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  }

void loop() {
  while (radio.available()) {
    radio.read(&receivedData, sizeof(receivedData));
    Serial.println("Received data from Arduino Nano 1:");
    Serial.print("sensor 1: ");
    Serial.println(receivedData.sensor1, 2);
    Serial.print("sensor 2: ");
    Serial.println(receivedData.sensor2, 2);
    Serial.print("sensor 3: ");
    Serial.println(receivedData.sensor3, 2);
    Serial.print("sensor 4: ");
    Serial.println(receivedData.sensor4, 2);
    Serial.print("Temperature: ");
    Serial.print(receivedData.temperature);
    Serial.println("°C");
    Serial.print("Humidity: ");
    Serial.print(receivedData.humidity);
    Serial.println("%\t");
    Serial.print("pH Value: ");
    Serial.println(receivedData.pHValue);
    
    sensorValue1 = receivedData.sensor1;
    sensorValue2 = receivedData.sensor2;
    sensorValue3 = receivedData.sensor3;
    sensorValue4 = receivedData.sensor4;

    resistance1 = calculateResistance(sensorValue1);
    resistance2 = calculateResistance(sensorValue2);
    resistance3 = calculateResistance(sensorValue3);
    resistance4 = calculateResistance(sensorValue4);
    
    if (sensorValue1 !=0){
      voltage1 = sensorValue1 * (5.0 / 1023.0);
      percent1 = (voltage1/5) * 100;
    }
    if (sensorValue2 !=0){
    voltage2 = sensorValue2 * (5.0 / 1023.0);
    percent2 = (voltage2/5) * 100;
    }
    if (sensorValue3 !=0){
    voltage3 = sensorValue3 * (5.0 / 1023.0);
    percent3 = (voltage3/5) * 100;
    }
    if (sensorValue4 !=0){
    voltage4 = sensorValue4 * (5.0 / 1023.0);
    percent4 = (voltage4/5) * 100;
    }

    printResistance("Sensor 1", resistance1, voltage1, percent1);
    printResistance("Sensor 2", resistance2, voltage2, percent2);
    printResistance("Sensor 3", resistance3, voltage3, percent3);
    printResistance("Sensor 4", resistance4, voltage4, percent4);

  }
}

// Function to calculate resistance based on analog value
float calculateResistance(int sensorValue) {
  float voltage = sensorValue * (5.0 / 1023.0);
  return (5.0 - voltage) / voltage;
}

// Function to print resistance values
void printResistance(const char* sensorName, float resistance, float voltage, int sensorValue) {
  Serial.print(sensorName);
  Serial.print(" Resistance: ");
  Serial.print(resistance, 2);
  Serial.print(" ohms, Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V, Sensor Value: ");
  Serial.println(sensorValue);
}

Where do you check which transmitter the data came from?

I just checked the serial monitor on both arduino nanos to see if it was the same. I tried to use a"transmitterId" but it doesn't show up in the receiver. it only shows 0 instead of 1 or 2

Serial of Transmitter 1

7:14:35.850 -> Transmitter ID: 1
17:14:35.850 -> Sensor 1 Resistance: 3.00 ohms, Voltage: 1.21 V, Sensor Value: 248
17:14:35.924 -> Sensor 2 Resistance: 3.26 ohms, Voltage: 1.18 V, Sensor Value: 242
17:14:35.991 -> Sensor 3 Resistance: 3.18 ohms, Voltage: 1.18 V, Sensor Value: 241
17:14:36.057 -> Sensor 4 Resistance: 3.16 ohms, Voltage: 1.17 V, Sensor Value: 240
17:14:36.157 -> 0.00 L/Min
17:14:36.157 -> Failed to read from DHT sensor!
17:14:36.190 -> pH Value: 3.00
17:14:36.223 -> Relay 1 Status: OFF
17:14:36.223 -> Relay 2 Status: OFF
17:14:36.257 -> Relay 3 Status: ON
17:14:36.257 -> Relay 4 Status: OFF
17:14:36.293 -> Pump Status: ON

Serial of Transmitter 2

17:20:54.242 -> Transmitter ID: 2
17:20:54.242 -> Sensor 1 Resistance: 0.01 ohms, Voltage: 4.97 V, Sensor Value: 1017
17:20:54.325 -> Sensor 2 Resistance: 62.94 ohms, Voltage: 0.08 V, Sensor Value: 16
17:20:54.392 -> Sensor 3 Resistance: 0.01 ohms, Voltage: 4.97 V, Sensor Value: 1016
17:20:54.492 -> Sensor 4 Resistance: 0.01 ohms, Voltage: 4.97 V, Sensor Value: 1017
17:20:54.559 -> 0.00 L/Min
17:20:54.559 -> Humidity: 80.00%	
17:20:54.591 -> Temperature: 31.80°C
17:20:54.591 -> pH Value: 9.00
17:20:54.626 -> Relay 1 Status: ON
17:20:54.659 -> Relay 2 Status: OFF
17:20:54.659 -> Relay 3 Status: ON
17:20:54.698 -> Relay 4 Status: ON
17:20:54.698 -> Pump Status: ON

Serial of the Receiver

6:53:46.615 -> Received data from Arduino Nano : 0
16:53:46.648 -> Transmitter ID:0
16:53:46.648 -> sensor 1: 250.00
16:53:46.682 -> sensor 2: 243.00
16:53:46.682 -> sensor 3: 243.00
16:53:46.715 -> sensor 4: 242.00
16:53:46.715 -> Temperature: nan°C
16:53:46.748 -> Humidity: nan%	
16:53:46.781 -> pH Value: 3.00
16:53:46.781 -> Sensor 1 Resistance: 3.09 ohms, Voltage: 1.22 V, Sensor Value: 24
16:53:46.848 -> Sensor 2 Resistance: 3.21 ohms, Voltage: 1.19 V, Sensor Value: 23
16:53:46.914 -> Sensor 3 Resistance: 3.21 ohms, Voltage: 1.19 V, Sensor Value: 23
16:53:46.980 -> Sensor 4 Resistance: 3.23 ohms, Voltage: 1.18 V, Sensor Value: 23

based on the serial monitors the transmitter 1 is the only one being received by the receiver.

The maimum NRF24 payload is 32 bytes,
Your structure has at least 33 bytes size:

Your receiver struct doesn't have the ID byte in it.

Also make sure you don't exceed the packet size in your messages.

Temperature, pH and humidity can all be held in 16-bit integers, (2 bytes) rather than a float (4 bytes). Multiply them by 10 on the Tx side and divide by 10 on the Rx side.

1 Like

Thankyou for that, I updated the Transmitter 1 and receiver code

Transmitter 1 updated code

///-----------------------------------EFL CODE - TRANSMITTER 1-------------------------------///
#include <DHT.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 10);  // CE, CSN

const byte pipe1[6] = "00001";

const int relayPin1 = 3;  // Pin for relay 1
const int relayPin2 = 4;  // Pin for relay 2
const int relayPin3 = 7;  // Pin for relay 3
const int relayPin4 = 8;  // Pin for relay 4
const int relayPump = 2;  // Pin for relay 5
const int flowPin = 5;    // Pin for Flow meter
const int thPin = 6;      // Pin for Temp and Humidity

const int moisturePin1 = A4;  // Pin for moisture sensor 1
const int moisturePin2 = A3;  // Pin for moisture sensor 2
const int moisturePin3 = A2;  // Pin for moisture sensor 3
const int moisturePin4 = A1;  // Pin for moisture sensor 4
const int phPin = A6;
const int tbdPin = A7;

//const float thresholdResistance = 1.0;  // Threshold resistance in ohms
const float lowerThreshold = 0.9;  // Lower threshold in ohms
const float upperThreshold = 1.5;  // Upper threshold in ohms
const long interval = 2000;        // Reading interval in milliseconds (5 seconds)

unsigned long currentTime;
unsigned long lastTime;
unsigned long pulse_freq;
double flow;

long previousMillis = 0;
DHT dht(thPin, DHT11);

struct SensorData {
  int sensor1;
  int sensor2;
  int sensor3;
  int sensor4;
  int16_t temperature;  // 16-bit integer for temperature
  int16_t humidity;     // 16-bit integer for humidity
  int16_t pHValue;      // 16-bit integer for pH value
  int flow;
  byte transmitterId;
};

SensorData node1data;

void pulse()  // Interrupt function

{
  pulse_freq++;
}

void setup() {
  Serial.begin(9600);  // Initialize serial communication
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.openWritingPipe(pipe1);
  radio.setPALevel(RF24_PA_LOW);
  radio.stopListening();


  // Set relay pins as OUTPUT and turn off relays initially (active HIGH)
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  pinMode(relayPump, OUTPUT);
  pinMode(flowPin, INPUT);
  pinMode(thPin, INPUT);

  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
  digitalWrite(relayPin3, HIGH);
  digitalWrite(relayPin4, HIGH);
  digitalWrite(relayPump, HIGH);
  digitalWrite(flowPin, HIGH);
  digitalWrite(thPin, HIGH);

  dht.begin();
  attachInterrupt(0, pulse, RISING);  // Setup Interrupt
}


void loop() {

  byte transmitterId = 1;

  int relay1Status = digitalRead(relayPin1);
  int relay2Status = digitalRead(relayPin2);
  int relay3Status = digitalRead(relayPin3);
  int relay4Status = digitalRead(relayPin4);
  int pumpStatus = digitalRead(relayPump);

  // Read temperature and humidity from DHT sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  int rawValue = analogRead(phPin);
  float pHValue = map(rawValue, 0, 1023, 0, 14);


  unsigned long currentMillis = millis();

  float resistance1 = calculateResistance(analogRead(moisturePin1));
  float resistance2 = calculateResistance(analogRead(moisturePin2));
  float resistance3 = calculateResistance(analogRead(moisturePin3));
  float resistance4 = calculateResistance(analogRead(moisturePin4));

  int sensorValue1 = analogRead(moisturePin1);
  int sensorValue2 = analogRead(moisturePin2);
  int sensorValue3 = analogRead(moisturePin3);
  int sensorValue4 = analogRead(moisturePin4);

  float voltage1 = sensorValue1 * (5.0 / 1023.0);
  float voltage2 = sensorValue2 * (5.0 / 1023.0);
  float voltage3 = sensorValue3 * (5.0 / 1023.0);
  float voltage4 = sensorValue4 * (5.0 / 1023.0);

  // Every second, calculate and print L/Min
  if (currentMillis >= (lastTime + 1000)) {
    lastTime = currentMillis;
    // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
    flow = (pulse_freq / 7.5);
    pulse_freq = 0;  // Reset Counter
  }


  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    Serial.println("Transmitter ID: " + String(transmitterId));
    printResistance("Sensor 1", resistance1, voltage1, sensorValue1);
    printResistance("Sensor 2", resistance2, voltage2, sensorValue2);
    printResistance("Sensor 3", resistance3, voltage3, sensorValue3);
    printResistance("Sensor 4", resistance4, voltage4, sensorValue4);
    Serial.print(flow);
    Serial.println(" L/Min");



    if (!isnan(humidity) && !isnan(temperature)) {
      Serial.print("Humidity: ");
      Serial.print(humidity);
      Serial.println("%\t");
      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println("°C");
    } else {
      Serial.println("Failed to read from DHT sensor!");
    }

    // Print pH value to Serial Monitor
    Serial.print("pH Value: ");
    Serial.println(pHValue);

    Serial.print("Relay 1 Status: ");
    Serial.println(relay1Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 2 Status: ");
    Serial.println(relay2Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 3 Status: ");
    Serial.println(relay3Status == LOW ? "ON" : "OFF");

    Serial.print("Relay 4 Status: ");
    Serial.println(relay4Status == LOW ? "ON" : "OFF");

    Serial.print("Pump Status: ");
    Serial.println(pumpStatus == LOW ? "ON" : "OFF");
  }

  if (resistance1 < lowerThreshold) {
    digitalWrite(relayPin1, LOW);
  } else if (resistance1 > upperThreshold) {
    digitalWrite(relayPin1, HIGH);
  }

  if (resistance2 < lowerThreshold) {
    digitalWrite(relayPin2, LOW);
  } else if (resistance2 > upperThreshold) {
    digitalWrite(relayPin2, HIGH);
  }

  if (resistance3 < lowerThreshold) {
    digitalWrite(relayPin3, LOW);
  } else if (resistance3 > upperThreshold) {
    digitalWrite(relayPin3, HIGH);
  }

  if (resistance4 < lowerThreshold) {
    digitalWrite(relayPin4, LOW);
  } else if (resistance4 > upperThreshold) {
    digitalWrite(relayPin4, HIGH);
  }

  // Check if any of the 4 relays are on
  if (relay1Status == LOW || relay2Status == LOW || relay3Status == LOW || relay4Status == LOW) {
    // Turn on the 5th relay
    digitalWrite(relayPump, LOW);
  } else {
    // Turn off the 5th relay
    digitalWrite(relayPump, HIGH);
  }
  delay(2000);
  node1data.sensor1 = sensorValue1;
  node1data.sensor2 = sensorValue2;
  node1data.sensor3 = sensorValue3;
  node1data.sensor4 = sensorValue4;
  node1data.temperature = int16_t(temperature * 10);  // Multiply temperature by 10
  node1data.humidity = int16_t(humidity * 10);        // Multiply humidity by 10
  node1data.pHValue = int16_t(pHValue * 10);          // Multiply pHValue by 10
  node1data.transmitterId = transmitterId;

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

}

// Function to calculate resistance based on analog value
float calculateResistance(int sensorValue) {
  float voltage = sensorValue * (5.0 / 1023.0);
  return (5.0 - voltage) / voltage;
}

// Function to print resistance values
void printResistance(const char* sensorName, float resistance, float voltage, int sensorValue) {
  Serial.print(sensorName);
  Serial.print(" Resistance: ");
  Serial.print(resistance, 2);
  Serial.print(" ohms, Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V, Sensor Value: ");
  Serial.println(sensorValue);
}

receiver updated code

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

const float lowerThreshold = 0.4;  // Lower threshold in ohms
const float upperThreshold = 1.5;  // Upper threshold in ohms

float percent1 = 0;
float percent2 = 0;
float percent3 = 0;
float percent4 = 0;
float resistance1 = 0;
float resistance2 = 0;
float resistance3 = 0;
float resistance4 = 0;
float voltage1 = 0;
float voltage2 = 0;
float voltage3 = 0;
float voltage4 = 0;
float sensorValue1 = 0;
float sensorValue2 = 0;
float sensorValue3 = 0;
float sensorValue4 = 0;
float relay1 = 0;
float relay2 = 0;
float relay3 = 0;
float relay4 = 0;
float pump = 0;

float percent11 = 0;
float percent21 = 0;
float percent31 = 0;
float percent41 = 0;
float resistance11 = 0;
float resistance21 = 0;
float resistance31 = 0;
float resistance41 = 0;
float voltage11 = 0;
float voltage21 = 0;
float voltage31 = 0;
float voltage41 = 0;
float sensorValue11 = 0;
float sensorValue21 = 0;
float sensorValue31 = 0;
float sensorValue41 = 0;
float relay11 = 0;
float relay21 = 0;
float relay31 = 0;
float relay41 = 0;
float pump1 = 0;
byte transmitterId;


RF24 radio(D4, D2);

const byte addresses[][6] = { "00001", "00002" };

struct SensorData {
  int sensor1;
  int sensor2;
  int sensor3;
  int sensor4;
  int16_t temperature;  // 16-bit integer for temperature
  int16_t humidity;     // 16-bit integer for humidity
  int16_t pHValue;      // 16-bit integer for pH value
  int flow;
  byte transmitterId;
};

SensorData receivedData;

void setup() {
  // Start Serial communication
  Serial.begin(9600);
  // Setup NRF module
  radio.begin();
  radio.openReadingPipe(0, addresses[0]);  // Set up the first reading pipe for "00001"
  radio.openReadingPipe(1, addresses[1]);  // Set up the second reading pipe for "00002"
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&receivedData, sizeof(receivedData));
    Serial.print("Received data from Arduino Nano : ");
    Serial.println(receivedData.transmitterId, 2);
    Serial.print("Transmitter ID:");
    Serial.println(receivedData.transmitterId, 2);
    Serial.print("sensor 1: ");
    Serial.println(receivedData.sensor1, 2);
    Serial.print("sensor 2: ");
    Serial.println(receivedData.sensor2, 2);
    Serial.print("sensor 3: ");
    Serial.println(receivedData.sensor3, 2);
    Serial.print("sensor 4: ");
    Serial.println(receivedData.sensor4, 2);

    // Divide these values by 10
    Serial.print("Temperature: ");
    Serial.print(receivedData.temperature / 10.0);  // Divide by 10
    Serial.println("°C");

    Serial.print("Humidity: ");
    Serial.print(receivedData.humidity / 10.0);  // Divide by 10
    Serial.println("%\t");

    Serial.print("pH Value: ");
    Serial.println(receivedData.pHValue / 10.0);  // Divide by 10

    sensorValue1 = receivedData.sensor1;
    sensorValue2 = receivedData.sensor2;
    sensorValue3 = receivedData.sensor3;
    sensorValue4 = receivedData.sensor4;

    resistance1 = calculateResistance(sensorValue1);
    resistance2 = calculateResistance(sensorValue2);
    resistance3 = calculateResistance(sensorValue3);
    resistance4 = calculateResistance(sensorValue4);

    if (sensorValue1 != 0) {
      voltage1 = sensorValue1 * (5.0 / 1023.0);
      percent1 = (voltage1 / 5) * 100;
    }
    if (sensorValue2 != 0) {
      voltage2 = sensorValue2 * (5.0 / 1023.0);
      percent2 = (voltage2 / 5) * 100;
    }
    if (sensorValue3 != 0) {
      voltage3 = sensorValue3 * (5.0 / 1023.0);
      percent3 = (voltage3 / 5) * 100;
    }
    if (sensorValue4 != 0) {
      voltage4 = sensorValue4 * (5.0 / 1023.0);
      percent4 = (voltage4 / 5) * 100;
    }

    printResistance("Sensor 1", resistance1, voltage1, percent1);
    printResistance("Sensor 2", resistance2, voltage2, percent2);
    printResistance("Sensor 3", resistance3, voltage3, percent3);
    printResistance("Sensor 4", resistance4, voltage4, percent4);
  }
  delay(2000);
}

// Function to calculate resistance based on analog value
float calculateResistance(int sensorValue) {
  float voltage = sensorValue * (5.0 / 1023.0);
  return (5.0 - voltage) / voltage;
}

// Function to print resistance values
void printResistance(const char* sensorName, float resistance, float voltage, int sensorValue) {
  Serial.print(sensorName);
  Serial.print(" Resistance: ");
  Serial.print(resistance, 2);
  Serial.print(" ohms, Voltage: ");
  Serial.print(voltage, 2);
  Serial.print(" V, Sensor Value: ");
  Serial.println(sensorValue);
}

the receiver is not displaying anything in the serial monitor, I also double-checked the connections and my structure is less than 32bytes (17)



And what the results? Are the updated codes works?

1 Like

The updated code doesn't work and I think it relates to the address because when I changed the "const byte addresses[][6] = { "00001", "00002" };" of the receiver to "const byte address[6] = "00001"; " it works like a charm and printing the data for the 1st transmitter with the address of 00001.

this is the screen shot of the serial monitor for the transmitter 1, 2 and receiver

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