#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <DHT.h>
#define FIREBASE_HOST ""
#define FIREBASE_AUTH ""
#define WIFI_SSID "Redmi9"
#define WIFI_PASSWORD "qwerty1234"
#define DHTPIN 2
#define DHTTYPE DHT11
#define SOIL_MOISTURE_PIN A0
#define PUMP_PIN D1
#define THRESHOLD 500
DHT dht(DHTPIN, DHTTYPE);
FirebaseData firebaseData;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(PUMP_PIN, OUTPUT);
digitalWrite(PUMP_PIN, LOW);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}
void loop() {
// Read control mode value from Firebase
Firebase.getString(firebaseData, "/controlMode");
String controlMode = firebaseData.stringData();
// If control mode is set to "manual"
if (controlMode == "manual") {
// Read pump control value from Firebase
Firebase.getString(firebaseData, "/pumpControl");
String pumpControl = firebaseData.stringData();
// Turn pump ON or OFF based on pump control value
if (pumpControl == "ON") {
digitalWrite(PUMP_PIN, HIGH);
Firebase.setString(firebaseData, "/data4", "ON");
Serial.println("Pump ON");
} else {
digitalWrite(PUMP_PIN, LOW);
Firebase.setString(firebaseData, "/data4", "OFF");
Serial.println("Pump OFF");
}
}
// If control mode is set to "auto"
else if (controlMode == "auto") {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int soilMoisture = analogRead(SOIL_MOISTURE_PIN);
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Soil Moisture: ");
Serial.println(soilMoisture);
// Send data to Firebase
Firebase.setFloat(firebaseData, "/data1", temperature);
Firebase.setFloat(firebaseData, "/data2", humidity);
Firebase.setInt(firebaseData, "/data3", soilMoisture);
// Control water pump
if (soilMoisture < THRESHOLD) {
digitalWrite(PUMP_PIN, HIGH);
Firebase.setString(firebaseData, "/data4", "ON");
Serial.println("Pump ON");
} else {
digitalWrite(PUMP_PIN, LOW);
Firebase.setString(firebaseData, "/data4", "OFF");
Serial.println("Pump OFF");
}
}
// If control mode is not set to "manual" or "auto"
else {
Serial.println("Invalid control mode!");
}
}
Why is this in the Extension development section? Are you building any extension?
Here several examples of FireBaseDB.
Look for the Firebase console, which has a place where you can see the Firebase DB contents online.
It is useful for debugging.