@Deva_Rishi below is the full code I have compiled prior to your recomendations
#include <Adafruit_ADS1X15.h>
Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
//FirebaseESP8266.h must be included before ESP8266WiFi.h
#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>
#include <DHT11.h>
#define FIREBASE_HOST " " //Without http:// or https:// schemes
#define FIREBASE_AUTH " "
#define WIFI_SSID " " //
#define WIFI_PASSWORD " "
#define LED_PIN 12
#define LED2_PIN 13
#define LED3_PIN 15
#define LED4_PIN 2
#define LED5_PIN 0
#define LED6_PIN 16
#define LED7_PIN TX
DHT11 dht11(14);
float gas, pressu, pho, soil;
float PH;
float humidity;
float temperature;
//Define FirebaseESP8266 data object
FirebaseData firebaseData;
String path = "/values"; // change it accord with the project firebase
unsigned long sendDataPrevMillis = 0;
unsigned long previousMillis = 0;
const int interval = 60000;
uint16_t count = 0;
void printResult(FirebaseData &data);
#include <SoftwareSerial.h>
// Define the RX pin of the ESP8266
const int rxPin = RX;
// Create a SoftwareSerial object
SoftwareSerial rs485(rxPin, rxPin);
byte pHBuffer[10];
int pHBufferIndex = 0;
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
pinMode(LED5_PIN, OUTPUT);
pinMode(LED6_PIN, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
//Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
firebaseData.setBSSLBufferSize(1024, 1024);
//Set the size of HTTP response buffers in the case where we want to work with large data.
firebaseData.setResponseSize(1024);
if (!ads.begin()) {
while (1);
}
// Set the baud rate of the SoftwareSerial object
rs485.begin(115200);
}
void loop()
{
updateTimeStamp();
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
// Read the photoresistor from the sensor.
adc0 = ads.readADC_SingleEnded(0);
pho = map(adc0, 0, 1300, 0, 500);
Serial.println(adc0);
// Read the pressure from the sensor.
adc1 = ads.readADC_SingleEnded(1);
pressu = map(adc1, 0, 1400, 0, 900);
// Read the gas from the sensor.
adc2 = ads.readADC_SingleEnded(2);
gas = map(adc2, 0, 1400, 0, 14000);
// Read the soilmoisture from the sensor.
adc3 = ads.readADC_SingleEnded(3);
soil = map(adc3, 350, 1800, 100, 0);
Serial.println("adc=");
Serial.println(adc2);
Serial.println("gas");
Serial.println(gas);
// Read the humidity from the sensor.
humidity = dht11.readHumidity();
// Read the temperature from the sensor.
temperature = dht11.readTemperature();
// Check if there is any data available
if (rs485.available()) {
pHBuffer[pHBufferIndex++] = rs485.read();
}
if (pHBufferIndex >= 10) {
// All of the pH data has been received.
// Read the pH value from the buffer.
PH = pHBuffer[0];
// Clear the buffer.
pHBufferIndex = 0;
// Print the data to the serial monitor
Serial.print(PH);
}
Serial.print(pho); Serial.print("\t");
Serial.print(pressu); Serial.print("\t");
Serial.print(soil); Serial.print("\t");
Serial.print(gas); Serial.print("\t");
Serial.println();
// to send the data from the sensor to firebase vvvvv
Firebase.setString(firebaseData, path + "/ PHOTORESITOR" , pho);
Firebase.setString(firebaseData, path + "/ Pressure Sensor " , pressu);
Firebase.setString(firebaseData, path + "/ GAS Sensor " , gas);
Firebase.setString(firebaseData, path + "/Soil moisture" , soil );
Firebase.setString(firebaseData, path + "/readHumidity" , humidity);
Firebase.setString(firebaseData, path + "/readTemperature" , temperature);
Firebase.setString(firebaseData, path + "/PH" , PH);
Firebase.getString(firebaseData, "/values/PHOTORESITOR_DATA"); // to get the PHOTORESITOR data from firebase
Serial.println(firebaseData.stringData());
String PHOTORESITOR_DATA = firebaseData.stringData();
Serial.println("PHOTORESITOR_DATA=");
Serial.println(PHOTORESITOR_DATA);
if ( PHOTORESITOR_DATA.toInt() >= pho)
{
Serial.println("inside1");
digitalWrite(LED_PIN, 1);
}
else
{
digitalWrite(LED_PIN, 0);
}
/// GAS
Firebase.getString(firebaseData, "/values/GAS_Sensor_DATA"); // to get the GAS data from firebase
Serial.println(firebaseData.stringData());
String GAS_DATA = firebaseData.stringData();
Serial.println("GAS_DATA=");
Serial.println(GAS_DATA);
if ( GAS_DATA.toInt() >= gas)
{
Serial.println("inside2");
digitalWrite(LED3_PIN, 1);
}
else
{
digitalWrite(LED3_PIN, 0);
}
//Pressure
Firebase.getString(firebaseData, "/values/Pressure_Sensor_DATA"); // to get the Pressure data from firebase
Serial.println(firebaseData.stringData());
String Pressure_DATA = firebaseData.stringData();
Serial.println("Pressure_DATA=");
Serial.println(Pressure_DATA);
if ( Pressure_DATA.toInt() >= pressu)
{
Serial.println("inside3");
digitalWrite(LED2_PIN, 1);
}
else
{
digitalWrite(LED2_PIN, 0);
}
//Soil_moisture
Firebase.getString(firebaseData, "/values/Soil_moisture_DATA"); // to get the Soil_moisture data from firebase
Serial.println(firebaseData.stringData());
String moisture_DATA = firebaseData.stringData();
Serial.println("moisture_DATA=");
Serial.println(moisture_DATA);
if ( moisture_DATA.toInt() >= soil)
{
Serial.println("inside4");
digitalWrite(LED4_PIN, 1);
}
else
{
digitalWrite(LED4_PIN, 0);
}
//PH
Firebase.getString(firebaseData, "/values/PH_DATA"); // to get the PH data from firebase
Serial.println(firebaseData.stringData());
String PH_DATA = firebaseData.stringData();
Serial.println("PH_DATA=");
Serial.println(PH_DATA);
if ( PH_DATA.toInt() >= PH)
{
Serial.println("inside7");
digitalWrite(LED7_PIN, 1);
}
else
{
digitalWrite(LED7_PIN, 0);
}
//TEMPREATURE
// If the temperature and humidity readings were successful, print them to the serial monitor.
Firebase.getString(firebaseData, "/values/TEMPREATURE_DATA"); // to get the TEMPREATURE data from firebase
if (temperature != -1 && humidity != -1)
{
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.println(firebaseData.stringData());
String TEMPREATURE_DATA = firebaseData.stringData();
if ( TEMPREATURE_DATA.toInt() >= temperature)
{
Serial.println("inside4");
digitalWrite(LED6_PIN, 1);
}
else
{
digitalWrite(LED6_PIN, 0);
}
// Humidity
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Firebase.getString(firebaseData, "/values/HUMIDITY_DATA"); // to get the Humidity data from firebase
String HUMIDITY_DATA = firebaseData.stringData();
if ( HUMIDITY_DATA.toInt() >= humidity)
{
Serial.println("inside4");
digitalWrite(LED5_PIN, 1);
}
else
{
digitalWrite(LED5_PIN, 0);
}
}
else
{
// If the temperature or humidity reading failed, print an error message.
Serial.println("Error reading data");
}
// Wait for 2 seconds before the next reading.
delay(1000);
}
void updateTimeStamp()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
FirebaseJson json;
json.set("Photoresistor", pho);
json.set("Gas", gas);
json.set("Soil moisture", soil);
json.set("Temperature", temperature);
json.set("Humidity", humidity);
json.set("Pressure", pressu);
json.set("PH", PH);
json.set("Ts/.sv", "timestamp");
Serial.println("updateTimeStamp: " );
Serial.printf("Push data with timestamp... %s\n", Firebase.pushJSON(firebaseData, "/history of farm1", json) ? "ok" : firebaseData.errorReason().c_str());
previousMillis = currentMillis;
}
}