8 in 1 sensor need help

Soooo im using a 8 in 1 soil sensor for our project and for somereason all of the codes work propwrly but when you enter the serial monitor it always goes

Failed to read data from Dht sensor
Response Timed Out

Failed to read data from Dht sensor
Response Timed Out

Failed to read data from Dht sensor
Response Timed Out

This is the code we use
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <WiFi.h>
#include <FirebaseESP8266.h>

// Replace with your Wi-Fi credentials
const char* ssid = "iPhone";
const char* password = "luigi.deguzman500";

// Replace with your Firebase project credentials
#define FIREBASE_HOST "https://esp32-agriboti-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "4xojUL5AyKj7kQwdVD7o4MVlk1fEzpPOYTq21sLf"

// Firebase data paths
#define TEMP_PATH "/sensor_data/temperature"
#define HUMIDITY_PATH "/sensor_data/humidity"
#define MOISTURE_PATH "/sensor_data/moisture"
#define EC_PATH "/sensor_data/ec"
#define PH_PATH "/sensor_data/ph"
#define N_PATH "/sensor_data/nitrogen"
#define P_PATH "/sensor_data/phosphorus"
#define K_PATH "/sensor_data/potassium"

// RS485 control pins
const int RO_PIN = 13; // Receive (data in) pin
const int DI_PIN = 12; // Transmit (data out) pin
const int RE_PIN = 16; // Receive Enable pin
const int DE_PIN = 5; // Driver Enable pin

// DHT sensor configuration
const int DHT_PIN = 2; // DHT data pin
const int DHT_TYPE = DHT11; // DHT sensor type

// Objects and variables
SoftwareSerial swSerial(RO_PIN, DI_PIN); // RS485 serial communication
ModbusMaster node; // Modbus communication
DHT dht(DHT_PIN, DHT_TYPE); // DHT sensor object

FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
FirebaseData firebaseData; // Added declaration for Firebase communication object

// Function to set MAX485 to transmit mode
void preTransmission()
{
digitalWrite(RE_PIN, HIGH);
digitalWrite(DE_PIN, HIGH);
}

// Function to set MAX485 to receive mode
void postTransmission()
{
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, LOW);
}

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

// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
    delay(500);
    Serial.print(".");
}
Serial.println();
Serial.print("Connected to WiFi: ");
Serial.println(WiFi.SSID());

// Configure Firebase
firebaseConfig.host = FIREBASE_HOST;
firebaseConfig.api_key = FIREBASE_AUTH;
Firebase.begin(&firebaseConfig, &firebaseAuth);

// Configure RS485 pins and initialize
pinMode(RE_PIN, OUTPUT);
pinMode(DE_PIN, OUTPUT);
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, LOW);

// Initialize RS485 communication
swSerial.begin(9600);
node.begin(2, swSerial);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);

// Initialize DHT sensor
dht.begin();

delay(1500);

}

void loop()
{
uint8_t result;

// Read Modbus data (7x 16-bit registers starting at 0x0000)
result = node.readHoldingRegisters(0x0000, 7);
if (result == node.ku8MBSuccess)
{
    float temperature = node.getResponseBuffer(0) / 10.0;
    float humidity = node.getResponseBuffer(7);
    float moisture = node.getResponseBuffer(1) / 10.0;
    float ec = node.getResponseBuffer(2);
    float ph = node.getResponseBuffer(3) / 100.0;
    float nitrogen = node.getResponseBuffer(4);
    float phosphorus = node.getResponseBuffer(5);
    float potassium = node.getResponseBuffer(6);

    // Send Modbus data to Firebase
    Firebase.setString(firebaseData, TEMP_PATH, String(temperature) + " °C");
    Firebase.setString(firebaseData, HUMIDITY_PATH, String(humidity) + " %");
    Firebase.setString(firebaseData, MOISTURE_PATH, String(moisture) + " %");
    Firebase.setString(firebaseData, EC_PATH, String(ec) + " µS/cm");
    Firebase.setString(firebaseData, PH_PATH, String(ph));
    Firebase.setString(firebaseData, N_PATH, String(nitrogen) + " mg/kg");
    Firebase.setString(firebaseData, P_PATH, String(phosphorus) + " mg/kg");
    Firebase.setString(firebaseData, K_PATH, String(potassium) + " mg/kg");

    Serial.println("Modbus data sent to Firebase!");
}
else
{
    printModbusError(result);
}

// Read DHT sensor data
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();

// Check if DHT readings are valid
if (!isnan(temperature) && !isnan(humidity))
{
    // Send DHT data to Firebase
    Firebase.setString(firebaseData, TEMP_PATH, String(temperature) + " °C");
    Firebase.setString(firebaseData, HUMIDITY_PATH, String(humidity) + " %");

    Serial.println("DHT data sent to Firebase!");
}
else
{
    Serial.println("Failed to read data from DHT sensor");
}

delay(1500);

}

// Function to print Modbus error messages
void printModbusError(uint8_t errNum)
{
switch (errNum)
{
case node.ku8MBSuccess:
Serial.println(F("Success"));
break;
case node.ku8MBIllegalFunction:
Serial.println(F("Illegal Function Exception"));
break;
case node.ku8MBIllegalDataAddress:
Serial.println(F("Illegal Data Address Exception"));
break;
case node.ku8MBIllegalDataValue:
Serial.println(F("Illegal Data Value Exception"));
break;
case node.ku8MBSlaveDeviceFailure:
Serial.println(F("Slave Device Failure"));
break;
case node.ku8MBInvalidSlaveID:
Serial.println(F("Invalid Slave ID"));
break;
case node.ku8MBInvalidFunction:
Serial.println(F("Invalid Function"));
break;
case node.ku8MBResponseTimedOut:
Serial.println(F("Response Timed Out"));
break;
default:
Serial.println(F("Unknown Error"));
break;
}
}

But for dome reason it cant work can someone please help me know the possible problem

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help

1 Like

if you are talking about one of those

They are regarded as being a complete scam anyway...

Please, once you'll have fixed the code tags in the first post, provide also more information about the sensor.

Thet sensor is a well known scam, it can't work.

#include <ModbusMaster.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#include <WiFi.h>
#include <FirebaseESP8266.h>

// Replace with your Wi-Fi credentials
const char* ssid = "iPhone";
const char* password = "luigi.deguzman500";

// Replace with your Firebase project credentials
#define FIREBASE_HOST "https://esp32-agriboti-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "4xojUL5AyKj7kQwdVD7o4MVlk1fEzpPOYTq21sLf"

// Firebase data paths
#define TEMP_PATH "/sensor_data/temperature"
#define HUMIDITY_PATH "/sensor_data/humidity"
#define MOISTURE_PATH "/sensor_data/moisture"
#define EC_PATH "/sensor_data/ec"
#define PH_PATH "/sensor_data/ph"
#define N_PATH "/sensor_data/nitrogen"
#define P_PATH "/sensor_data/phosphorus"
#define K_PATH "/sensor_data/potassium"

// RS485 control pins
const int RO_PIN = 13; // Receive (data in) pin
const int DI_PIN = 12; // Transmit (data out) pin
const int RE_PIN = 16; // Receive Enable pin
const int DE_PIN = 5;  // Driver Enable pin

// DHT sensor configuration
const int DHT_PIN = 2;    // DHT data pin
const int DHT_TYPE = DHT11; // DHT sensor type

// Objects and variables
SoftwareSerial swSerial(RO_PIN, DI_PIN); // RS485 serial communication
ModbusMaster node;                       // Modbus communication
DHT dht(DHT_PIN, DHT_TYPE);              // DHT sensor object

FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
FirebaseData firebaseData; // Added declaration for Firebase communication object

// Function to set MAX485 to transmit mode
void preTransmission()
{
    digitalWrite(RE_PIN, HIGH);
    digitalWrite(DE_PIN, HIGH);
}

// Function to set MAX485 to receive mode
void postTransmission()
{
    digitalWrite(RE_PIN, LOW);
    digitalWrite(DE_PIN, LOW);
}

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

    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println();
    Serial.print("Connected to WiFi: ");
    Serial.println(WiFi.SSID());

    // Configure Firebase
    firebaseConfig.host = FIREBASE_HOST;
    firebaseConfig.api_key = FIREBASE_AUTH;
    Firebase.begin(&firebaseConfig, &firebaseAuth);

    // Configure RS485 pins and initialize
    pinMode(RE_PIN, OUTPUT);
    pinMode(DE_PIN, OUTPUT);
    digitalWrite(RE_PIN, LOW);
    digitalWrite(DE_PIN, LOW);

    // Initialize RS485 communication
    swSerial.begin(9600);
    node.begin(2, swSerial);
    node.preTransmission(preTransmission);
    node.postTransmission(postTransmission);

    // Initialize DHT sensor
    dht.begin();

    delay(1500);
}

void loop()
{
    uint8_t result;

    // Read Modbus data (7x 16-bit registers starting at 0x0000)
    result = node.readHoldingRegisters(0x0000, 7);
    if (result == node.ku8MBSuccess)
    {
        float temperature = node.getResponseBuffer(0) / 10.0;
        float humidity = node.getResponseBuffer(7);
        float moisture = node.getResponseBuffer(1) / 10.0;
        float ec = node.getResponseBuffer(2);
        float ph = node.getResponseBuffer(3) / 100.0;
        float nitrogen = node.getResponseBuffer(4);
        float phosphorus = node.getResponseBuffer(5);
        float potassium = node.getResponseBuffer(6);

        // Send Modbus data to Firebase
        Firebase.setString(firebaseData, TEMP_PATH, String(temperature) + " °C");
        Firebase.setString(firebaseData, HUMIDITY_PATH, String(humidity) + " %");
        Firebase.setString(firebaseData, MOISTURE_PATH, String(moisture) + " %");
        Firebase.setString(firebaseData, EC_PATH, String(ec) + " µS/cm");
        Firebase.setString(firebaseData, PH_PATH, String(ph));
        Firebase.setString(firebaseData, N_PATH, String(nitrogen) + " mg/kg");
        Firebase.setString(firebaseData, P_PATH, String(phosphorus) + " mg/kg");
        Firebase.setString(firebaseData, K_PATH, String(potassium) + " mg/kg");

        Serial.println("Modbus data sent to Firebase!");
    }
    else
    {
        printModbusError(result);
    }

    // Read DHT sensor data
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    // Check if DHT readings are valid
    if (!isnan(temperature) && !isnan(humidity))
    {
        // Send DHT data to Firebase
        Firebase.setString(firebaseData, TEMP_PATH, String(temperature) + " °C");
        Firebase.setString(firebaseData, HUMIDITY_PATH, String(humidity) + " %");

        Serial.println("DHT data sent to Firebase!");
    }
    else
    {
        Serial.println("Failed to read data from DHT sensor");
    }

    delay(1500);
}

// Function to print Modbus error messages
void printModbusError(uint8_t errNum)
{
    switch (errNum)
    {
    case node.ku8MBSuccess:
        Serial.println(F("Success"));
        break;
    case node.ku8MBIllegalFunction:
        Serial.println(F("Illegal Function Exception"));
        break;
    case node.ku8MBIllegalDataAddress:
        Serial.println(F("Illegal Data Address Exception"));
        break;
    case node.ku8MBIllegalDataValue:
        Serial.println(F("Illegal Data Value Exception"));
        break;
    case node.ku8MBSlaveDeviceFailure:
        Serial.println(F("Slave Device Failure"));
        break;
    case node.ku8MBInvalidSlaveID:
        Serial.println(F("Invalid Slave ID"));
        break;
    case node.ku8MBInvalidFunction:
        Serial.println(F("Invalid Function"));
        break;
    case node.ku8MBResponseTimedOut:
        Serial.println(F("Response Timed Out"));
        break;
    default:
        Serial.println(F("Unknown Error"));
        break;
    }
}

This is the code and yes we are using those sensors, its okay if even the sensors are not accurate we just need them to workkkk

Your DHT data pin is not correctly connected. Post your wiring diagram.

So, which one is causing the message? Or are both causing it?

Here it issss

Im not sure but it always says in the serial monitor that

Response timed out

Even when "working" those things just produce random nonsense.

The built in Arduino random number generator works just as well, and you don't need to buy and wire up any "sensor".

Its okay if its just random numbers qe just need a physical sensor that looks like it work or if even better acctually worksss

Then why are you bothering to post? Just connect up the "non-working" sensor, then write a very simple program that ignores the sensor and prints out some reasonable looking numbers.

e.g.

Serial.print("N: ");
Serial.println(random(0, Nmax));  
Serial.print("P: ");
Serial.println(random(0, Pmax);
// etc.

Just like the "sensor" does!

But the thing issss we need it to workkk or better yet the sensor senses just anything since it can be seen from the serial monitor

Hi, @meinado12

Your DHT data pin is possibly not correctly connected.

You could need a 10K pullup resistor as shown above.

Tom.. :smiley: :+1: :coffee: :australia:

Hi @meinado12 ,
Welcome to the forum..

Lot's of issues..
obvious mistake in the code that sticks out, you ask for 7 registers but try to read 8 results, not causing an issue yet as you node is not working..

the diagram is quite blurry and does not seem to match the code..

are you using esp32??
esp32-pinout-reference-gpio
don't use pin 2 for the dht it is also tied to the onboard led..
drop the software serial just use rx2,tx2 and Serial2, esp32 has 3 hardware uarts..
esp32-hardware-serial2-example
you also seem to be using some depreciated Firebase lib..
take baby steps..
get your sensor working then work on the db..

that 485 adapter board is a 5v board you need to protect the rx pin on the esp32 as it is 3.3v..
how-to-level-shift-5v-to-3-3v

be nice if you had a 485 adapter for your pc and could verify the sensor talks..

good luck.. ~q

But we only have 3 pins for our humiture sensor

Why not pick a completely different type of sensor?

A 9DOF IMU sensor from a reputable supplier gives you nine measurements that actually have meaning!

define what "work" means ?

[exagerating on]
may be you can replace the sensor just by the random() function... it will be simpler. :slight_smile:
[exagerating off]

I'm out as first post still looks ugly. Good luck

1 Like

Qe nasically just need the sensor to work and not use random numbers since, our proffesir can acctually check the codes and see that it didnt work