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