Skip to content

Commit

Permalink
added soilmoisture
Browse files Browse the repository at this point in the history
  • Loading branch information
amri-tah committed Jun 8, 2024
1 parent 5658bfa commit 59870fa
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions arduino codes/plantpulse_final.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
#include <FirebaseESP8266.h>
#include <DHT.h>

#define AOUT_PIN A0
const int minMoistureValue = 0; // Analog reading for very wet soil
const int maxMoistureValue = 1023; // Analog reading for very dry soil

#define RELAY_PIN D1 // The ESP8266 pin connected to the IN pin of relay
#define DHTPIN 4 // Set the pin connected to the DHT11 data pin
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 4 // Pin connected to the DHT11 data pin
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define FIREBASE_HOST "esp-to-firebase-test-default-rtdb.firebaseio.com"
Expand Down Expand Up @@ -56,7 +60,6 @@ void setup() {
digitalWrite(RELAY_PIN, HIGH); // turn off pump (active-low relay)
}


bool readMotorStatus() {
if (Firebase.getBool(firebaseData, "/Motor")) {
if (firebaseData.dataType() == "boolean") {
Expand All @@ -68,6 +71,26 @@ bool readMotorStatus() {
return false; // default to off in case of error
}

void sendSoilMoistureData(){
int sensorValue = analogRead(AOUT_PIN); // Read the analog value from sensor

// Map the sensor value to a percentage (0% to 100%)
int moisturePercent = map(sensorValue, minMoistureValue, maxMoistureValue, 100, 0);

Serial.print("Moisture: ");
Serial.print(sensorValue);
Serial.print(" -> ");
Serial.print(moisturePercent);
Serial.println("%");

if (Firebase.setInt(firebaseData, "/SoilMoisture", moisturePercent)) {
Serial.println("Soil Moisture data sent successfully");
} else {
Serial.print("Failed to send soil moisture data: ");
Serial.println(firebaseData.errorReason());
}
}

void sendDHTdata(){
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Expand Down Expand Up @@ -101,15 +124,17 @@ void sendDHTdata(){
Serial.println(firebaseData.errorReason());
}
}
// The loop function repeats indefinitely


void loop() {
sendDHTdata();
sendSoilMoistureData();
bool motorStatus = readMotorStatus();

if (motorStatus) {
digitalWrite(RELAY_PIN, LOW); // turn on pump (active-low relay)
} else {
digitalWrite(RELAY_PIN, HIGH); // turn off pump (active-low relay)
}
delay(4000); // check status every 2 seconds
delay(2000); // check status every 2 seconds
}

0 comments on commit 59870fa

Please sign in to comment.